Intall multiple version of php on one server

This article is about how to install php-5.1, php-5.2 and php-5.3 on one server, and use them simultaneously

Based on CentOS 5.6, for Apache only

1. Enable rpmforge and epel yum repository

wget http://packages.sw.be/rpmforge-release/rpmforge-release-0.5.2-2.el5.rf.x86_64.rpm
wget http://download.fedora.redhat.com/pub/epel/5/x86_64/epel-release-5-4.noarch.rpm
sudo rpm -ivh rpmforge-release-0.5.2-2.el5.rf.x86_64.rpm
sudo rpm -ivh epel-release-5-4.noarch.rpm

2. Install php-5.1

CentOS/RHEL 5.x series have php-5.1 in box, simply install it with yum, eg:

sudo yum install php php-mysql php-mbstring php-mcrypt 

Compile and install php 5.2 and 5.3 from source

For php 5.2 and 5.3, we can find many rpm packages on the Internet. However, they all conflict with the php which comes with CentOS, so, we’d better build and install them from soure, this is not difficult, the point is to install php at different location.

However, when install php as an apache module, we can only use one version of php at the same time. If we need to run different version of php on the same server, at the same time, for example, different virtual host may need different version of php. Fortunately, the cool FastCGI and PHP-FPM can help.

Build and install php-5.2 with fastcgi enabled

1) Install required dev packages

yum install gcc libxml2-devel bzip2-devel zlib-devel \
	curl-devel libmcrypt-devel libjpeg-devel \
	libpng-devel gd-devel mysql-devel

2) Compile and install

wget http://cn.php.net/get/php-5.2.17.tar.bz2/from/this/mirror
tar -xjf php-5.2.17.tar.bz2
cd php-5.2.17
./configure --prefix=/usr/local/php52 \
	--with-config-file-path=/etc/php52 \
	--with-config-file-scan-dir=/etc/php52/php.d \
	--with-libdir=lib64 \
	--with-mysql \
	--with-mysqli \
	--enable-fastcgi \
	--enable-force-cgi-redirect \
	--enable-mbstring \
	--disable-debug \
	--disable-rpath \
	--with-bz2 \
	--with-curl \
	--with-gettext \
	--with-iconv \
	--with-openssl \
	--with-gd \
	--with-mcrypt \
	--with-pcre-regex \
	--with-zlib
make -j4 > /dev/null
sudo make install
sudo mkdir /etc/php52
sudo cp php.ini-recommended /etc/php52/php.ini

3) create a fastcgi wrapper script
create file /usr/local/php52/bin/fcgiwrapper.sh

#!/bin/bash
PHP_FCGI_MAX_REQUESTS=10000
export PHP_FCGI_MAX_REQUESTS
exec /usr/local/php52/bin/php-cgi
chmod a+x /usr/local/php52/bin/fcgiwrapper.sh

Build and install php-5.3 with fpm enabled

wget http://cn.php.net/get/php-5.3.6.tar.bz2/from/this/mirror
tar -xjf php-5.3.6.tar.bz2 
cd php-5.3.6
./configure --prefix=/usr/local/php53 \
	--with-config-file-path=/etc/php53 \
	--with-config-file-scan-dir=/etc/php53/php.d \
	--enable-fpm \
	--with-fpm-user=apache \
	--with-fpm-group=apache \
	--with-libdir=lib64 \
	--with-mysql \
	--with-mysqli \
	--enable-mbstring \
	--disable-debug \
	--disable-rpath \
	--with-bz2 \
	--with-curl \
	--with-gettext \
	--with-iconv \
	--with-openssl \
	--with-gd \
	--with-mcrypt \
	--with-pcre-regex \
	--with-zlib 

make -j4 && sudo make install
sudo mkdir /etc/php53
sudo cp php.ini-production /etc/php53/php.ini

sed -i -e 's#php_fpm_CONF=\${prefix}/etc/php-fpm.conf#php_fpm_CONF=/etc/php53/php-fpm.conf#' \
	sapi/fpm/init.d.php-fpm
sudo cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
sudo chmod a+x /etc/init.d/php-fpm
sudo /sbin/chkconfig --add php-fpm
sudo /sbin/chkconfig php-fpm on

sudo cp sapi/fpm/php-fpm.conf /etc/php53/

Configue php-fpm
Edit /etc/php53/php-fpm.conf, change some settings. This step is mainly to uncomment some settings, you can adjust the value if you like.

pid = run/php-fpm.pid
listen = 127.0.0.1:9000
pm.start_servers = 10
pm.min_spare_servers = 5
pm.max_spare_servers = 20

Then, start fpm

sudo /etc/init.d/php-fpm start

Install and setup mod_fastcgi, mod_fcgid

