Apache with multiple PHP Versions

This is the third part of our Gentoo tutorials. In the first two parts we have installed Gentoo from the minimal installation disc and added some interesting applications and utilities.

In this third part we will install an apache webserver with several different php versions. I will use php4 and php5, however this guide can be used for php5 and php6 (as soon as there is at least a release candidate version) or even it can be used to install multiple different php5 versions – it all depends on your needs.

First thing first:

Requirements

The requirements for this tutorial are a bit modest :-)

  • Gentoo Installation (You can use any flavor of Linux and their own installation system such as rpm, apt-get.. etc, but I will only cover the gentoo emerge system syntax. You will need to find the right syntax for your version on your own)
  • Stable and preferably fast connection to Internet.
  • A lot of free time.

We will begin with installing the base – our Apache webserver

Apache Installation

The installation procedure is straight and forward:

emerge -av apache

This will list the USE flags and what exactly would be installed. Check if SSL is enabled and type ‘Yes’ to begin the actual installation. After a couple of minutes the compile should be finished. To start Apache at boot time, we have to add it to the default run level:

rc-update add apache2 default

The default configuration of apache should be just fine for your needs, but you might want to have a quick look at it. It should be located insider your /etc/apache2/httpd.conf file. One thing that I find quite irritating is the long path to the default vhost document root. And to make it much easier for me to manage my web files I added a symbolic link:

ln -s /var/www/localhost/htdocs /www

Now, I can simply type ‘cd /www’ and I’m there :-) Nice, eh?

I mentioned SSL, however although it is compiled and there is support for SSL in the apache server, it is not installed by default. To install it we need to first generate a key and crt file and then enable it in the configuration. To do so, execute the following commands:

cd /root
mkdir ssl
cd ssl

openssl genrsa -out serv.key 1024
openssl rsa -in server.key -out server.pem
openssl req -new -key server.pem -out server.csr
openssl x509 -req -days 365 -in server.csr -signkey server.pem -out server.crt

cp server.key /etc/apache2/ssl/
cp server.crt /etc/apache2/ssl/

To tell apache where are our new custom key and certificate file, we edit the configuration file, which is located in /etc/apache2/modules.d/41_mod_ssl.default-vhost.conf. Set the following in the file:

SSLCertificateFile /etc/apache2/ssl/server.crt
SSLCertificateKeyFile /etc/apache2/ssl/server.key

The last step is to edit /etc/conf.d/apache2 and add “-D SSL -D SSL_DEFAULT_VHOST” to enable SSL.

Start apache by typing:

/etc/init.d/apache2 start

and visit both http://localhost/ and https://localhost/ (if it was on your localhost of course, replace with IP/domain if you are performing network install). If you do not see any errors and a default apache page is displayed – we are all set to continue with

Installing the first PHP

We will use ‘emerge’ to install the first PHP so that it can automatically resolve any dependencies that our second (and third, and forth .. and so on) php might require. To spice the installation a little bit, I’ve added the following line in my /etc/portage/package.use file:

=dev-lang/php-5* cgi force-cgi-redirect bzip2 curl ftp gd mhash odbc soap sockets tokenizer xml xmlreader xmlrpc xmlwriter xpm xsl zip

If you don’t need any of the above, feel free to remove it from the list. I should also mention that if you do not have mysql installed on the system, emerge will try and install it for you. It is ok for me to install it (I will need it in the future anyway) and thus I left the line just like that.

Now compile your initial php by typing:

emerge -av ‘=dev-lang/php-5*’

Now that should take a long time to complete. Grab a pizza, watch tv .. do something! :) It took my p4 dual core about 45 minutes to compile.

Once finished, if you have installed the mySQL (as I did) with that emerge, we should configure it first:

emerge –config =dev-db/mysql-5.0.44-r2

Your version might be different – check what emerged installed and run the config option. It will ask you for a root password for your mySQL server and chown/chmod and in just a simple word – configure mySQL.

At that step we have both Apache and php installed configured and ready to run, but they still don’t know each other. To introduce them we will use a module called mod_suphp. SuPHP is a little, handy tool which will allow us to run several php versions. It will also add additional security by executing the php scripts using the owner/configured user instead of the webservers default, but that’s another security topic which I won’t cover here.

