Tuesday, December 6, 2011

PHP Compiler Part Two: An Introduction to Apache and PHP

I use XAMPP. Relieves me from the hassle of having to download and install Apache and then install PHP and then configure them to work with each other. Things get only more complicated if I want to install MySQL, Perl, etc. "XAMPP is an easy to install Apache distribution containing MySQL, PHP and Perl". Yes, it is. And not just that, it comes with phpMyAdmin, PEAR (the PHP Extension and Application Repository), Perl, Mercury Mail Transport System, etc and even a bunch of modules for Apache, all of them configured and ready to use. The latest version even comes with Tomcat to run Java Servlet Pages.

Apache is the actual web-server. It receives requests from browsers. PHP works under Apache. Apache's functionality can be extended by Modules. They may be loaded through directives specified in the Apache configuration file located at Apache/conf/httpd.conf. Apache also supports CGI. Here, Apache calls an executable to generate the web page that is to be given as response back to the browser. The CGI script can receive POST variables via stdin or Apache can also set environment variables which can be accessed within the CGI script. The output is given back to Apache via stdout.

PHP can be run either as an Apache module (mod_php) or as a CGI program. From what I understand, running PHP as a module is faster while running it as a CGI executable is more secure. XAMPP keeps its configuration separate from the main Apache config by using an Include directive (similar in function to C's #include) in httpd.conf to include httpd-xampp.conf.

Running PHP as an Apache module
When running PHP as a module, you should see something like php5apache2.dll or libphp5.so in your Apache/modules folder. .so files are shared libraries. They are the Unix/Linux equivalent of .dll files in Windows. You might be seeing a combination of .dll and .so files in your modules folder. The extension really doesn't matter.
LoadModule php5_module modules/php5apache2_2.dll

<IfModule php5_module>
<FilesMatch "\.php$">
SetHandler application/x-httpd-php
</FilesMatch>
</IfModule>
A configuration like the one above specifies the module to load and if the php5_module has been loaded, Apache is instructed to recognize all requests that match the regular expression \.php$ as PHP scripts.

Running PHP as a CGI program
When running PHP in this mode, you should notice a php-cgi.exe in your php folder. It is the CGI version of the PHP interpreter. If you have installed xampp in your program files folder, your config should look something like this.
ScriptAlias /php-cgi/ "C:/Program Files/xampp/php/"

<IfModule !php5_module>
<FilesMatch "\.php$">
SetHandler application/x-httpd-php-cgi
</FilesMatch>
<IfModule actions_module>
Action application/x-httpd-php-cgi "/php-cgi/php-cgi.exe"
</IfModule>
</IfModule>
To replace something like PHP, I realized I would have to build my own module for Apache which I could then set as the handler for PHP scripts. More on that in Part 3.

No comments:

Post a Comment