November 21, 2024
How To Run Multiple PHP Versions on One Server Using Apache and PHP-FPM on CentOS 7

How To Run Multiple PHP Versions on One Server Using Apache and PHP-FPM on CentOS 7

Multiple PHP version has come nowadays, so if you want to deploy your current project that is made upon PHP 7.4 and your server PHP version is PHP 5.6 and your older projects are also running on that server and you don’t want to hamper your running projects as well you want to host the recent application that is made for PHP 7.4, so how will you do it.

The answer is simple we will need to run multiple instances of PHP by assigning them a different port number.

I am using centos 7 and Apache2 for this article.

How does 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 versions 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 (the oldest Php version that which can be installed easily in centos 7 you might install php5.3 but I have not tested it 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 we need to 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 by pasting the following command on the 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 the 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 the given line in your terminal.

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

Running 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

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 the 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 a script wrapper to call both versions of php on the basis of users request to create a 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

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 admin@vexplains.com
     ServerName vexplains.com
     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 admin@vexplains.com
    ServerName vexplains.com
    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

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

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 have any problem then mail me @ kumarvedant777@gmail.com

Vedant Kumar

Currently I'm working as an Implementation Engineer, Started my career as an System Administrator - Linux. Additionally loves to explore new technologies and research about new open-source software that ease the development cycle.

View all posts by Vedant Kumar →

Leave a Reply

Your email address will not be published. Required fields are marked *


The reCAPTCHA verification period has expired. Please reload the page.

close

Ad Blocker Detected!

VEDANT EXPLAINS
We've noticed that you are using an ad blocker. Advertising helps fund our server cost and keep it truly independent. It helps to build our content creator team. So please disable your ad blocker, and help us to keep providing you with free- great content - for free. Thank you for your support.

Refresh