multiple php on apache centos

Harry Fairbanks picture Harry Fairbanks · Apr 28, 2014 · Viewed 13.4k times · Source

How can I get multiple php versions running on Centos 6.5 at the same time ?

Here's how.

Requirements
Centos 6.5 (Possible works with 6.6 and 7)
Apache Apache/2.2.15 (Possible works with other versions)

This guide installs and uses
FASTCGI (see the comments for alternative installation)
PHPFARM

This install was done this way so it would be compatable with iredmail.
You can install iredmail on a server with this set up.

Step 1
Installing PHPFarm

yum install gcc libxml2-devel openssl-devel bzip2-devel curl-devel libjpeg-devel     freetype-devel icu libicu-devel gcc-c++ postgresql-devel aspell-devel git -y

$ cd /opt/
$ git clone https://github.com/cweiske/phpfarm.git phpfarm
$ cd phpfarm/src/
$ cd /opt/phpfarm/src/

For each version of php you want run this. However, If you want custom modules such as mysql support skip this and see the part just below it.

$ ./compile.sh 5.3.1
$ ./compile.sh 5.3.3
$ ./compile.sh 5.5.11

If you get compile errors, reboot and try./compile.sh 5.3.1 again This worked for me when I ran into this problem

MYSQL & Module Support
Steps for getting MySQL support (and other modules) for custom phpfarm install of php version 5.5.14. These instructions work for any version just rename 5.5.14 to what ever like 5.4.3. You will of course need a mysql server to connect to to make use of the mysql module within php.
These steps need to be completed in this order
Step 1A
Ensure you have these paths and the date time is correct I did this as root. You should have the date.timezone you intend to use.

cd /opt/phpfarm/src
vi custom-php.ini

date.timezone=America/Halifax
include_path=".:/opt/phpfarm/inst/php-$version/pear/php/"

Step 1B
Ensure you have these paths and the date time is correct

cd /opt/phpfarm/src
vi default-custom-php.ini

date.timezone=America/Halifax
include_path=".:/opt/phpfarm/inst/php-$version/pear/php/"

Step 1C
Pay Special Attention to this line

--with-config-file-path=/opt/phpfarm/inst/php-5.5.11/lib/ \

it will need to be adjusted for the version you are working with. As root

vi custom-options-5.5.14.sh

#!/bin/bash

#gcov='--enable-gcov'
configoptions="
--disable-debug \
--with-config-file-path=/opt/phpfarm/inst/php-5.5.11/lib/ \
--enable-short-tags \
--with-pear \
--enable-bcmath \
--enable-calendar \
--enable-exif \
--enable-ftp \
--enable-mbstring \
--enable-pcntl \
--enable-soap \
--enable-sockets \
--enable-wddx \
--enable-zip \
--with-zlib \
--with-gettext \
--enable-pdo \
--with-pdo-mysql \
--enable-cgi \
--enable-json \
--with-curl \
--with-openssl \
--enable-openssl \
--with-mysql \
--enable-mysql \
$gcov"

Step 1D
Now compile as root. Phpfarm will automatically look for a file named custom-options-5.5.14.sh when you compile 5.5.14, or any other version with of course respective version numbers.

./compile.sh 5.5.14

Later when you check out your web page with the phpinfo(); function, you will see support for these modules and different "Configure Command" text on the page.


If these steps are not completed in order you may have to do it again. In order to do this again first remove the files from the src folder and the inst folder.

rm -rf /opt/phpfarm/inst/php-5.5.14

rm -rf /opt/phpfarm/src/php-5.5.14

The rm will remove the folder and the -rf stands for r recursive and f force. Ref: http://linuxcommand.org/lc3_man_pages/rm1.html


If you have errors check with this site. There are others but I found this one useful.

http://crybit.com/20-common-php-compilation-errors-and-fix-unix/


Reference for the mysql and module support

Issue activating a php extension using PHP Farm


Step 2
Adding phpfarm to your profiles

add this to the bottom of .bashrc for root and non-root user. The .bashrc file can be found in the users root folder or cd ~/ then ls -all and you should see it.

PATH="$PATH:/opt/phpfarm/inst/bin:/opt/phpfarm/inst/current-bin"

also execute this in terminal after you have added it to the .bashrc files for root and non-root user

export PATH="$PATH:/opt/phpfarm/inst/bin:/opt/phpfarm/inst/current-bin"

now exit the terminal and log back in. try this command

switch-phpfarm 5.5.11

you should be able to switch back and forth between diff php versions

[root@test joe]# switch-phpfarm 5.5.11
Setting active PHP version to 5.5.11
PHP 5.5.11 (cli) (built: May 17 2014 22:01:31) (DEBUG)
Copyright (c) 1997-2014 The PHP Group
Zend Engine v2.5.0, Copyright (c) 1998-2014 Zend Technologies
[root@test joe]#

add the repo for CentOS/RHEL 6, 64 Bit (x86_64):

cd /tmp
rpm -Uvh http://packages.sw.be/rpmforge-release/rpmforge-release-0.5.3-1.el6.rf.x86_64.rpm

Step 3
install the yums

yum install php php-cli mod_fastcgi

NOTE at this point if you try to run the switch-phpfarm 5.5.11 it wont work properly but thats ok. we will still be able to run multiple websites with different versions of php

Step 4
Ensure your cgi-bin is created and files are configured

$ cd  /var/www

