How to setup Multiple PHP versions on Apache using FastCGI

To setup Multiple PHP versions on Apache using FastCGI

PHP is a server scripting language used for the development of software applications with the help of internet. It is a powerful tool to create dynamic contents for interaction with databases. Contrary to popular beliefs, it is really easy to set up several versions of PHP on Apache, and this feat can be achieved by using the FastCGI script.  In this article, you will get accustomed to the installation of php 5.6 in one domain and php 7.1 on Apache web server. 

 

Requirements

-Apache Webserver

 

Installing Multiple PHP

Before you begin with the process, you should install the epel release.

[root@linuxhelp ~]# yum install epel-release
Loaded plugins: fastestmirror, refresh-packagekit, security
Setting up Install Process
Determining fastest mirrors
* base: mirror.dhakacom.com
* extras: mirrors.nhanhoa.com
* updates: mirror.dhakacom.com
base | 3.7 kB 00:00
extras | 3.4 kB 00:00
extras/primary_db | 29 kB 00:00
updates | 3.4 kB 00:00
updates/primary_db | 4.7 MB 00:00
Resolving Dependencies
.
.
.
.
Installed:

epel-release.noarch 0:6-8


Complete!

 

 

Once it is done, you should download the installation package, which can be done with the help of the wget command.

[root@linuxhelp ~]# wget http://ftp.tu-chemnitz.de/pub/linux/dag/redhat/el6/en/x86_64/rpmforge/RPMS/mod_fastcgi-2.4.6-2.el6.rf.x86_64.rpm
--2017-11-11 16:52:50-- http://ftp.tu-chemnitz.de/pub/linux/dag/redhat/el6/en/x86_64/rpmforge/RPMS/mod_fastcgi-2.4.6-2.el6.rf.x86_64.rpm
Resolving ftp.tu-chemnitz.de... 134.109.228.1, 2001:638:911:b0e:134:109:228:1
Connecting to ftp.tu-chemnitz.de|134.109.228.1|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 119000 (116K) [application/octet-stream]
Saving to: “mod_fastcgi-2.4.6-2.el6.rf.x86_64.rpm”

100%[====================================================================================================================================================================>] 119,000 --.-K/s in 0.001s

2017-11-11 16:52:55 (78.5 MB/s) - “mod_fastcgi-2.4.6-2.el6.rf.x86_64.rpm” saved [119000/119000]

 

After that, you should install it.

[root@linuxhelp ~]# rpm -Uvh mod_fastcgi-2.4.6-2.el6.rf.x86_64.rpm
warning: mod_fastcgi-2.4.6-2.el6.rf.x86_64.rpm: Header V3 DSA/SHA1 Signature, key ID 6b8d79e6: NOKEY
Preparing... ########################################### [100%]
1:mod_fastcgi ########################################### [100%]

 

And then, make sure you install all your required packages as follows. It is essential to have them installed before starting the installation of PHP.

[root@linuxhelp ~]# yum install gcc libxml2-devel libXpm-devel gmp-devel libicu-devel t1lib-devel aspell-devel openssl-devel bzip2-devel libcurl-devel libjpeg-devel libvpx-devel libpng-devel freetype-devel readline-devel libtidy-devel libxslt-devel libmcrypt-devel pcre-devel curl-devel mysql-devel ncurses-devel gettext-devel net-snmp-devel libevent-devel libtool-ltdl-devel libc-client-devel postgresql-devel bison gcc make -y