sudo yum install libtool httpd-devel apr-devel
wget http://www.fastcgi.com/dist/mod_fastcgi-current.tar.gz
tar -xzf mod_fastcgi-current.tar.gz
cd mod_fastcgi-2.4.6
cp Makefile.AP2 Makefile
sudo make top_dir=/usr/lib64/httpd/ install
sudo sh -c "echo 'LoadModule fastcgi_module modules/mod_fastcgi.so' > /etc/httpd/conf.d/mod_fastcgi.conf"
yum install mod_fcgid

Setup and test virtual hosts

1) Add the following line to /etc/hosts

127.0.0.1 web1.example.com web2.example.com web3.example.com

2) Create web document root and drop an index.php under it to show phpinfo
switch to user root, run

mkdir /var/www/fcgi-bin
for i in {1..3}; do
	web_root=/var/www/web$i
	mkdir $web_root
	echo "" > $web_root/index.php
done

Note: The empty /var/www/fcgi-bin directory is required, DO NOT REMOVE IT LATER

3) Create Apache config file(append to httpd.conf)


NameVirtualHost *:80

# module settings
# mod_fcgid
<IfModule mod_fcgid.c>
        idletimeout 3600
        processlifetime 7200
        maxprocesscount 17
        maxrequestsperprocess 16
        ipcconnecttimeout 60 
        ipccommtimeout 90
</IfModule>
# mod_fastcgi with php-fpm
<IfModule mod_fastcgi.c>
        FastCgiExternalServer /var/www/fcgi-bin/php-fpm -host 127.0.0.1:9000
</IfModule>


# virtual hosts...

#################################################################
#1st virtual host, use mod_php, run php-5.1
#################################################################
<VirtualHost *:80>
        ServerName web1.example.com
        DocumentRoot "/var/www/web1"

        <ifmodule mod_php5.c>
                <FilesMatch \.php$>
                        AddHandler php5-script .php
                </FilesMatch>
        </IfModule>

        <Directory "/var/www/web1">
                DirectoryIndex index.php index.html index.htm
                Options -Indexes FollowSymLinks
                Order allow,deny
                Allow from all
        </Directory>

</VirtualHost>
#################################################################
#2nd virtual host, use mod_fcgid, run php-5.2
#################################################################
<VirtualHost *:80>
        ServerName web2.example.com
        DocumentRoot "/var/www/web2"

        <IfModule mod_fcgid.c>
                AddHandler fcgid-script .php
                FCGIWrapper /usr/local/php52/bin/fcgiwrapper.sh
        </IfModule>

        <Directory "/var/www/web2">
                DirectoryIndex index.php index.html index.htm
                Options -Indexes FollowSymLinks +ExecCGI
                Order allow,deny
                Allow from all
        </Directory>

</VirtualHost>
#################################################################
#3rd virtual host, use mod_fastcgi + php-fpm, run php-5.3
#################################################################
<VirtualHost *:80>
        ServerName web3.example.com
        DocumentRoot "/var/www/web3"


        <IfModule mod_fastcgi.c>
                ScriptAlias /fcgi-bin/ /var/www/fcgi-bin/
                AddHandler php5-fastcgi .php
                Action php5-fastcgi /fcgi-bin/php-fpm
        </IfModule>

        <Directory "/var/www/web3">
                DirectoryIndex index.php index.html index.htm
                Options -Indexes FollowSymLinks +ExecCGI
                Order allow,deny
                Allow from all
        </Directory>

</VirtualHost>

4) restart apache. visit the 3 sites respectly to view phpinfo
and validate the result. ie:
http://web1.example.com
http://web2.example.com
http://web3.example.com
If all OK, you can use one of the 3 virtual host as template to create new virtual host, with the desired php version.

References:

This entry was posted in System Administration, Web and tagged , , , , , . Bookmark the permalink.

