HOWTO Develop Apache Module in C++

Apache Webserver is all written in ‘c’. So how does one develop using C++? Simple.

  1. Generate the stub in “c” using apxs tool (e.g. apxs -n foo -g)
  2. use extern “C” in the module file to define the handlers that Apache would invoke.
  3. LoadFile is your friend which will load

Here is a little more detail. Apache now comes with apxs tool that will generate a simple stub and with it compilation makefiles etc.

First I define Foo.h which contains a simple class implementation. For this howto I am keeping things simple by not splitting in multiple files. So here is Foo.h which only has the handler for incoming request and produces “Hello world from foo”.

Then I renamed the mod_foo.c to mod_foo.cpp and updated content. I declared a global object of Foo class. Again it could have been singleton but for now I simplified it. Important point to note is the use of extern "C" at line 8 so the c++ compiler would generate names which are compatible with c. And I register my hook using a lambda which is not strictly necessary.

Next is to update the makefile to include CXX and CXXFLAGS for libtool to find them.

Now comes configuring apache to register the module and load necessary dependencies. This will allow you to visit /foo on your httpd webserver (e.g. and see this code in action.

Finally make reload would compile, install and bounce apache. Otherwise you can run these operations separately and upload the module to your apache installation. Now go ahead and start the browser and visit the page to see your code running in all its glory.

Configure Apache to run PHP on Windows

Just rebuilt my Windows laptop and had to reload Apache, MySQL and PHP. Some may suggest WAMP or XAMPP projects to simplify things, but those are my last resorts. I like to have full control of the situation. So I downloaded the latest versions of each from respective site and installed them. And then the problem happened.

PHP installed successfully but had an error that it could not access httpd.conf so PHP files could not be executed. To get the short answer, I turned to Google. It took me about 10 minutes and sorting of 20 webpages to get to the right configuration. Only if I had gone directly to Apache it would be better. But here is what configuration in Apache has to be done to get PHP files recognized and run by Apache.

Modify httpd.conf file to have following.

# Tell Apache where to pick PHP Module from
LoadModule php5_module "C:/Program Files/PHP/php5apache2_2.dll"

# Tell Apache that it should treat index.php just like index.html
DirectoryIndex index.html index.php

# Add type for PHP file handler
AddType application/x-httpd-php .php
# This is optional but allows user to access php source file (php files with extension  renamed) with full syntax highlighting
AddType application/x-httpd-php-source .phps