Loaded plugins: fastestmirror, refresh-packagekit, security
Setting up Install Process
Loading mirror speeds from cached hostfile
* base: mirror.dhakacom.com
* epel: epel.scopesky.iq
* extras: mirrors.nhanhoa.com
* updates: mirror.dhakacom.com
Package 1:make-3.81-23.el6.x86_64 already installed and latest version
Resolving Dependencies
--> Running transaction check
---> Package aspell-devel.x86_64 12:0.60.6-12.el6 will be installed
--> Processing Dependency: aspell = 12:0.60.6-12.el6 for package: 12:aspell-devel-0.60.6-12.el6.x86_64
.
.
.
Dependency Installed:
aspell.x86_64 12:0.60.6-12.el6 autoconf.noarch 0:2.63-5.1.el6 automake.noarch 0:1.11.1-4.el6 cloog-ppl.x86_64 0:0.15.7-1.2.el6
cpp.x86_64 0:4.4.7-18.el6 elfutils-devel.x86_64 0:0.164-2.el6 elfutils-libelf-devel.x86_64 0:0.164-2.el6 file-devel.x86_64 0:5.04-30.el6
gettext-libs.x86_64 0:0.17-18.el6 keyutils-libs-devel.x86_64 0:1.4-5.el6 krb5-devel.x86_64 0:1.10.3-65.el6 libX11-devel.x86_64 0:1.6.4-3.el6
libXau-devel.x86_64 0:1.0.6-4.el6 libXpm.x86_64 0:3.5.10-2.el6 libc-client.x86_64 0:2007e-11.el6 libcom_err-devel.x86_64 0:1.41.12-23.el6
libevent-doc.noarch 0:1.4.13-4.el6 libevent-headers.noarch 0:1.4.13-4.el6 libgcj.x86_64 0:4.4.7-18.el6 libgcrypt-devel.x86_64 0:1.4.5-12.el6_8
libgpg-error-devel.x86_64 0:1.7-4.el6 libidn-devel.x86_64 0:1.18-2.el6 libmcrypt.x86_64 0:2.5.8-9.el6 libselinux-devel.x86_64 0:2.0.94-7.el6
libsepol-devel.x86_64 0:2.0.41-4.el6 libtidy.x86_64 0:0.99.0-19.20070615.1.el6 libxcb-devel.x86_64 0:1.12-4.el6 lm_sensors-devel.x86_64 0:3.1.1-17.el6
mpfr.x86_64 0:2.4.1-6.el6 mysql.x86_64 0:5.1.73-8.el6_8 popt-devel.x86_64 0:1.13-7.el6 postgresql.x86_64 0:8.4.20-8.el6_9
postgresql-libs.x86_64 0:8.4.20-8.el6_9 ppl.x86_64 0:0.10.2-11.el6 rpm-devel.x86_64 0:4.8.0-55.el6 t1lib.x86_64 0:5.1.2-6.el6_2.1
tcp_wrappers-devel.x86_64 0:7.6-58.el6 xorg-x11-proto-devel.noarch 0:7.7-14.el6 zlib-devel.x86_64 0:1.2.3-29.el6

Complete!

 

Once it is done, you shall install PHP from source and for that you should use wget command. First, let us install PHP 5.6

[root@linuxhelp ~]# wget http://in1.php.net/distributions/php-5.6.32.tar.gz
--2017-11-11 17:09:13-- http://in1.php.net/distributions/php-5.6.32.tar.gz
Resolving in1.php.net... 103.21.58.218
Connecting to in1.php.net|103.21.58.218|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 19294956 (18M) [application/x-gzip]
Saving to: “php-5.6.32.tar.gz”

100%[===================================================================================================================================================================>] 19,294,956 216K/s in 49s

2017-11-11 17:10:04 (382 KB/s) - “php-5.6.32.tar.gz” saved [19294956/19294956]
[root@linuxhelp ~]# tar -zxf php-5.6.32.tar.gz

 

After it is done, you shall move inside the extracted directory by running the following command.

[root@linuxhelp ~]# cd php-5.6.32
In that directory, you should build your configuration with the help of the following command.
[root@linuxhelp php-5.6.32]# ./buildconf --force
Forcing buildconf
Removing configure caches
buildconf: checking installation...
buildconf: autoconf version 2.63 (ok)

 

You should run your configure for all your PHP extensions.

