This is something I’ve been thinking about for awhile because many organizations and hosting solutions are slow to adopt PHP 5 because it might break some stuff. Meanwhile I was writing stuff that used features that required PHP 5 and nobody could host it!
So today I decided to see how easy it is to fix this problem. So here’s what you’ll need:
- Compiler tools. GCC, linkers, etc.
- Apache 1.3 or later.
- Latest PHP 4.x and 5.x source code from http://www.php.net
I’ll assume here that you want PHP 4 to be default and then if a user prefers PHP 5 instead, he can enable it himself.
Install PHP 4
You can skip this step if PHP 4 is already installed. Basically make sure you use the –with-apxs configure option and install it like you normally would.
See http://www.php.net/manual/en/install.php
Install PHP 5
This is very important. Before you do anything else, you must modify two lines in one source file.
Open your PHP source folder (ex: php-5.1.0) and navigate to sapi/apache. There you will find a file called mod_php5.c.
Inside this file you will find a block of code that looks like this:
handler_rec php_handlers[] =
{
{”application/x-httpd-php”, send_parsed_php},
{”application/x-httpd-php-source”, send_parsed_php_source},
{”text/html”, php_xbithack_handler},
{NULL}
};
You must add a 5 suffix to application/x-httpd-php and application/x-httpd-php-src so that the block of code becomes something like:
handler_rec php_handlers[] =
{
{”application/x-httpd-php5″, send_parsed_php},
{”application/x-httpd-php5-source”, send_parsed_php_source},
{”text/html”, php_xbithack_handler},
{NULL}
};
And that’s it.
Now one thing you must remember is that your optional PHP version must be installed from source. Why? Because it must be installed in a separate location, isolated from everything else.
You must use the –with-apxs and –prefix configure options. I set the prefix to /usr/local/php4 which should work well for anyone. (–prefix=/usr/local/php4)
Configure Apache
Now this is the part that makes things really tick.
When running make install, apxs should have added the lines needed to load both mod_php5 and mod_php4 and you also need the following lines:
AddType application/x-httpd-php .php AddType application/x-httpd-php-source .phps
Like this all PHP files (files with the .php extension) will be parsed with PHP 4. Now what you could do is add these lines so that files with the .php5 extension are parsed with PHP 5:
AddType application/x-httpd-php5 .php5 AddType application/x-httpd-php5-source .php5s
But that would be no fun. And it’s not nearly future proof. But simply a bad idea.
There are two ways of fixing this problem. You want each user to be able to select either PHP 4 or 5.
You most likely already allow them to use .htaccess files, so your users could simply create .htaccess files–where they want PHP 5–containing the x-httpd-php5 lines and they are set to go.
I believe this is the best approach because this allows your users to use PHP4 in some places and PHP5 in others.
But another way to avoid .htaccess files is using user configuration files.
On Mac OS X each user has his own configuration file located in /etc/httpd/users/user.conf.
And at the bottom of httpd.conf there is an include which reads like this:
Include /private/etc/httpd/users/*.conf
So if users are allowed to edit their own *.conf file, they could add the correct AddType lines there.
Another more complex solution is, if you provide a web configuration interface such as CPanel, have an option where you can select PHP 4/5. This option would change the configuration for that user at the apache level.
So as you can see, running both PHP 4 and 5 concurrently, or parallel, is really easy if done right. Just do it!