To install mod_suPHP I had to enter the following line in my /etc/portage/package.keywords:

=www-apache/mod_suphp* ~x86

and to configure it, I added the following line in my /etc/portage/package.use file:

www-apache/mod_suphp* mode-force checkpath

Compile the module by typing:

emerge -av mod_suphp

It should compile quickly (as it is about 400k in size). Once installed, add the following option in your /etc/conf.d/apache2 file:

-D SUPHP

Great. We now have apache and php working together via mod_suPHP. Lets test it out. Go to your /www (which is a sym link to the real /var/www/localhost/htpdocs folder) create a folder called php5 and place a new index.php file inside:

cd /www
mkdir php5 && cd php5
touch index.php

Now open index.php with your favourite text editor (nano, vi .. whatever you use) and put the following code inside it (and yes you need to place exactly that code as we will need it later for the second php):

<?php phpinfo();?>

Go ahead and try to view it with your browser by going to http://localhost/php5/. If you have followed everything step by step you must see an ’500 Internal Server Error’. The problem is due to the fact that you have created the file using the root username, while suPHP expects the file owner to match the virtual host owner and additionally, as added security, suPHP will not allow users which UID is less than 1000 and GID less than 100. In a real server environment you will need to add a separate vhost and configure a non-root user in order to take advantage of the added security, but for now, we will just reconfigure suPHP to allow scripts to run with user apache. First we need to determine the apache user UID and GID. To do so we type:

pancho ~ # id apache
uid=81(apache) gid=81(apache) groups=81(apache)
pancho ~ #

On my server, apache has UID=81 and GID=81 so what I need to do is to edit /etc/suphp.conf and set the following values:

min_uid=81
min_gid=81

Finally, chown the www directory recursively and set the user to apache and group to apache:

chown -R apache: /www

That’s it. The index.php file should now load just fine and a big phpinfo page should load.

Adding the second php


Once again, I will use the latest (and probably last?) php4 version 4.4.7, but you can use any version you might need – it doesn’t really matter. Since emerge doesn’t allow me to install php4 anymore, I will install it by hand. First we download the php4 install tar file from www.php.net and extract it somewhere on the disc (I used /root/php folder, but any directory will do).

Next, we need to configure the php. To do so we will use the phpinfo page we have created earlier. At the top of the phpinfo page there is a very long line that starts with ‘./configure’. Copy that line into a text editor and edit the paths so that it will install the new php into its very own directory. In my case I have edited the following values:

‘–prefix=/usr/lib/php4′
‘–mandir=/usr/lib/php4/man’
‘–infodir=/usr/lib/php4/info’
‘–with-config-file-path=/etc/php/cgi-php4

Now configure the php and install it:

./configure

make
make install

This installed the php binary into /usr/lib/php4/bin folder and the binary itself is called php. Since suPHP expects (for some reason) that the binary name would be php-cgi, we have to edit its conf file (/etc/suphp.conf) again and set:

x-httpd-php4=php:/usr/lib/php4/bin/php-cgi
to
x-httpd-php4=php:/usr/lib/php4/bin/php

That’s it! We have php4 (or whatever php version you have selected) installed. Let’s now test it – create a folder /www/php4 and put the same index.php file as in php5. Now visit the page with your browser – it should still show php5. That’s because the .php fle extension is still served by the php5 extension. To correct this we will have to instruct Apache that .php files in this particular directory should be handled by php4 binary. To do so, create an .htaccess file inside the php4 directory and put the following inside:

AddHandler x-httpd-php4 .php

If you refresh the page now, you should see the change. That’s it – we have now two different php versions running concurrently without a need of a restart of apache.

Bookmark and Share

bookmark bookmark bookmark bookmark bookmark bookmark bookmark bookmark bookmark bookmark bookmark bookmark
tabs-top  banner ad


One Response to “Apache with multiple PHP Versions”

  1. Melnyk says:

    Fantastic goods from you, man. I’ve understand your stuff previous to and you are just extremely excellent. I really like what you’ve acquired here, certainly like what you are stating and the way in which you say it. You make it enjoyable and you still care for to keep it sensible. I can’t wait to read much more from you. This is really a great website.

Leave a Reply