Run Multiple PHP versions with Nginx on Ubuntu
Posted on July 30th, 2019
There are many reasons you might want to run multiple PHP versions with Nginx on Ubuntu server. For example, if one of your application is built on 7.0 and another one on PHP 7.2, you have to use multiple versions of PHP. In this short guide, I will show you how to complete this task on an Ubuntu server.
Our goal is to install PHP7.0, PHP7.1, PHP7.2, and PHP7.3 on the server. Then, we will update our virtual host configuration to use whatever PHP version we desire. We will also verify the configuration to make sure that PHP versions are being updated successfully.
Before getting started with the actual tutorial, there are some prerequisites that you must clear. There are only two requirements, the first one is, you must have Nginx with PHP-FPM configured on your server with a single PHP version. The second one is you must have sudo privileges on the server. We need sudo privileges because we will update the virtual host configuration files.
Once you are clear on prerequisites, continue with the tutorial.
Run Multiple PHP versions with Nginx
First of all, we will install other PHP versions on our server. If you followed our guide on setting up Nginx with PHP-FPM on Ubuntu, you might have PHP7.2 installed on your server.
Check the current PHP version
Whatever the case is, you must know the PHP version that is already installed on the server. It is because you can avoid installing it again. It is not a problem but it might save some time for you. Execute the following command to know the current PHP version installed on your server.
$ php -v
You should see the PHP version you are using in the output of this command. Once you know which version of PHP you are using right now, avoid installing the same version again to save some time. Now, let’s install other PHP versions on our server.
Install Multiple PHP versions
First of all, add the repository that will allow us to install PHP on our server. If you are using Ubuntu 16.04 or Ubuntu 18.04, the chances are, you already have this repository added in your sources. If not or you are not sure, execute the following commands to add the repository.
$ sudo add-apt-repository ppa:ondrej/php -y $ sudo apt-get update
Now, Execute the following commands to install PHP7.0 with FPM on your server.
$ sudo apt-get install php7.0 php7.0-fpm php7.0-mbstring php7.0-curl php7.0-mysql -y
To install PHP7.1 with FPM, execute the following commands on your server.
$ sudo apt-get install PHP7.1 php7.1-fpm php7.1-mbstring php7.1-curl php7.1.mysql -y
Similarly, you can repeat the process for PHP versions 7.2 and 7.3. Note that we have only included some commonly used PHP extensions in this installation. You can install as many PHP extensions as you require.
Once you are done installing different PHP versions on your server, Let’s see how you can use them.
Use Multiple PHP versions with Nginx
Now comes the interesting part. It’s time to change the PHP versions for some of our Nginx virtual hosts. In this tutorial, I am going to update the PHP version of the default Nginx site running on my server. However, you can update virtual host files on your server as per your requirements.
To open the Nginx virtual host file in edit mode, execute the following command.
$ sudo nano /etc/nginx/sites-available/SITE_NAME
Do not forget to replace the placeholder with the virtual host file name. If you are editing the default virtual host, you have to replace SITE_NAME with default.
In the file, find a line containing the following content.
Now, replace the PHP version in that line. For example, If you want to use PHP7.3 for that specific virtual host, the updated line should look like the following
If you want to use PHP7.1, replace it with 7.1 and so on. After updating your virtual host configuration file, execute the following command to restart the Nginx server. You have to restart the Nginx server to apply the changes.
$ sudo service nginx restart
Once done, your application will start using the PHP version you have set in the virtual host directory. Now, Let’s see how we can test the updated configuration.
Test the configuration
To test the configuration, we can create a test PHP file inside the document root of our site and access it from the browser to check the PHP version it is using. To create a test PHP file, execute the following command.
$ echo "<?php phpinfo(); ?>" | sudo tee /path/to/document/root/test.php
If you are using default virtual host in Nginx, your path will be
/var/www/html. After executing the command, try to access the test configuration file from the browser using the following URL format.
The output will show you the PHP version on which your site is running. It will also show you other PHP configuration information. After verification, make sure to delete the test.php file from your server as it might reveal some information about your server that you would not like to share.
Conclusion: So, this is how you can use multiple PHP versions with Nginx. If you have multiple virtual hosts on your server, you can use different PHP versions for different virtual hosts too! It means that you can run some of your sites on PHP7.1 and some of your sites on PHP7.2 and some of your sites on PHP7.3.
Let us know if you need help using Multiple PHP versions on your server. You can comment down your query or contact our support department for help!