21 Responses to Intall multiple version of php on one server

  1. Pingback: [How to] - Multiple PHP version in Apache

  2. Markku says:

    This is the first ever on-hands article I found and it really saved my day, making it extremely easy to have both Apache+mod_php5 and Nginx+(possibly several versions of) PHP-FPM. Everything seems to work fine now and I am ready to move my server for production.

    My sincere thanks for sharing the simple steps.

  3. Jigar says:

    Hi,

    Is it possible to do something like in :

    httpd.conf

    LoadModule php53_module modules/mod_php53.so
    LoadModule php52_module modules/mod_php52.so

    AddType application/x-httpd-php53 .php53
    AddType application/x-httpd-php52 .php52

    Or we can add the AddType line in .htaccess

  4. Curu Wong says:

    I don’t know how apache module works and am not sure if loadind two different version of php module will cause symbol conflict.
    also, changing php file suffix may/may not suite your environment(you have to change each file name)
    You may give it a try and share with me, thanks!

  5. Jigar says:

    Hi,

    Okay thanks. Will surely share if i am able to run two php_module together.

    Now I am trying to look if I can change the module name php5_module to php53_module.

    For renaming “application/x-httpd-php” to “application/x-httpd-php53″ i will try following this blog http://digitalsandwich.com/archives/14-php5-and-php4-running-on-same-apache-server.html

  6. Bashadmin says:

    Hey i have installed 5.2 using yum and compiled and installed 5.4 under /opt/php54 can you please tell me what else is the step to configure php54 so that using can use it if they add some codes in .htaccess like below

    .htacces

    AddHandler application/x-httpd-php54 .php

  7. Curu Wong says:

    you may install php-5.4 as fastcgi or php-fpm, then tell Apache to use it with mod_fastcgi or mod_fcgid.

  8. There is phpfarm, which allows you to install multiple PHP versions in parallel. I wrote a tutorial about how to run use them all in apache.

  9. Pingback: Centos 6 downgrade to php 5,2

  10. Pingback: How to change repo to upgrade php

  11. Goran says:

    Hello,
    I am trying to setup 2 versions of PHP on Centos 5.9. I have installed default 5.4.19, and I was trying to setup another 5.2.17 PHP version to be run with Fast CGI and I followed the second part completely. However, when I try to run http://web2.example.com it returns 500 error message. In the apache log there are only 2 lines that repeat:
    [notice] mod_fcgid: call /var/www/web2/index.php with wrapper /usr/local/php52/bin/fcgiwrapper.sh
    and
    [notice] mod_fcgid: process /var/www/web2/index.php(25250) exit(server exited), terminated by calling exit(), return code: 255
    Please note that I had to add .php at the and of the FCGIWrapper because apache would not start without it:
    FCGIWrapper /usr/local/php52/bin/fcgiwrapper.sh .php
    Also please note that http://web1.example.com with PHP 5.4.19 is working absolutely fine.
    Please help. Thank you very much in advance.

  12. Curu Wong says:

    seems the wrapper failed to start php, have you insalled php 5.2 properly?

  13. Pingback: How to revert to previous version of PHP?

  14. dr.dimitru says:

    Hi!
    Thanks for tutorial.
    At the end when I’m trying to run:
    sudo /etc/init.d/php-fpm start
    I got this error:
    Starting php-fpm /etc/init.d/php-fpm: 58: /etc/init.d/php-fpm: /usr/local/sbin/php-fpm: Permission denied
    failed

    Any idea around this?

  15. Curu Wong says:

    your /usr/local/sbin/php-fpm is not executable ?

  16. dr.dimitru says:

    @Curu Yes after I make folder executable it just says “file not found”.
    Actually folder /usr/local/sbin/ is empty.
    I did search for find / -name php-fpm and has only one result: /etc/init.d/php-fpm
    As I understand now – I’m simply don’t have the bin file of php-fpm, or this issue is much deeper?
    I’m operating on Debian 6, I did all installation configurations exactly by this tutorial.

  17. dr.dimitru says:

    @Curu thank you for your help.
    Anyways I give up on php-fpm and just upgrade php5 version up to php5.5.6.
    Now upgrading my scripts.

  18. Liya says:

    The article indeed is very helpful. It saved my day :) :)

    All the very best

  19. Sagar Mallick says:

    Starting httpd: Syntax error on line 1033 of /etc/httpd/conf/httpd.conf:
    Invalid wrapper file extension

    ServerName web1.example.com
    DocumentRoot “/var/www/web1″

    AddHandler php5-script .php

    DirectoryIndex index.php index.html index.htm
    Options -Indexes FollowSymLinks
    Order allow,deny
    Allow from all

    ServerName web2.example.com
    DocumentRoot “/var/www/web2″

    AddHandler fcgid-script .php
    This line is 1033 FCGIWrapper /usr/local/php52/bin/fcgiwrapper.sh.php

    DirectoryIndex index.php index.html index.htm
    Options -Indexes FollowSymLinks +ExecCGI
    Order allow,deny
    Allow from all

    How can i solve this.

  20. somnath says:

    I am Facing a prob like,
    Starting httpd: Syntax error on line 1033 of /etc/httpd/conf/httpd.conf:
    Invalid wrapper file extension

    ServerName web1.example.com
    DocumentRoot “/var/www/web1″

    AddHandler php5-script .php

    DirectoryIndex index.php index.html index.htm
    Options -Indexes FollowSymLinks
    Order allow,deny
    Allow from all

    ServerName web2.example.com
    DocumentRoot “/var/www/web2″

    AddHandler fcgid-script .php
    This line is 1033 FCGIWrapper /usr/local/php52/bin/fcgiwrapper.sh

    DirectoryIndex index.php index.html index.htm
    Options -Indexes FollowSymLinks +ExecCGI
    Order allow,deny
    Allow from all

    How can i solve this? i am also try with the
    This line is 1033 FCGIWrapper /usr/local/php52/bin/fcgiwrapper.sh.php
    but getting same error please help me urgently.

  21. Curu Wong says:

    @somnath:

    FCGIWrapper /usr/local/php52/bin/fcgiwrapper.sh .php
    not
    FCGIWrapper /usr/local/php52/bin/fcgiwrapper.sh.php
    you miss a space between “.sh” and “.php”.

Leave a Reply