Part 2: The Windows way.
Until Apache 2.0, the community disregarded using the webserver on Windows systems. It’s true that the 1.3 release also had a Windows version, but it has always seemed that it was treated like the ugly duckling. Even the release notes stated that it works, but it’s not recommended to use for anything serious.
With the 2.0 and subsequent releases the state of the matter changed. Windows support and performance got a hell lot better, and people use it. This is why when you create a module (on Linux first, of course, since it’s easier) you should consider porting it to Windows. And Apache helps a lot in this regard with the APR runtime, which hides away all of the ugly differences between OS’s. The only problem that remains is that you cannot find a quick, step-by-step guide on how to build a module. Yes, a single module, and not the entire Apache tree. Building the tree is explained, fairly detailed even on Apache’s website. So let’s get down to action.
What we’re trying to achieve here is to write a simple module, targeting Apache 2.0 and using free tools along the way. To build it I have used:
- The Apache 2.x sources (mostly for inspiration, and when I want to build the entire tree)
- Microsoft’s Platform SDK
- Microsoft’s Visual C++ 2005 Express Edition
- Lots of coffee.
First off you will have to download and install the tools above, if you haven’t already. As a rule of thumb, installing VS Express first and the Platform SDK afterwords worked best for me.
Before you are going to start off on the actual module coding, you need to setup the workspace:
- First off you must tell VS 2005 where the Platform SDK can be found. Otherwise it won’t found the many needed required header files. To do this, open VS 2005 and from the Tools menu open the Options dialog.
- In the Projects and Solutions > VC++ Directories you will have to add an entry for Include files, Executable files and Library files:
- Executable files: [install_path]Bin
- Include files: [install_path]Include
- Library files: [install_path]Lib
Regarding the actual module creation you can either import the entire Apache project tree (conveniently put as a Visual Studio project) or create a single project with the module alone. Let’s look at both ways:
Building the entire Apache tree
Instructions on how to import and build the Apache tree are easy to find in the documentation, so I won’t go there. But, after you have imported the project in VS you should see lots of smaller sub-project, for each of the default modules. This is great, as you can look at how they are configured and create your own.
To create your own module – let’s say mod_foo you’d go like this:
- Import the Apache .vbs file which creates all the projects.
- Right-click on the Apache solution and select Add New Project. Give it a name and click Next to get to the configuration wizard.
- In the configuration dialog, the most important is to pick the right project type. Select the Dynamic Library (.dll) library one.
- Then right click the new project and select Add > New Item > Code > cpp file. Give it a name, and before clicking OK change the extension to .c (more similar to *NX).
- To make sure that the module will find the relevant include files and dependant libs you will have to add the [apache_src_root]/includes folder to the Additional includes property on the new project. Also add a dependency on the libapr subproject in the Apache solution.
- Write the code. At last! Copy the code in part 1. Because of the beauty in apr it will just work.
- Build the entire solution.If you have built it at least once before you can just build the new module
If you don’t get any errors in the build above you can go ahead and install the module. Copy it to the [apache2_root]/modules and modify the httpd.conf file to load it. Also make sure you assign a virtual folder to call the module, so you can test it out. One little Apache restart and you’re done. You should be able to navigate in your browser to the virtual mappting you’ve defined earlier and it should work.
I later realized (thanks to wizardcoder) that if someone only used windows, without going through the Linux part, the code needed in the steps cannot be copied from anywhere. So here it is – a zip with a default simple module.
Could you provide a small sample, “6.Write the code. At last! Copy the code in part 1. Because of the beauty in apr it will just work.” is a quite short for me ^_^…
I have juste finish to write a plugin for Firefox, there’s a small sample called “Simple” on gecko, it’s a good exemple…
The code is the same as in part 1 – creating the module under Linux. The main point of the apache runtime is that it hides the differences between coding a module for Unix and Windows or Mac.
i am new bee in PHP. i want to create a CAS module in apache under windows, but i can’t figure out any solution.please help me.
There already is a CAS module for Apache – just look over the tutorial here (http://www.sfu.ca/itservices/cas/Install_SFU_mod_cas.html) to install it.
Other than that, in order to write an Apache module you do not use PHP, but rather C/C++ and Apache’s own ARP libraries.
hi again cristi,
First of all thanx for your response.you provided me a link, that i already visited.but my problem is that i want mod_cas module for windows.i want to use it in drupal.on that particular link installaion is Linux user.i have link for windows http://news.softpedia.com/news/Create-Loadable-Modules-for-Apache-on-Windows-64149.shtml
.but firstlly i want mod_cas for installation.plz help me.
I have to say this is one of the worst explanations i’ve seen yet, can we have some real details not just vague ideas of proceses you go through.
Maybe a link back to your 1st tutorial may help explain some of the things you missed here (i’ve been unable to find it so have no idea).
The first part is here: https://ivascucristian.com/archives/how-do-you-create-a-new-apache-module/
I hope this will help clear some things. In any case, this does not intend to be a step-by-step tutorial, more like a list of pointers/tips. Sorry I haven’t been more clear
Can we create apache module which can talk to database?
These modules are written in C (or Perl occasionally) so if you can (and want to) connect to a database using this, yes you can. However I think more suited for this would be using an existing module, like mod_python, and write your program in that.
Other than being overly complicated, you can write code inside your module that will connect and do something with the database. Since the modules get loaded when apache starts though, you will have to deal with the connection and everything else directly inside the method that processes the actual request.
We are using “/portal” context for our web application.
We want to use Auth MemCache Cookie(http://sourceforge.net/projects/authmemcookie/) apache module to protect our web application context “/portal”.
But the problem is it will also protect our login page (/portal/login.jsp).
Is possible modify module source code such that it will protect for “/portal” url except (/portal/login.jsp)?
Please advise. Any help on would be greatly appreciated.
Can it possible
My newly created module is still a .so file.
Should I rename it DLL?