How to run different PHP versions on the same server

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

Sometimes a SysAdmin receives strange requests. For example: a couple of days ago my client need to upload a new site, this new site needs PHP7.4 while their server run with PHP7.2. I’ve installed then 7.4 but all the other VirtualHosts (at least the important ones) broke under php7.4. Initially I was thinking on some kind of containers but I’ve found something simpler. This is how to run different PHP versions on the same server.

My setup

I always create or reuse a virtual machine for all my articles here. Because you should always test before break something important.
For this tutorial I’ve created a fresh install of an ¿old? Ubuntu server 18.04. I will have a site, sitio1 (sitio is site in spanish) that needs PHP 7.2; and another site that needs PHP 7.4, sitio2.

1. Add Ondrej php repository

Back in my days you had to edit config files by hand. But now we have this package software-properties-common to easily manage your distribution and independent software vendor software sources.

So our 1st step is to install that package:

sudo apt-get install software-properties-common
How to run different PHP versions. install software-properties-common package
Installing software-properties-common

Our 2nd step is to actually add the repository by running:

sudo add-apt-repository ppa:ondrej/php

adding a repository

Pay attention to on screen messages and follow the instructions. Then run

sudo apt-get update

2. Install two different PHP versions

2.1. PHP 7.2

The goal of this tutorial is to show you how I managed to run different php versions on the same server. I’m not installing (almost) any php module but if you are reading this you surely need some of them, mysql support for example. Don’t forget to install them.

sudo apt-get install php7.2 php7.2-fpm libapache2-mod-php7.2 libapache2-mod-fcgid
How to run different PHP versions. install php7.2
Installing PHP7.2 and Apache

2.2 PHP 7.4

Now repeat the process with php 7.4. Remember to install the needed php modules.

sudo apt-get install php7.4 php7.4-fpm libapache2-mod-php7.4
Installing PHP 7.4

3. Start fpm services

Now we need to start the fpm service for both versions

sudo systemctl start php7.2-fpm
sudo systemctl start php7.4-fpm

You can then check the status of those service running:

sudo systemctl status php7.x-fpm

starting php-fpm server

4. Creating the VirtualHosts

4.1 VirtualHost data

We are going to create first directories under the /var/www for every VirtualHost.

mkdir /var/www/sitio1
mkdir /var/www/sitio2
chown  -R www-data:www-data /var/www

Secondly, create a php file on every directory with the following content only to show php information:

<?php 
phpinfo(); 
?>
How to run different PHP versions. virtualhosts 1

4.2 VirtualHost configuration

We need to create a .conf file for every site, with the following content:

nano /etc/apache2/sites-available/sitio1.conf
<VirtualHost *:80>
     ServerAdmin admin@sitio1
     ServerName sitio1
     DocumentRoot /var/www/sitio1
     DirectoryIndex info.php
     <Directory /var/www/sitio1>
        Options Indexes FollowSymLinks MultiViews
        AllowOverride All
        Order allow,deny
        allow from all
     </Directory>

    <FilesMatch \.php$>
      # this part do the trick
      SetHandler "proxy:unix:/run/php/php7.2-fpm.sock|fcgi://localhost"
    </FilesMatch>

     ErrorLog ${APACHE_LOG_DIR}/sitio1_error.log
     CustomLog ${APACHE_LOG_DIR}/sitio1_access.log combined
</VirtualHost>

The create another file /etc/apache2/sites-available/sitio2.conf changing the proper information (sitio1 becomes sitio2, php7.2 becomes php7.4 etc)

Configuring a virtualhost

5. Add DNS information

Or not. I mean, on the real life you will need to create proper DNS records for both sites. As this is only for testing and learning purposes, I’m only creating records on my /etc/hosts file. The IP address of this virtual machine is 192.168.122.14

How to run different PHP versions. /etc/hosts files
/etc/hosts file

Know I’m able to resolve sitio1 and sitio2.

6. Enabling everything

We need to enable some apache modules needed to handle this configuration. Then to enable those virtualhosts, and finally to start apache:

sudo a2enmod actions fcgid alias proxy_fcgi
sudo a2ensite sitio1
sudo a2ensite sitio2
sudo systemctl start apache2
enabling virtualhosts and starting apache2

You can also test the configuration for misspellings, missing modules or any other problem with apachetcl configtest.

7. test time

Now we need to point a browser to http://sitio1 and http://sitio2 and check if their are running the correct PHP version:

One out of two running the correct php
How to run different PHP versions. testing the sites
Two out of two running fine.

¡Yay!. And this is how to run different PHP versions on the same server. Now I’m curious on what other combinations I could try.

Bonus: Older ubuntus

My client actually have Ubuntu 16 that went out of support back in April 2021. The repository I had to add was ppa:tomvlk/php-archive

Everything Linux, A.I, IT News, DataOps, Open Source and more delivered right to you.
Subscribe
"The best Linux newsletter on the web"
Gonzalo Rivero
Gonzalo Rivero
I am Gonzalo, I live in Salta, a city located in the NW of Argentina. I play the guitar and a little harmonica. I also like to bike.

2 COMMENTS

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest articles

Join us on Facebook