[root@linuxhelp php-5.6.32]# ./configure --prefix=/opt/php-5.6 --with-pdo-pgsql --with-zlib-dir --with-freetype-dir --enable-mbstring --with-libxml-dir=/usr --enable-soap --enable-calendar --with-curl --with-mcrypt --with-zlib --with-gd --with-pgsql --disable-rpath --enable-inline-optimization --with-bz2 --with-zlib --enable-sockets --enable-sysvsem --enable-sysvshm --enable-pcntl --enable-mbregex --with-mhash --enable-zip --with-pcre-regex --with-mysql --with-pdo-mysql --with-mysqli --with-png-dir=/usr --enable-gd-native-ttf --with-openssl --with-fpm-user=apache --with-fpm-group=apache --with-libdir=lib64 --enable-ftp --with-imap --with-imap-ssl --with-kerberos --with-gettext --with-gd --with-jpeg-dir=/usr/lib/ --enable-fpm
checking for grep that handles long lines and -e... /bin/grep
checking for egrep... /bin/grep -E
checking for a sed that does not truncate output... /bin/sed
checking build system type... x86_64-unknown-linux-gnu
checking host system type... x86_64-unknown-linux-gnu
checking target system type... x86_64-unknown-linux-gnu
checking for cc... cc.
.
.
.
.
config.status: creating sapi/fpm/init.d.php-fpm
config.status: creating sapi/fpm/php-fpm.service
config.status: creating sapi/fpm/php-fpm.8
config.status: creating sapi/fpm/status.html
config.status: creating sapi/cgi/php-cgi.1
config.status: creating ext/phar/phar.1

config.status: creating ext/phar/phar.phar.1

config.status: creating main/php_config.h

config.status: executing default commands

 

 

Once it is done, you shall run the make and make install commands to build the installation process.

[root@linuxhelp php-5.6.32]# make
/bin/sh /root/php-5.6.32/libtool --silent --preserve-dup-deps --mode=compile cc -Iext/opcache/ -I/root/php-5.6.32/ext/opcache/ -DPHP_ATOM_INC -I/root/php-5.6.32/include -I/root/php-5.6.32/main -I/root/php-5.6.32 -I/root/php-5.6.32/ext/date/lib -I/root/php-5.6.32/ext/ereg/regex -I/usr/include/libxml2 -I/usr/include/freetype2 -I/usr/include/imap -I/root/php-5.6.32/ext/mbstring/oniguruma -I/root/php-5.6.32/ext/mbstring/libmbfl -I/root/php-5.6.32/ext/mbstring/libmbfl/mbfl -I/root/php-5.6.32/ext/sqlite3/libsqlite -I/root/php-5.6.32/ext/zip/lib -I/root/php-5.6.32/TSRM -I/root/php-5.6.32/Zend -I/usr/include -g -O2 -fvisibility=hidden -c /root/php-5.6.32/ext/opcache/ZendAccelerator.c -o ext/opcache/ZendAccelerator.lo
/root/php-5.6.32/ext/opcache/ZendAccelerator.c: In function ‘accel_use_shm_interned_strings’:
/root/php-5.6.32/ext/opcache/ZendAccelerator.c:397: warning: assignment discards qualifiers from pointer target type

.
.
.
.
.
Generating phar.php
Generating phar.phar
PEAR package PHP_Archive not installed: generated phar will require PHP's phar extension be enabled.
directorygraphiterator.inc
invertedregexiterator.inc
clicommand.inc
pharcommand.inc
directorytreeiterator.inc
phar.inc

Build complete.

 

[root@linuxhelp php-5.6.32]# make install
Installing shared extensions: /opt/php-5.6/lib/php/extensions/no-debug-non-zts-20131226/
Installing PHP CLI binary: /opt/php-5.6/bin/
Installing PHP CLI man page: /opt/php-5.6/php/man/man1/
Installing PHP FPM binary: /opt/php-5.6/sbin/
Installing PHP FPM config: /opt/php-5.6/etc/
Installing PHP FPM man page: /opt/php-5.6/php/man/man8/
Installing PHP FPM status page: /opt/php-5.6/php/php/fpm/
Installing PHP CGI binary: /opt/php-5.6/bin/
Installing PHP CGI man page: /opt/php-5.6/php/man/man1/
Installing build environment: /opt/php-5.6/lib/php/build/
Installing header files: /opt/php-5.6/include/php/
Installing helper programs: /opt/php-5.6/bin/
.
.
.
.
[PEAR] XML_Util - installed: 1.4.2
[PEAR] PEAR - installed: 1.10.5
Wrote PEAR system config file at: /opt/php-5.6/etc/pear.conf
You may want to add: /opt/php-5.6/lib/php to your php.ini include_path
/root/php-5.6.32/build/shtool install -c ext/phar/phar.phar /opt/php-5.6/bin
ln -s -f phar.phar /opt/php-5.6/bin/phar
Installing PDO headers: /opt/php-5.6/include/php/ext/pdo/

 