If the cgi-bin isn’t already here create it

$ mkdir cgi-bin

For each version of php you intend to use make one of these files. Replace the ending with the version number

vi /var/www/cgi-bin/php.fastcgi.5.5.11

step 5
Inside the file php.fastcgi.5.5.11

#!/bin/bash
PHPRC="/opt/phpfarm/src/php-5.5.11/php.ini-development"
PHP_FCGI_CHILDREN=4
PHP_FCGI_MAX_REQUESTS=1000
export PHPRC
export PHP_FCGI_CHILDREN
export PHP_FCGI_MAX_REQUESTS
exec /opt/phpfarm/inst/bin/php-cgi-5.5.11

The first line

PHPRC="/opt/phpfarm/src/php-5.5.11/php.ini-development"

Tells you witch php.ini to use this is different in ubuntu. The last line

exec /opt/phpfarm/inst/bin/php-cgi-5.5.11

also needs to be changed for each particular version. I am not sure what it does but I do know it needs to be changed.

Step 6
Enable fastcgi files to be executable for apache:apache user and group

Example 1

[root@test joe]# chown apache:apache /var/www/cgi-bin/php.fastcgi.5.5.11
[root@test joe]# chmod +x /var/www/cgi-bin/php.fastcgi.5.5.11

Example 2

[root@test joe]# chown apache:apache /var/www/cgi-bin/php.fastcgi.5.3.3
[root@test joe]# chmod +x /var/www/cgi-bin/php.fastcgi.5.3.3

Example 3

[root@test joe]# chown apache:apache /var/www/cgi-bin/php.fastcgi.5.3.1
[root@test joe]# chmod +x /var/www/cgi-bin/php.fastcgi.5.3.1

Step 7
Editing the httpd.conf file
Here’s what you need for the /etc/httpd/conf/httpd.conf file. First find “NameVirtualHost *:80” and use this as a starting point.The “#” means the line is commented out. Uncomment this line by deleting the #. it Should now look like this.

NameVirtualHost *:80

This will allow multiple virtual host to operate on Apache by their "ServerName" in each virtual host reference. I left some lines commented to show you what you can do without. Make sure the bottom of the /etc/httpd/conf/httpd.conf looks like this.

<VirtualHost *:80>
    ServerName test1.com
   #ServerAdmin [email protected]
    DocumentRoot /var/www/html/test1
    ScriptAlias /cgi-bin/ "/var/www/cgi-bin/"
    <Directory "/var/www/html/test1">
            Options +Indexes FollowSymLinks +ExecCGI
            AddHandler php5-fastcgi .php
            Action php5-fastcgi /cgi-bin/php.fastcgi.5.5.11
            AllowOverride All
            Order allow,deny
            Allow from All
    </Directory>
</VirtualHost>
<VirtualHost *:80>
    ServerName test2.com
#   ServerAdmin [email protected]
    DocumentRoot /var/www/html/test2
    ScriptAlias /cgi-bin/ "/var/www/cgi-bin/"
    <Directory "/var/www/html/test2">
            Options +Indexes FollowSymLinks +ExecCGI
            AddHandler php5-fastcgi .php
            Action php5-fastcgi /cgi-bin/php.fastcgi.5.3.3
            AllowOverride All
            Order allow,deny
            Allow from All
    </Directory>
</VirtualHost>
<VirtualHost *:80>
    ServerName test3.org
#   ServerAdmin [email protected]
    DocumentRoot /var/www/html/test3
    ScriptAlias /cgi-bin/ "/var/www/cgi-bin/"
    <Directory "/var/www/html/test3">
            Options +Indexes FollowSymLinks +ExecCGI
            AddHandler php5-fastcgi .php
            Action php5-fastcgi /cgi-bin/php.fastcgi.5.3.1
            AllowOverride All
            Order allow,deny
            Allow from All
    </Directory>
</VirtualHost>
<VirtualHost *:80>
    ServerName test4.net
#   ServerAdmin [email protected]
    DocumentRoot /var/www/html/test6
</VirtualHost>

Step 8
Editing the etc/hosts file
here is what u need in the etc/hosts file.

127.0.0.1   localhost test1.com test2.com test3.org test4.net
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6

Step 9

Now restart your server

service httpd restart

Step 10
Testing
if you add phpinfo() to each of the index.php sites you will notice that all the php versions will be different. also the last one test4.net will go with the default for centos. Also note that test3.org and test4.net are not .com's but will still work. In the future I plan to make a guide for the phpfarm files so you can install with mysql support. This was another issue I ran into. If you notice some errors in this or have difficult making it work, please comment and I will get to it as soon as I can.

Answer

Plamen Vasilev picture Plamen Vasilev · Feb 22, 2015

Several notices from me:

  1. yum install php php-cli mod_fastcgi is not correct. mod_php should not be enabled. also mod_fastcgi can be installed without external repository using mod_fcgid
  2. For PHP 5.2 and libxml>=1.2.9 you have to apply this patch libxml29_compat.patch
  3. to set php version for all host without mod_php enabled add this to httpd.conf

    <Directory "/var/www">
        Options All +ExecCGI
        AddHandler php-cgi .php
        Action php-cgi /cgi-bin/php.fastcgi.5.5.18
    </Directory>
    
  4. when compiling on 64bit systems the compilation fails because of lib64 folder so you should add --with-libdir=lib64 to configoptions