Running Multiple PHP Versions on One Linode

Linode Staff

Is it possible to have more than one PHP version on a Linode? For example, can you run an older version for compatibility but have a newer version of PHP available for new sites?

2 Replies

Yes, you can! However, it's a bit more involved than just installing multiple versions. Most PHP packages will install libraries in the same location, so the one installed last will overwrite other versions.

To get around this, you can use a program that can help you build different versions of PHP and switch between them. I'd recommend PHPBrew as it was tailor made just for this purpose.

If you're using cPanel, you can switch between PHP versions by using the MultiPHP Manager for WHM.

You should be able to achieve something similar using PHP-FPM. I tried this out on a newly deployed Linode running Debian 10 and was able to get two basic sites (each using a different PHP version) up and running. I pretty much started out by installing Apache, php and php-fpm.

sudo apt update
sudo apt upgrade
sudo apt install apache2 php php-fpm libapache2-mod-php

After those packages were installed I created the directories and a basic index.php page for my site.

sudo mkdir -p /var/www/html/site1.mydomain.com/public_html
sudo echo “<?php phpinfo(); ?>” > /var/www/html/site1.mydomain.com/public_html/index.php

Next, I created a simple configuration file for the website.

/etc/apache2/sites-available/site1.conf

<VirtualHost *:80>
  ServerName site1.mydomain.com
  DocumentRoot /var/www/html/site1.mydomain.com/public_html
  DirectoryIndex index.php
</VirtualHost>

Disable the default Apache site, enable the new one, and fix the ownership for our website’s files/directories so that Apache has access to them.

sudo a2dissite 000-default
sudo a2ensite site1
sudo chown -R www-data:www-data /var/www/html/site1.mydomain.com

Make sure to test the configuration for errors and reload Apache.

sudo apache2ctl configtest
sudo systemctl reload apache2

With that, I had my index.php script being executed (not by php-fpm) and a phpinfo(); page being served when I navigated to my site. To have the php scripts executed by php-fpm, I ran the following command and modified the configuration for my website:

sudo a2enmod proxy_fcgi setenvif

/etc/apache2/sites-available/site1.conf

<VirtualHost *:80>
  ServerName site1.mydomain.com
  DocumentRoot /var/www/html/site1.mydomain.com/public_html
  DirectoryIndex index.php
  <FilesMatch “.php$”>
    SetHandler “proxy:unix:/var/run/php/php-7.3-fpm.sock|fcgi://localhost/
  </FilesMatch>
</VirtualHost>

IMPORTANT NOTE: That path to the unix socket will be slightly different if you're using a different version of php-fpm. I’ve typically found that the sockets can be located in the /var/run/php/ directory after a new version of php-fpm is installed.

All that’s left was to test the configuration and reload Apache.

sudo apache2ctl configtest
sudo systemctl restart apache2

Now that I have one site that’s setup to use php-fpm (as confirmed by the Server API section on the phpinfo(); page), I pretty much went through a similar process to install another version of php and configure a different site to use that version.

In order to install a different version of php I needed to first add a third-party repository to my repo list. The following commands from this article setup everything for me on Debian 10.

sudo apt update
sudo apt install -y lsb-release ca-certificates apt-transport-https software-properties-common
echo "deb https://packages.sury.org/php/ $(lsb_release -sc) main" | sudo tee /etc/apt/sources.list.d/sury-php.list
wget -qO - https://packages.sury.org/php/apt.gpg | sudo apt-key add -
sudo apt update

NOTE: You may need to install gnupg2 in order for the wget command to work

sudo apt install gnupg2

After updating my list of packages I then installed php8.0 and php8.0-fpm

sudo apt install php8.0 php8.0-fpm

Next, I created the directory and files for the new site:

sudo mkdir -p /var/www/html/site2.mydomain.com/public_html
sudo echo “<?php phpinfo(); ?>” > /var/www/html/site2.mydomain.com/public_html/index.php
# Don’t forget to update the ownership of the files
sudo chown -R www-data:www-data /var/www/html/site2.mydomain.com

The apache configuration for the site is almost identical to the previous site.

/etc/apache2/sites-available/site2.conf

<VirtualHost *:80>
  ServerName site2.mydomain.com
  DocumentRoot /var/www/html/site2.mydomain.com/public_html
  <Directory /var/www/html/site2.mydomain.com/public_html>
    DirectoryIndex index.php
  </Directory>
  <FilesMatch “.php$”>
    SetHandler “proxy:unix:/var/run/php/php-8.0-fpm.sock|fcgi://localhost/
  </FileMatch>
</VirtualHost>

From there, you just need to test the configuration and then reload Apache.

sudo apache2ctl configtest
sudo systemctl restart apache2

Once Apache reloads you should be able to switch between the two sites and see that they both use different versions of PHP.

Reply

Please enter an answer
Tips:

You can mention users to notify them: @username

You can use Markdown to format your question. For more examples see the Markdown Cheatsheet.

> I’m a blockquote.

I’m a blockquote.

[I'm a link] (https://www.google.com)

I'm a link

**I am bold** I am bold

*I am italicized* I am italicized

Community Code of Conduct