Run multiple PHP versions on the same server

Run-multiple-PHP-versions-on-the-same-server

You would have faced the trouble of keep swapping the PHP version depending on the task in hand you need to use. With this solution, you can install multiple PHP versions in the system and make them work simultaneously.

To run two or more PHP versions for different sites on the same LAMP stack by using mod_fcgid to create a .fcgi wrapper script.  The script forwards all the PHP requests which were made from php-cgi binary.

Considering a server running on PHP 7.2  where a specific site requires an older PHP version (eg PHP 5.6).

Required Installation Packages

The php56-php packages are in the Remi repos and they install to /opt/remi/

In order to create the PHP wrapper script, mod_fcgid Apache module needs to be installed.

[root@server ~]#yum install php56-php php56-php-pear mod_fcgid 

Remove alternate PHP configuration(s)

A configuration file to load PHP 5.6 would be created  into Apache at /etc/httpd/conf.d/php56-php.conf, which has to be DELETED else it would conflict with the existing PHP version and would fail Apache to start.

[root@server ~]#rm -f/etc/httpd/conf.d/php56-php.conf

PHP wrapper script creation

This script will set the environment for the specific PHP versions. (here PHP 5.6)

Example path of a specified site ‘/var/www/www.actsupport.com/cgi-bin/php56.fcgi’ (name the script depending on the PHP version).

[root@server ~]#cd/var/www/www/actsupport.com/cgi-bin
[root@server ~]#vim php56.fcgi
#!/bin/sh
PHPRC=/opt/remi/php56/root/etc/
export PHPRC
export PHP_FCGI_MAX_REQUESTS=5000
export PHP_FCGI_CHILDREN=16
exec /opt/remi/php56/root/usr/bin/php-cgi
[root@server ~]#chmod +x
/var/www/www.actsupport.com/cgi-bin/php56.fcgi

PHPRC is the path of the folder for php.ini and the exec is the full path to the php-cgi binary of the enabling PHP version, Update the values on your environment accordingly.

Update Apache Configuration

Append the Virtual Host for the specific domain on the question.

[root@server ~]#/etc/httpd/sites-available/actsupport.com.conf
(Generic/Custom path based on the environment)
<IfModule mod_fcgid.c>
RemoveHandler .php
RemoveType application/x-httpd-php
Action application/x-httpd-php5 /cgi-bin/php56.fcgi
AddType application/x-httpd-php5 .php
AddHandler application/x-httpd-php5 .php
</IfModule>

HTTPS config to be updated, if applicable
EDIT PHP config

Modify by commenting out the <FilesMatch> and SetHandler block because it can’t be overridden by the RemoveHandler directive we added to the site’s virtualhost entry.

It is being replaced with an AddHandler directive that performs the same purpose but will respect being overridden.

[root@server ~]#/etc/httpd/conf.d/php.conf
#   <FilesMatch \.php$>
#   SetHandler application/x-httpd-php
#   </FilesMatch>
AddHandler application/x-httpd-php .php

PHP Modules Installation

The required PHP modules vary based on the site and if we restart Apache now, the alternate PHP version would run without the necessary modules and be incompatible accordingly. Though very few modules being installed by default the below list works in many setups. However, install other PHP modules that are needed as necessary and make sure to install the phpXX-php-* versions from the Remi Repo for any PHP versions used to avoid accidental module installation for the wrong PHP versions of human error prefix.

php56-php-gd 
php56-php-mbstring 
php56-php-xml 
php56-php-mysqlnd 
php56-php-mssql 
php56-php-ioncube-loader

To install the above use

[root@server ~]#yum install php56-php-gd php56-php-mbstring php56-php-xml php56-php-mysqlnd php56-php-mssql php56-php-ioncube-loader

Also make sure the php.ini for the specific PHP version is probably located at /opt/remi/phpXX/root/etc/php.ini.

Finally restart Apache

Finally, you need to restart Apache

[root@server ~]#service httpd restart

Verify PHP changes

Create a info.php page and verify changes.

[root@server ~]#vim info.php
<?php
phpinfo:();
?>

Read our exciting blog Post: Steps to Install Jira on Centos7.

I hope you have resolved the issue related to multiple PHP versions. If you need any assistance Contact Us.

To get updates follow us on Facebook, Twitter, LinkedIn

Subscribe to get free blog content to your Inbox

Loading
Written by actsupp-r0cks