Running Multiple PHP version on the same server

Share with friends

Running Multiple PHP versions on the same server is not always required but some time when you want to upgrade your old system to a newer version. then Running Multiple PHP version is mandatory. Today I am going to tell you how you can run multiple PHP versions on the same server.

I am using centos 7 and Apache2 for this article.

How It Work

when the PHP file is run on apache serve then Apache will call “SetHandler application/x-httpd-php” which will decide which installed module needs to call to run a PHP script. which can be defined under the apache configuration file (httpd.conf) generally can found under “etc/httpd/conf/ “ directory.

This is the standard method to call PHP file when you have only one php installed on the server but when you try to run multiple version of PHP on the same server then we need some magical module called “PHP-FPM”

What is PHP-FPM

FPM is an alternative to normal php FastCGI with some additional features. to know more about PHP-FPM you can check php official website

Follow the Given step to install multiple versions of php. I am installing php 5.6 (oldest php version that which can be installed easily in centos 7 you might install php5.3 but I have not tested yet for php5.3) and another version is the latest php 7.4.

Step 1 Install required packages

Run given Command to install each package.

Apache

yum install httpd

Extra Packages for Enterprise Linux(EPEL)

yum install epel-release

Yum utilities

yum install yum-utils

Finally Update yum

yum update

Step 2 Install PHP multiple versions

first of all install remi-repository from where we will install php-fpm.

Remi-repository

yum install http://rpms.remirepo.net/enterprise/remi-release-7.rpm

PHP5.6 and PHP-FPM5.6

yum install php56
yum install php56-php-fpm

PHP7.4 and PHP-FPM7.4

yum install php74
yum install php74-php-fpm

Step 3 Stop both PHP-FPM

Stop php-fpm buy pasting following command on terminal.

systemctl stop php56-php-fpm
systemctl stop php74-php-fpm

step 4 Configure PHP-FPM

By default, apache will listen to port 9000 for php.when we have multiple versions of php we need to define a unique port to listen to each php version. you can manually change the port number by opening the www.conf file.

php 5.6

For manually change open /etc/opt/remi/php56/php-fpm.d/www.conf and replace the 9000 with 9056(whatever port you want). If you want to change from the command copy and paste given line in your terminal.

sed -i 's/:9000/:9056/' /etc/opt/remi/php56/php-fpm.d/www.conf

php 7.4

For manual open /etc/opt/remi/php74/php-fpm.d/www.conf and replace the 9000 with 9074 (whatever port you want). If you want to change from the command copy and paste given line in your terminal.

sed -i 's/:9000/:9074/' /etc/opt/remi/php74/php-fpm.d/www.conf

Run two php-fpms

Copy and paste the given command on terminal

systemctl start php74-php-fpm
systemctl start php56-php-fpm

Step 5 Configure SELinux

When you want to use a new port then you need to add it to SELINUX. To add the newly used port on SELINUX run following command.

semanage port -a -t http_port_t -p tcp 9074
semanage port -a -t http_port_t -p tcp 9056

Step 6 Make Script Wrapper

we need to make script wrapper to call both versions of php on the basis of users request to create script wrapper run following command

php5.6

cat > /var/www/cgi-bin/php56.fcgi << EOF
#!/bin/bash
exec /bin/php56-cgi
EOF

php7.4

cat > /var/www/cgi-bin/php74.fcgi << EOF
#!/bin/bash
exec /bin/php74-cgi
EOF

make script wrapper exicutable

Make recently created two file exicutalbe by running following command.

sudo chmod 755 /var/www/cgi-bin/php56.fcgi
sudo chmod 755 /var/www/cgi-bin/php74.fcgi

Step 7 Configure Apache

Open the file httpd.conf and configure your file with desired directory root for php5.6 and php7.4 I am going to display my configuration here.

php5.6

<VirtualHost *:80>
     ServerAdmin [email protected]
     ServerName localhost56.test
     DocumentRoot /var/www/html/56
     DirectoryIndex index.php index.html
<FilesMatch "\.php$">
  <If "-f %{REQUEST_FILENAME}">
      SetHandler "proxy:fcgi://127.0.0.1:9056"
  </If>
</FilesMatch>
     ScriptAlias /cgi-bin/ "/var/www/cgi-bin/"
     AddHandler php56-fcgi .php
     Action php56-fcgi /cgi-bin/php56.fcgi
 <Directory  "/var/www/html/56">
    AllowOverride All
  </Directory>
</VirtualHost>

ph.7.4

<VirtualHost *:80>
    ServerAdmin [email protected]
    ServerName localhost74.test
    DocumentRoot "/var/www/html/php74"
  DirectoryIndex index.html index.php
  <Directory  "/var/www/html/php74">
    AllowOverride All
  </Directory>
  <FilesMatch "\.php$">
 <If "-f %{REQUEST_FILENAME}">
        SetHandler "proxy:fcgi://127.0.0.1:9074"
</If>
</FilesMatch>
 ScriptAlias /cgi-bin/ "/var/www/cgi-bin/"
     AddHandler php74-fcgi .php
     Action php74-fcgi /cgi-bin/php74.fcgi
</VirtualHost>

Step 8 Start/Enable Services

Now All setting is completed we need to start or enable all the settings with the following command

systemctl enable httpd
systemctl enable php56-php-fpm
systemctl enable php74-php-fpm
systemctl start httpd
systemctl start php56-php-fpm
systemctl start php74-php-fpm

now the two domain that we have configred are running in two diffrent versions of php.

Additional Information

when you want to install some package for php then you need to install as below.

for example, if you want to install php-mbstring then you need to install as below

php5.6

yum install php56-php-mbstring

php7.4

yum install php74-php-mbstring

after successfull installtion you need to restart php-fpm

systemctl stop php56-php-fpm
systemctl stop php74-php-fpm

Other than new module installation, if you made any change on php.ini file then you also need to restart PHP-FPM

if you encounter any problem during the instalation then you can contact me by email at [email protected] or you can cmment below I will get you as soon as possible.

Have A good Day !!! 🙂

if you want to know how to install WSL on windows 10 then please visit wsl/ubuntu link