Logo

Setting up PHP5 in parallel with PHP4 in SuSE 9.2

SuSE 9.2 ships with Apache2 and PHP4. In order to install PHP5 some extra work is therefore required. This Mini-HOWTO explains how to setup PHP5 in parallel with PHP4 and also how to easily switch between PHP4 and PHP5 on the same server using Apache2 and also how to maintain different ini-files for PHP4 and PHP5.

 

Configuration files and directories for Apache2 in SuSE 9.2

Before we start we give a short overview of where important configuration files and directories for Apache2 are located in SuSE 9.2
Note: The configuration files setup by SuSE 9.2 is different from the standard one-single "etc/httpd.conf" used by other systems and as recommended in the Apache2 documentation.

Dir: /etc/apache2

Purpose: General configuration directory for Apache2, this is where the "httpd.conf" lives.

Dir: /etc/apache2/conf.d

Purpose: Module configuration files, for example php4.conf . All the configuration files in this directory will be automatically read by the main httpd.conf by means of an "include conf.d/*" command so the exact name doesn't really matter as long as the file is in this directory.

File: /etc/sysconfig/apache2

This is the main Apache2 configuration file. This file is the one that is really used to configure apache when it is started. This is also the file that the "Yast2" HTTPD-module edits.

From our point of view the most important thing is that this is the place where we tell Apache2 what external modules to load.

In the SuSE 9.2 configuration this is done by listing all the modules in the string variable APACHE_MODULES. In the SuSE configuration there are no static "AddModule" directives in any of the configuration files for Apache. Instead this is dynamically generated each time apache is started (for example by /etc/init.d/apache2 start)

The generation of the actual module file names is quite clever in that the script looks at the core module name in the APACHE_MODULE variable and automatically determines the name of the file name of the load modules. This means that for PHP we only have to give the name "php4" or "php5" as the name of the module. The script will then discover that the name of the file load module is in fact "libphp4.so" or "libphp5.so" automatically.

 

Making sure you have the correct Apache2 setup

PHP is only guaranteed to work with the Apache2 "Prefork MPM" (Multi-Processing-Module) and you need to have apache2-prefork installed. This also means that the APACHE_MPM in /etc/sysconfig/apache2 must NOT be set to "worker". You can read more about the reasons for this issues in the Apache Documentation : Thread Safety For general information about MPMs please see Apache Documentation : MPM

If you use Yast2 to install Apache2 and the prefork module then all this will be automatically setup.

Before continuing please make sure that You have successfully installed Apache2 on your server. For example by directing your favorite browser to http://localhost/

 

Installing PHP5

For some reason SuSE 9.2 sees fit to not include (or make available) PHP5 RPMs so we need to manually install this version of PHP. In the rest of the text we will go through installing and compiling PHP5 for SuSE 9.2

Step one; install PHP4 RPMs

Yes, you read it right. By installing PHP4 (from the SuSE 9.2 CD or DVD) we get some configuration files for free that we will need anyway for PHP5. Primarily we will get "/etc/apache2/conf.d/php4.conf" created and also the APACHE_MODULE string updated with an "php4" module so we can easily see where we need to later change this to "php5".

Step two; Enable the PHP4 module in the Yast2 HTTPD configuration

This will allow us to check that the PHP4 is fully working with Apache2.

Run a standard PHP script; for example by copying the following script to "/srv/www/htdocs/"

<?php 
phpinfo(); 
?>
and name it as "phpinfo.php" . If you now go to your favorite browser and run this script as "http://localhost/phpinfo.php" you should get the standard PHP4 information presented as a quite big table.

Please note that if You run PHP4 directly from the RPMs most of the PHP4 additional functionality are built as PHP modules which needs to be enabled in the "/etc/php.ini" configuration file (specifically You need to enable libgd to make JpGraph work.)

Step three; compile PHP5

First download the latest PHP5 tar-ball from php.net or your closest mirror and unpack it in a temporary directory.

Since we will compile PHP5 ourself we need first to make sure a number of libraries and the corresponding header files are installed in the system in order to be able to compile PHP5. This is done by installing a number of "*-devel.rpm" on your server. Depending your wanted configuration different development libraries must be made available.

At the very minimum you will need the "apache2-devel.rpm" which provides the "/sbin/apxs2" (Apache eXtenSion 2) command used to build modules with Apache2. Other modules you might need are

  • jpeg-devel.rpm
  • png-devel.rpm
  • mm-devel.rpm
  • xml2-devel.rpm
  • mysql-devel.rpm
Before you compile PHP5 you need to configure it by running the "./configure" command with the options you want to be included in PHP5.

We use a small shell script called "mkphp5" to avoid having to re-type all the options each time we compile a new version of PHP.

In our setup we have chosen to include, at build time, a number of the common modules. The configuration file we use for PHP5 compilation is therefore the following (You might want to change this to suit your specific setup):