Once it is done, you should copy the php.ini file from source location towards /opt/php-5.6/lib/ as follows.

[root@linuxhelp php-5.6.32]# cp /root/php-5.6.32/php.ini-production /opt/php-5.6/lib/php.ini

[root@linuxhelp php-5.6.32]# /opt/php-5.6/bin/php --ini
Configuration File (php.ini) Path: /opt/php-5.6/lib
Loaded Configuration File: /opt/php-5.6/lib/php.ini
Scan for additional .ini files in: (none)
Additional .ini files parsed: (none)

 

And then, you should create FastCGI script in the following location.

[root@linuxhelp ~]# vim /var/www/cgi-bin/php56.fcgi
#!/bin/bash
PHPRC="/opt/php-5.6/lib/php.ini"
PHP_CGI=/opt/php-5.6/bin/php-cgi
PHP_FCGI_CHILDREN=8
PHP_FCGI_MAX_REQUESTS=3000
export PHPRC
export PHP_FCGI_CHILDREN
export PHP_FCGI_MAX_REQUESTS
exec $PHP_CGI

 

 

Once it is done, you should provide appropriate permission for your fastcgi location. You can do that by running the following commands.

[root@linuxhelp ~]# chown apache:apache /var/www/cgi-bin/php56.fcgi
[root@linuxhelp ~]# chmod +x /var/www/cgi-bin/php56.fcgi

Now, we are done with the installation of PHP 5.6
 

Installing PHP 7

First, download the PHP source package using wget command.

[root@linuxhelp ~]# wget http://in1.php.net/distributions/php-7.1.11.tar.gz
--2017-11-11 17:09:26-- http://in1.php.net/distributions/php-7.1.11.tar.gz
Resolving in1.php.net... 103.21.58.218
Connecting to in1.php.net|103.21.58.218|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 19450481 (19M) [application/x-gzip]
Saving to: “php-7.1.11.tar.gz”

100%[===================================================================================================================================================================>] 19,450,481 769K/s in 69s

2017-11-11 17:10:36 (276 KB/s) - “php-7.1.11.tar.gz” saved [19450481/19450481]

 

Once it is done, you ought to extract the downloaded package as follows.

[root@linuxhelp ~]# tar -zxf php-7.1.11.tar.gz

[root@linuxhelp ~]# cd php-7.1.11
[root@linuxhelp php-7.1.11]# ./buildconf --force
Forcing buildconf
Removing configure caches
buildconf: checking installation...
buildconf: autoconf version 2.63 (ok)
rebuilding main/php_config.h.in

 

Next, you should run the following command.

[root@linuxhelp php-7.1.11]# ./configure --prefix=/opt/php-7.1 --with-pdo-pgsql --with-zlib-dir --with-freetype-dir --enable-mbstring --with-libxml-dir=/usr --enable-soap --enable-calendar --with-curl --with-mcrypt --with-zlib --with-gd --with-pgsql --disable-rpath --enable-inline-optimization --with-bz2 --with-zlib --enable-sockets --enable-sysvsem --enable-sysvshm --enable-pcntl --enable-mbregex --with-mhash --enable-zip --with-pcre-regex --with-mysqli --with-pdo-mysql --with-mysqli --with-png-dir=/usr --enable-gd-native-ttf --with-openssl --with-fpm-user=apache --with-fpm-group=apache --with-libdir=lib64 --enable-ftp --with-imap --with-imap-ssl --with-kerberos --with-gettext --with-gd --with-jpeg-dir=/usr/lib/ --enable-fpm
checking for grep that handles long lines and -e... /bin/grep
checking for egrep... /bin/grep -E
checking for a sed that does not truncate output... /bin/sed
checking build system type... x86_64-unknown-linux-gnu
checking host system type... x86_64-unknown-linux-gnu
checking target system type... x86_64-unknown-linux-gnu
.
.
.
.
config.status: creating sapi/fpm/status.html
config.status: creating sapi/cgi/php-cgi.1
config.status: creating ext/phar/phar.1
config.status: creating ext/phar/phar.phar.1
config.status: creating main/php_config.h
config.status: executing default commands

 

