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.

959 views
d

By. Jacob

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. Understanding file permissions in Unix / Linux based systems, and how to make files immutable.
  2. In this article I will explain how to enable a swapfile on small instances, and why it might be useful, even if you do have enough physical memory.
  3. How to determine an optimal value for pm.max_children and related php-fpm settings for your server and web applications.
  4. Tutorial showing how to configure a VirtualBox Guest VM with HOST-only and NAT adapter, while using the WWW folder from the HOST OS.
  5. 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.

More in: Linux servers