#! /bin/sh
./configure --prefix=/usr/share --datadir=/usr/share/php \
--bindir=/usr/bin \
--with-apxs2=/usr/sbin/apxs2-prefork \
--libdir=/usr/share --includedir=/usr/include \
--bindir=/usr/bin \
--with-config-file-path=/etc/php5 \
--enable-mbstring \
--enable-mbregex \
--with-mysql  \
--with-gd --enable-gd-imgstrttf --enable-gd-native-ttf \
--with-zlib-dir=/usr/lib \
--with-png-dir=/usr/lib --with-jpeg-dir=/usr/lib \
--with-tiff-dir=/usr/lib --with-ttf-dir=/usr/lib \
--with-freetype-dir=/usr/lib \
--enable-ftp \
--enable-memory-limit \
--enable-safe-mode \
--enable-bcmath \
--enable-calendar \
--enable-ctype --with-ftp \
--with-bz2 \
--enable-inline-optimization \
--with-iconv
Please note a very important configuration here; we have set the default configuration directory (where the php.ini file is) to /etc/php5 this is only important if You plan to be able to easily switch between PHP4 and PHP5. The idea is that the existing /etc/php.ini file is used for plain PHP4 and that "/etc/php5/php.ini" is used for PHP5. By having these files in separate directories we can much more easily switch between PHP4 and PHP5 without having to rename/replace the php.ini configuration file.

When you run this you might get some errors saying that the configuration file cannot find some library. This is a sign that you might have the library installed but not yet have the "*-devel" RPM version added to your system which is needed since this is where all the usual header files needed for compilation would be.

So for example if you get an error like "Cannot find PNG libraries. Please check your that the corresponding "png-devel" library is installed and if not go back to Yast2 and install the needed "*-devel.rpm" versions of the libraries.

When You have been able to successfully run the ./configuration command it is time to compile. Type "make" as usual but do not type "make install", now wait until the compilation finishes.

Tip: If you are on a Pentium4 HT or on a real dual CPU machine you can speed up the compilation by instead giving the "make -j3" command which will start up to 3 concurrent compilation processes.

Step 4; Installing PHP5

Again; Do not run "make install" since this will try to modify the configuration files in a way that isn't SuSE 9.2 friendly.

The resulting PHP5 that you have built can be found in "./.libs/libphp5.so". We now only want to copy this file to the location of the other Apache2 modules.

Note: Again, PHP is only guaranteed to work with the non-threaded version of Apache2, which means that you should have installed the "apache2-prefork" MPM and NOT the "apache2-worker" MPM.

If you have correctly installed the prefork MPM several existing modules should now be installed in "usr/lib/apache2-prefork/". If you have correctly installed PHP4 you should see the "libphp4.so" module in this directory. Please make sure of this and then copy "libphp5.so" into this directory.

Trouble shooting: If you do not have a number of modules in the apache2-prefork directory check your apache2-worker directory. If you have a number of modules there then you need to un-install the apache2-worker MPM and re-install apache2-prefork.

Step 5; Setting up the correct PHP5 ini file

If you followed our recommendation above to put the configuration file in /etc/php5/php.ini the only thing you have to do now is to manually copy the default recommended php.ini file into this directory.
$> cp php.ini-recommended /etc/php5/php.ini
Now do any site specific adjustments to the ini file that You usually do.

Step 6; Enabling PHP5 as a module for Apache2

There is only one step left in order to get PHP5 up and running. telling Apache2 that we want this module to be loaded instead of PHP4.

This is done by going into the "/etc/sysconfig/apache2" file and change the string variable "APACHE_MODULES". In that string you will see "php4", now change this to "php5" and we are all done.

For example; in our system this variable looks like

APACHE_MODULES="suexec access actions alias auth \
auth_dbm autoindex cgi dir env expires include log_c \
onfig mime negotiation setenvif userdir ssl dav dav_svn php4"
and we change this to
APACHE_MODULES="suexec access actions alias auth auth_dbm \
autoindex cgi dir env expires include log_c \
onfig mime negotiation setenvif userdir ssl dav dav_svn php5"

Step 7; Restarting PHP5

Now switch to the user "root" and restart the Apache2 server. This is easiest done by typing
$> /etc/init.d/apache2 restart
Which will do the necessary restart. This script should now give You two "Done" if everything did go well.

Now try out the installation by going into you favorite browser and visit the same script we used previously to check PHP4, i.e. point your browser to "http://localhost/phpinfo.php"

If You followed the instructions above You should now see that You are running PHP5 version 5.x.y where the exact version number depends of course on what version of PHP5 You downloaded.

Trouble shooting: If your browser asks you to download content with mime-type "mime/x-httpd-application" when you try to visit the PHP script it means that Apache does not yet run PHP as a module. Make sure you have included the "php5" in the APACHE_MODULES string as described above.

 

Switching between PHP4 and PHP5

It would be possible to have two Apache processes running simultaneous and listening to different ports; each Apache server setup with different versions of PHP. We will not do this because all our links has to be augmented with the correct ports and it also gets more complicated to keep several simultaneous versions of configuration files for Apache2 in SuSE 9.2 since the config files are arranged to handle a single Apache2 process.

If You have followed the installation above You will only have to change the APACHE_MODULES string from "php4" to "php5" or vice-versa and then re-start the apache server as described above. This could be done manually or by a short shell script that edits the APACHE_MODULES string automatically.

After you edit the string you only have to re-start Apache and the change has taken place.

 

References
Apache2 Documentation
Main Documentation portal

Apache: The definitive guide
Online book from O'Reilly

Setting up parallel PH4/PHP5 using virtual hosts
An newer HowTo that shows how it is possible to run both PHP4 and PHP5 in parallel on the same server using Apache virtual hosts.

Shade
ShadeShadeShade

 

Valid (X)HTMLValid CSSDriven by PHPRSS Feed
Text & Images © Aditus Consulting 2000-2009