Enabling Custom Apache Server Modules for PHP Sites

| August 29, 2013

To meet all of the requirements of your PHP application, sometimes you need to add custom modules. Modular architecture is one of the major reasons for the Apache server's global popularity. Most web sites are powered by this server and our stats confirm this fact. Such architecture allows easy extension with optional functionality, third party add-ons, and custom modifications. Of course, a new module isn't always the right solution for every problem. If you are sure this is the only solution for your PHP application, Apache's architecture is designed in such a way that it's should not be a daunting task. Today we'll illustrate key points of Apache module's architecture and show you how to enable them in Jelastic ZEROCodeChange PaaS.

Note: Apache modules installed in the Jelastic Cloud by default can be found in this list

Create environment

1. First of all log into your Jelastic account and click on the Create environment button at the top left corner of the dashboard.

2. In the opened dialog, go to the PHP tab, pick Apache as your application server and specify the number of cloudlets your application needs. Then type the name for your environment and confirm the environment creation.

Environment with Apache Server for PHP Sites

In several seconds your environment will be available from the Jelastic dashboard.

Apache Server Environment

Build your Apache server module

1. Download Apache httpd from apxs with the same version as your Apache server. In our case we have Apache 2.2.15, so we will download and install httpd 2.2.15.

2. Download the source of the required module in the Apache modules list. For the purpose of this example, we will use an entertaining module named mod_pony.

3. The next step is your Apache module compilation. Here's how we've compiled our mod_pony module:

apxs -i -a -c mod_pony.c
/usr/lib64/apr-1/build/libtool --silent --mode=compile gcc -prefer-pic -O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector --param=ssp-buffer-size=4 -m64 -mtune=generic -Wformat-security -fno-strict-aliasing -DLINUX=2 -D_REENTRANT -D_GNU_SOURCE -pthread -I/usr/include/httpd -I/usr/include/apr-1 -I/usr/include/apr-1 -c -o mod_pony.lo mod_pony.c && touch mod_pony.slo
/usr/lib64/apr-1/build/libtool --silent --mode=link gcc -o mod_pony.la -rpath /usr/lib64/httpd/modules -module -avoid-version mod_pony.lo
/usr/lib64/httpd/build/instdso.sh SH_LIBTOOL='/usr/lib64/apr-1/build/libtool' mod_pony.la /usr/lib64/httpd/modules
/usr/lib64/apr-1/build/libtool --mode=install cp mod_pony.la /usr/lib64/httpd/modules/
libtool: install: cp .libs/mod_pony.so /usr/lib64/httpd/modules/mod_pony.so
libtool: install: cp .libs/mod_pony.lai /usr/lib64/httpd/modules/mod_pony.la
libtool: install: cp .libs/mod_pony.a /usr/lib64/httpd/modules/mod_pony.a
libtool: install: chmod 644 /usr/lib64/httpd/modules/mod_pony.a
libtool: install: ranlib /usr/lib64/httpd/modules/mod_pony.a
libtool: finish: PATH="/usr/local/sbin:/bin:/sbin:/usr/bin:/usr/sbin:/sbin" ldconfig -n /usr/lib64/httpd/modules
Libraries have been installed in:
If you ever happen to want to link against installed libraries
in a given directory, LIBDIR, you must either use libtool, and
specify the full pathname of the library, or use the `-LLIBDIR'
flag during linking and do at least one of the following:
- add LIBDIR to the `LD_LIBRARY_PATH' environment variable
during execution
- add LIBDIR to the `LD_RUN_PATH' environment variable
during linking
- use the `-Wl,-rpath -Wl,LIBDIR' linker flag
- have your system administrator add LIBDIR to `/etc/ld.so.conf'
See any operating system documentation about shared libraries for
more information, such as the ld(1) and ld.so(8) manual pages.
chmod 755 /usr/lib64/httpd/modules/mod_pony.so
[activating module `pony' in /etc/httpd/conf/httpd.conf]

4. Finally you'll get an .so file of your Apache module. We got a mod_pony.so file with the following path: /usr/lib64/httpd/modules/mod_pony.so .

Upload and configure your module

1. Go back to your Jelastic environment, which you created earlier and click the Config button for Apache server.

Apache Server Configuration

2. Then upload your .so file to the modules folder.

so File Uploading

3. Navigate to the conf folder and put the corrections to the httpd file by adding the LoadModule string in the following format:

LoadModule {module-name}_module /usr/lib64/php/modules/{file-name}.so


LoadModule pony_module /usr/lib64/php/modules/mod_pony.so

Httpd File Editing

4. Specify the settings required for your module.

E.g., for further configuration of the mod_pony module we need to create a .htaccess file inside of the webroot > ROOT directory and to name a location that maps to the handler:

<Files pony>
     SetHandler pony
Apache Server Modules Settings

Don't forget to save your settings!

5. Restart the Apache server in order to enable your Apache module.

6. Ensure that your Apache module works properly. In our case we've got a pony image.

Apache Server Module

In just a few simple steps you can enjoy your own custom modules. We can conclude that the Apache server exemplifies much of what is best about the open-source software development paradigm. It is not only highly efficient and secure as a web server, more importantly it is very customizable and highly effective.