Install Multiple PHP Versions on the Same Server

In this tutorial I will show you how you can install multiple PHP versions, on a server running Apache.

356 views

Edited: 2020-12-09 12:59

Sometimes you may need to run multiple versions of PHP on the same server in order to support outdated software. I had to install PHP7.4 alongside PHP8.0 myself recently to support Nextcloud, which does not yet support PHP8.

The unfortunate reality with Ubuntu is that they are often slow at updating the official repositories, and so, we will either have to compile PHP from the source, or use a thrid-party repository. One that is often recommended is ondrej/php. The disadvantage of not using the official repositories is that you could be missing out on security updates, but this risk might be worth it for many looking to run the latest version of PHP.

The maintainer of this package is Ondřej Surý, a Debian developer, so it should be safe to add it to your system; apparently, the official packages are even based on ondrej/php. Of course when adding third-party PPAs, it is always a good idea to only add PPA's from sources you trust.

Installing multiple versions of PHP

Follow the below steps to get things running.

1. Add Ondřej's PPA:

sudo add-apt-repository ppa:ondrej/php
sudo apt update

2. Install the relevant PHP7.4 packages that you need for your project. For example:

apt install php7.4 php7.4-mysql php7.4-mbstring php7.4-gd php7.4-fpm

3. Then install PHP8.0:

apt install php8.0 php8.0-mysql php8.0-mbstring php8.0-gd php8.0-fpm

4. make sure to enable PHP8.0-fpm:

a2enconf php8.0-fpm

Now that php8.0-fpm is enabled as the default configuration to load, we can enable php7.4-fpm for a single VHOST; to do this, we just need to include the configuration file for php7.4-fpm in the VHOST configuration for the website we want to use PHP7.4.

5. include the php8.0-fpm.conf file in the relevant VHOST:

include /etc/apache2/conf-available/php7.4-fpm.conf

The php7.4-fpm.conf file should be included within the <VirtualHost> block, like this:

<VirtualHost 10.0.0.11:443>
    DocumentRoot "/var/www/example-site-name/"
    ServerName example.com
    ErrorLog ${APACHE_LOG_DIR}/example.com-error.log
    CustomLog ${APACHE_LOG_DIR}/example.com-access.log common

    include /etc/apache2/conf-available/php7.4-fpm.conf
   
    # The rest of your configurations...
    # ...

When done, remember to either reload or restart Apache.

service apache2 restart

Fully uninstalling PHP

If you later need to uninstall the older version completely, you can easily do so using the asterisk wildcard (*) character:

apt-get remove purge php7.*

For some reason you will need to use apt-get rather than just apt for this to work.

Removing packages you do not need is a good idea. Also remember to remove the line you added in the VHOST configuration file.

Testing if php-fpm works

To test if things are working, you can create a phpinfo.php file in the website root, and open it in your web browser:

<?php
echo phpinfo();

This file will show various details about your server in your browser, including the version of PHP in use by the virtual host.

Links

  1. The main PPA for supported PHP versions with many PECL - launchpad.net

Tell us what you think:

  1. You may have wondered what the /etc/php/8.0/conf.d/ directory is for in Debian and Ubuntu, and whether it is better to edit the conf.d files than editing php.ini directly; find out in this Tutorial.
  2. Hot to install the Apache HTTP server in Linux.
  3. How to configure a SMTP server with letsencrypt on an amazon EC2 instance.
  4. Changing the default charset for the Apache HTTP server may be done by changing either the v-host .conf files or the charset.conf file. If you do not have access to these files, you can try .htaccess instead. Find out how in this tutorial.
  5. How to have multiple public IPs with one AWS EC2 Instance

More in: Linux servers