How to run multiple versions of PHP on a server with Rocky Linux 9 / 8

Everything Linux, A.I, IT News, DataOps, Open Source and more delivered right to you.
Subscribe
"The best Linux newsletter on the web"

Hello, friends. Today, you will learn how to run multiple versions of PHP on a server with Rocky Linux 9 / 8

As we all know, PHP is an important language at server level since it has been used to create most of the web applications that exist. However, many times having so many web applications on the same server can cause the need to have several versions of PHP.

Let’s go for it.

Running multiple versions of PHP on a server with Rocky Linux 9 / 8

Preparing the system

Before starting, open a terminal or via SSH access the server and completely update the system.

sudo dnf update

Next, add the EPEL and REMI repositories.

sudo dnf install -y https://dl.fedoraproject.org/pub/epel/epel-release-latest-9.noarch.rpm
sudo dnf install -y https://rpms.remirepo.net/enterprise/remi-release-9.rpm

In the case of Rocky Linux 8 run

sudo dnf install -y https://dl.fedoraproject.org/pub/epel/epel-release-latest-8.noarch.rpm
sudo dnf install -y https://rpms.remirepo.net/enterprise/remi-release-8.rpm

Next, refresh the system repositories:

sudo dnf makecache

Now we can continue.

Install httpd

For this post, we will use Apache as a web server. First, install it with this command

sudo dnf install httpd

Start it up and set the appropriate firewall relays.

sudo systemctl enable --now httpd
sudo firewall-cmd --add-service=http --permanent
sudo firewall-cmd --reload

Now we can continue.

Install different PHP versions on Rocky Linux

For this post, we will use PHP versions 7.4 and 8.1. First install them, with this command.

sudo dnf install php74 php74-php-fpm
sudo dnf install php81 php81-php-fpm

Now create two PHP execution scripts. Each script will handle each version. Start with the one for PHP 8.1

sudo vi /var/www/cgi-bin/php81.fcgi

And add the following

/bin/bash
exec /bin/php81-cgi

Save the changes and close the editor.

Now it is the turn of the other script for PHP 7.4

sudo vi /var/www/cgi-bin/php74.fcgi

And add the following:

#!/bin/bash
exec /bin/php74-cgi

Moreover, save the changes and close the editor.

Set permissions on the files

sudo chmod 755 /var/www/cgi-bin/php*

Now it is necessary to make new Virtualhost for the different PHP versions. The idea is that they work on ports besides 80 but you can also configure a specific version for this.

Create the configuration file for PHP 8.1

sudo vi /etc/httpd/conf.d/php81.conf

Add this content

Listen 8181

<VirtualHost *:8181>
     ServerAdmin [email protected]
     ServerName exmaple81.com
     DocumentRoot /var/www/html
     DirectoryIndex index.php index.html
<FilesMatch "\.php$">
  <If "-f %{REQUEST_FILENAME}">
      SetHandler "proxy:unix:/var/opt/remi/php81/run/php-fpm/www.sock|fcgi://localhost"
  </If>
</FilesMatch>
     ScriptAlias /cgi-bin/ "/var/www/cgi-bin/"
     AddHandler php81-fcgi .php
     Action php81-fcgi /cgi-bin/php81.fcgi
 <Directory  "/var/www/html">
    AllowOverride All
  </Directory>
</VirtualHost>

Save the changes and close the editor.

Now it’s PHP 7.1’s turn

sudo vi /etc/httpd/conf.d/php74.conf

And add

Listen 8074

<VirtualHost *:8074>
     ServerAdmin [email protected]
     ServerName 74.test
     DocumentRoot /var/www/html
     DirectoryIndex index.php index.html
<FilesMatch "\.php$">
  <If "-f %{REQUEST_FILENAME}">
      SetHandler "proxy:unix:/var/opt/remi/php74/run/php-fpm/www.sock|fcgi://localhost"
  </If>
</FilesMatch>
     ScriptAlias /cgi-bin/ "/var/www/cgi-bin/"
     AddHandler php74-fcgi .php
     Action php74-fcgi /cgi-bin/php74.fcgi
 <Directory  "/var/www/html">
    AllowOverride All
  </Directory>
</VirtualHost>

Save the changes and close the editor.

Create a PHP file to demonstrate the operation.

sudo nano /var/www/html/index.php

Add some content

<?php
phpinfo();
?>

Then, you have to enable PHP-fpm and restart Apache

sudo systemctl enable --now httpd.service php81-php-fpm.service php74-php-fpm.service php74-php-fpm.service

Now open a web browser and visit http://your-server:8181 and you will see run the PHP 8.1 version of PHP 8.1

PHP 8.1 working
PHP 8.1 working

And open http://your-server:8074 to check the version with PHP 7.4

PHP 7.4 working too
PHP 7.4 working too

Remember that for all this to work, you have to open the specified ports.

Conclusion

With this post, you learned how to manage several PHP versions on the same server. It is a great solution for many cases.

Everything Linux, A.I, IT News, DataOps, Open Source and more delivered right to you.
Subscribe
"The best Linux newsletter on the web"
Angelo
Angelo
I am Angelo. A systems engineer passionate about Linux and all open-source software. Although here I'm just another member of the family.

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest articles

Join us on Facebook