This tutorial helps you with the installation and configuration of two VirtualHost on the Nginx web server with different PHP versions. First VirtualHost will work with PHP 5.6 and another VirtualHost will run with PHP 7.2. So just go through this tutorial. You can also use more than two PHP versions with Nginx as required but this tutorial covers two only.
PHP Installation
For the installation of PHP versions, we use the PPA maintained here. Use the below couple of commands to add the PPA to your system. For this tutorial, I used PHP 5.6 and PHP 7.2 to configure it with the Nginx web server. To use the multiple PHP versions, we will use PHP FPM and FastCGI. Let’s install the following packages on your system. You may also need to install additional PHP modules. For the tutorial, only the above packages are required. After installation, php-fpm services will be started automatically. Use the following commands to make sure both services are running.
Recommended: How to Enable/Disable PHP Modules on Ubuntu
Nginx Installation
The Nginx web server is packages are available in the official Ubuntu repository. Launch terminal on your system or login with ssh for remote systems. Execute the following commands to install the latest available version of the Nginx web server.
Nginx Configuration
Get ready for the configuration of websites in the Nginx server. For the testing purpose, I am configuring two websites to work with two different-2 PHP versions. First, create two directories on your server. Now, create and index.php containing the phpinfo() function. After that create server blocks for both sites on Nginx. The latest version of Nginx keeps the server block configuration files under /etc/nginx/sites-available directory. Create a file for the first virtual host and edit in your favorite text editor. Add the following content. Make sure to use the correct ServerName and directory path according to your setup. This website is configured to work with PHP 5.6. Similarly, create a second VirtualHost configuration file to work with PHP 7.2. Edit configuration file in text editor: Add the following content to file with proper ServerName and DocumentRoot. All right, You have created both websites in your Nginx. But they are still not active. Nginx keeps active sites under /etc/nignx/sites-enabled directory. You can simply create a symbolic link for both config files to this directory or use the below command to do the same. After making all the changes restart Nginx to reload new settings changes. Your setup has been completed now. Go to the next step to test your setup.
Test Setup
All done. You can access both sites in your favirote web browser . You will see that php56.example.com shows the version PHP 5.6 and php72.example.com is showing the PHP 7.2 as the configuration.
Congratulations, You system is ready to host websites with different PHP versions. Happy hosting.