And then, let us compile it by running make and make install commands.

[root@linuxhelp php-7.1.11]# make
/bin/sh /root/php-7.1.11/libtool --silent --preserve-dup-deps --mode=compile cc -DZEND_ENABLE_STATIC_TSRMLS_CACHE=1 -Iext/opcache/ -I/root/php-7.1.11/ext/opcache/ -DPHP_ATOM_INC -I/root/php-7.1.11/include -I/root/php-7.1.11/main -I/root/php-7.1.11 -I/root/php-7.1.11/ext/date/lib -I/usr/include/libxml2 -I/usr/include/freetype2 -I/usr/include/imap -I/root/php-7.1.11/ext/mbstring/oniguruma -I/root/php-7.1.11/ext/mbstring/libmbfl -I/root/php-7.1.11/ext/mbstring/libmbfl/mbfl -I/root/php-7.1.11/ext/sqlite3/libsqlite -I/root/php-7.1.11/ext/zip/lib -I/root/php-7.1.11/TSRM -I/root/php-7.1.11/Zend -I/usr/include -g -O2 -fvisibility=hidden -DZEND_SIGNALS -c /root/php-7.1.11/ext/opcache/ZendAccelerator.c -o ext/opcache/ZendAccelerator.lo
/bin/sh /root/php-7.1.11/libtool --silent --preserve-dup-deps --mode=compile cc -DZEND_ENABLE_STATIC_TSRMLS_CACHE=1 -Iext/opcache/ -I/root/php-7.1.11/ext/opcache/ -DPHP_ATOM_INC -I/root/php-7.1.11/include -I/root/php-7.1.11/main -I/root/php-7.1.11 -I/root/php-7.1.11/ext/date/lib -I/usr/include/libxml2 -I/usr/include/freetype2 -I/usr/include/imap -I/root/php-7.1.11/ext/mbstring/oniguruma -I/root/php-7.1.11/ext/mbstring/libmbfl -I/root/php-7.1.11/ext/mbstring/libmbfl/mbfl -I/root/php-7.1.11/ext/sqlite3/libsqlite -I/root/php-7.1.11/ext/zip/lib -I/root/php-7.1.11/TSRM -I/root/php-7.1.11/Zend -I/usr/include –g
.
.
.
.
Generating phar.php
Generating phar.phar
PEAR package PHP_Archive not installed: generated phar will require PHP's phar extension be enabled.
directorygraphiterator.inc
invertedregexiterator.inc
clicommand.inc
pharcommand.inc
directorytreeiterator.inc
phar.inc

Build complete.

 

 

[root@linuxhelp php-7.1.11]# make install
Installing shared extensions: /opt/php-7.1/lib/php/extensions/no-debug-non-zts-20160303/
Installing PHP CLI binary: /opt/php-7.1/bin/
Installing PHP CLI man page: /opt/php-7.1/php/man/man1/
Installing PHP FPM binary: /opt/php-7.1/sbin/
Installing PHP FPM defconfig: /opt/php-7.1/etc/
Installing PHP FPM man page: /opt/php-7.1/php/man/man8/
Installing PHP FPM status page: /opt/php-7.1/php/php/fpm/
Installing phpdbg binary: /opt/php-7.1/bin/
Installing phpdbg man page: /opt/php-7.1/php/man/man1/
Installing PHP CGI binary: /opt/php-7.1/bin/
Installing PHP CGI man page: /opt/php-7.1/php/man/man1/
Installing build environment: /opt/php-7.1/lib/php/build/
Installing header files: /opt/php-7.1/include/php/
Installing helper programs: /opt/php-7.1/bin/
program: phpize
program: php-config
Installing man pages: /opt/php-7.1/php/man/man1/
page: phpize.1
page: php-config.1
Installing PEAR environment: /opt/php-7.1/lib/php/
[PEAR] Archive_Tar - already installed: 1.4.3
[PEAR] Console_Getopt - already installed: 1.4.1
[PEAR] Structures_Graph- already installed: 1.1.1
[PEAR] XML_Util - already installed: 1.4.2
[PEAR] PEAR - already installed: 1.10.5
Wrote PEAR system config file at: /opt/php-7.1/etc/pear.conf
You may want to add: /opt/php-7.1/lib/php to your php.ini include_path
/root/php-7.1.11/build/shtool install -c ext/phar/phar.phar /opt/php-7.1/bin
ln -s -f phar.phar /opt/php-7.1/bin/phar
Installing PDO headers: /opt/php-7.1/include/php/ext/pdo/

 

