Wednesday, July 01, 2009


Apache 2.2.x modules with Delphi II

After my "Apache 2.2.x modules with Delphi" article, I received many requests for an example, and here it is.

Creating the directory structure

Create a directory where your Delphi project will reside, for example C:\myModule, then copy the files HTTPD2.pas, ApacheTwoApp.pas and ApacheTwoHTTP.pas from your Delphi/Source directory to the newly created directory.

Patching HTTPD2.pas

Open the file c:\myModule\HTTPD2.pas and replace/fix the lines as shown in my previous post.

The code

This is a very small HelloWorld WebBroker example, it is composed of only three files, mod_helloworld.dpr, main.pas and main.dfm


library mod_helloworld;

HTTPD2 in 'HTTPD2.pas',
main in 'main.pas' {WebModule1: TWebModule};

{$E so}

{$R *.res}

apache_module name 'helloworld_module';

Application.CreateForm(TWebModule1, WebModule1);


unit main;


SysUtils, Classes, HTTPApp,

TWebModule1 = class(TWebModule)
procedure WebModule1WebActionItem1Action(Sender: TObject;
Request: TWebRequest; Response: TWebResponse; var Handled: Boolean);
{ Public declarations }

WebModule1: TWebModule1;


{$R *.dfm}

procedure TWebModule1.WebModule1WebActionItem1Action(Sender: TObject;
Request: TWebRequest; Response: TWebResponse; var Handled: Boolean);
Response.Content := 'Hello from Apache Module!';



object WebModule1: TWebModule1
OldCreateOrder = False
Actions = < default =" True" name =" 'WebActionItem1'" pathinfo =" '/test'" onaction =" WebModule1WebActionItem1Action">
Left = 679
Top = 385
Height = 150
Width = 215

Compilling the module

Save the three files in c:\myModule and open the .dpr with Delphi, then compile to your Apache2 modules directory, usually in C:\Program files\Apache Software Foundation\Apache2.2\modules.

Configuring Apache2

If the Apache 2.2 service it's running, please stop it, then open your C:\Program files\Apache Software Foundation\Apache2.2\conf\httpd.conf file and look for the "LoadModule" entries, add this after the last entry:

LoadModule helloworld_module modules/

And below this line add this:

<Location /test>
SetHandler mod_helloworld-handler

Testing the module

Restart your Apache 2.2 service and open a web browser, then type http://localhost:8080/test, it should show a page with the text "Hello from Apache Module!".

That's it.
Thanks so much for the information!

Now I have a question: What effect does the keepalive variable in HTTPD2 have? I can find lots of google hits, but no clear explanation.
Did you read this?:
I have an Apache module that I made using your post. It was working fine until I compiled my module using delphi7 on windows7. The unique change is to compile on windows7 because I´ve compiled always with Delphi 7. Now I don´t have a clue of what is happening. Please, I appreciate your help.

Did you try compiling it with Delphi 2007 or newer?. Newer versions of Delphi have Windows Vista+ support, older version does not.
I fixed it!! there wasn´t necessary to compile using delphi 2007. I just redeclared one of the functions of one of my unit interfaces, which where returning a complex type (an object containing methods with null terminated String as params). I were making my Apache extension crash. Thanks anyway for your advise, you have a very useful blog.
Im looking for answer how can I setup my delphi written apache module (web broker applitation) for entire subdomain (virtual host) on my web server. When I'm trying to use < Location /> directive in httpd.conf or httpd-vhosts.conf it doesn't work. It seems, that Location can not be only '/', it must be non-empty path. I tryed also "SetHandler myapp-handler" i both config files without properly result. There is always displayed page for default acion, but never for other actions identified by pathinfo property of webaction.
hi, how do I deploy this .so on a linux apache server? I get a ELF header error when I try to start the server.

You cannot deploy a .so created by Delphi, for this, please take a look here:
Post a Comment

Links to this post:

Create a Link

<< Home

This page is powered by Blogger. Isn't yours?