Run Multiple PHP versions with Nginx on Ubuntu

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.

fastcgi_pass unix:/run/php/php7.X-fpm.sock;

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

fastcgi_pass unix:/run/php/php7.3-fpm.sock;

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.

http://DOMAIN_OR_SERVER_IP/test.php

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!

 

Source : https://www.interserver.net/tips/kb/multiple-php-versions-nginx-ubuntu/

Posted on: August 1, 2022, by :  | 1 views