Once it is done, you should copy the php.ini file towards your php location with the help of the following command.

[root@linuxhelp php-7.1.11]# cp /root/php-7.1.11/php.ini-production /opt/php-7.1/lib/php.ini
[root@linuxhelp php-7.1.11]# /opt/php-7.1/bin/php --ini
Configuration File (php.ini) Path: /opt/php-7.1/lib
Loaded Configuration File: /opt/php-7.1/lib/php.ini
Scan for additional .ini files in: (none)
Additional .ini files parsed: (none)

 

And then, create a file and enter fastcgi script inside it as follows.

[root@linuxhelp ~]# cd /var/www/cgi-bin/
[root@linuxhelp cgi-bin]# vim php71.fcgi
#!/bin/bash
PHPRC="/opt/php-7.1/lib/php.ini"
PHP_CGI2=/opt/php-7.1/bin/php-cgi
PHP_FCGI_CHILDREN=8
PHP_FCGI_MAX_REQUESTS=3000
export PHPRC
export PHP_FCGI_CHILDREN
export PHP_FCGI_MAX_REQUESTS
exec $PHP_CGI2

 

Later you should set the owner permission for it, and that can be done by running the following command.

[root@linuxhelp cgi-bin]# chown apache:apache /var/www/cgi-bin/php71.fcgi
[root@linuxhelp cgi-bin]# chmod +x /var/www/cgi-bin/php71.fcgi

 

Once it is done, you should set the domains inside your host file.

[root@linuxhelp ~]# vim /etc/hosts
192.168.7.226 test.php56.com
192.168.7.226 test.php71.com

 

Now, let us configure the Apache VirtualHost file. Run the following command to open the .conf file. 

[root@linuxhelp ~]# vim /etc/httpd/conf/httpd.conf

 

And add the following lines to it. You should make two configurations, one for PHP 5.6 and one for PHP 7.1

<VirtualHost *:80>
ServerName test.php56.com
DocumentRoot /var/www/html/
ErrorLog /var/www/html/php/error.log
ScriptAlias /cgi-bin/ "/var/www/cgi-bin/"
<Directory /var/www/html/ >
Options +Indexes FollowSymLinks +ExecCGI
AddHandler php5-fastcgi .php
Action php5-fastcgi /cgi-bin/php56.fcgi
AllowOverride All
Order allow,deny
Allow from All
</Directory>
</VirtualHost>

 

<VirtualHost *:80>
ServerName test.php71.com
DocumentRoot /var/www/html/
ErrorLog /var/www/html/php/error71.log
ScriptAlias /cgi-bin/ "/var/www/cgi-bin/"
<Directory /var/www/html/ >
Options +Indexes FollowSymLinks +ExecCGI
AddHandler php71-fastcgi .php
Action php71-fastcgi /cgi-bin/php71.fcgi
AllowOverride All
Order allow,deny
Allow from All
</Directory>
</VirtualHost>

 

Finally, restart your Apache service by running the following command.

[root@linuxhelp ~]# service httpd restart

 

The process doesn't end with this, so let us switch over to the browser and give http://test.php56.com as URL. You can check all the information. 

PHP Version

 

Also, check the configuration of PHP 5.6

Configure

 

Now let us check for PHP 7.1, so again on your browser, give http://test.php71.com as your URL. 

version 7.1

phpinfo

With this, the installation of Multiple PHP comes to an end.

 

 


Thank you! for using Linux Help.

You find this tutorial helpful? Share with your friends to keep it alive.
For more help topics browse our website www.linuxhelp.com
Be the first to comment, we value your suggestions. For further queries please comment below.

Tags: Apache Php
user image Author :  Jackson