Verification: a143cc29221c9be0

Nginx different php versions for websites

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.

sudo apt install python-software-properties
sudo add-apt-repository ppa:ondrej/php

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.

apt update
sudo apt install php5.6 php5.6-fpm
sudo apt install php7.2 php7.2-fpm

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.

sudo systemctl status php5.6-fpm
sudo systemctl status php7.2-fpm
  • 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.

sudo apt update 
sudo apt install nginx

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.

sudo mkdir /var/www/php56
sudo mkdir /var/www/php72

Now, create and index.php containing the phpinfo() function.

echo "" > /var/www/php56/index.php
echo "" > /var/www/php72/index.php

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.

sudo vim /etc/nginx/sites-available/php56.example.com

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.

# Application with PHP 5.6
#
server {
	listen 80;

	root /var/www/php56;
	index index.php;
	server_name php56.example.com;

	location ~* \.php$ {
		# With php-fpm unix sockets
		fastcgi_pass unix:/var/run/php/php5.6-fpm.sock;
		include         fastcgi_params;
		fastcgi_param   SCRIPT_FILENAME    $document_root$fastcgi_script_name;
		fastcgi_param   SCRIPT_NAME        $fastcgi_script_name;
	}
}

Similarly, create a second VirtualHost configuration file to work with PHP 7.2. Edit configuration file in text editor:

sudo vim /etc/nginx/sites-available/php72.example.com

Add the following content to file with proper ServerName and DocumentRoot.

# Application with PHP 7.2
#
server {
	listen 80;

	root /var/www/php72;
	index index.php;
	server_name php72.example.com;

	location ~* \.php$ {
		# With php-fpm unix sockets
		fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
		include         fastcgi_params;
		fastcgi_param   SCRIPT_FILENAME    $document_root$fastcgi_script_name;
		fastcgi_param   SCRIPT_NAME        $fastcgi_script_name;
	}
}

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.

sudo ln -s /etc/nginx/sites-available/php56.example.com /etc/nginx/sites-enabled/
sudo ln -s /etc/nginx/sites-available/php72.example.com /etc/nginx/sites-enabled/

After making all the changes restart Nginx to reload new settings changes.

sudo systemctl restart nginx.service

Your setup has been completed now. Go to the next step to test your setup.