The easiest way to install multiple versions of PHP is by using the PPA from Ondřej Surý, who is also the maintainer of certbot PPA. To add this PPA, run the following commands in terminal. The software-properties-common
package is needed if you want to install software from PPA. It’s installed automatically on Ubuntu desktop, but might be missing on your Ubuntu server.
sudo apt install software-properties-common sudo add-apt-repository ppa:ondrej/php sudo apt update
Now you can install PHP7.2 on Ubuntu 16.04/17.10 by executing the following command.
sudo apt install php7.3 php7.3-fpm
sudo apt install php7.3-mysql php7.3-mbstring php7.3-xml php7.3-gd php7.3-curl php7.3-zip
sudo apt install php7.2 php7.2-fpm
And install some common PHP7.2 extensions.
sudo apt install php7.2-mysql php7.2-mbstring php7.2-xml php7.2-gd php7.2-curl php7.2-zip
You can view all available PHP7.2 extensions by typing in sudo apt install php7.2
and pressing Tab key twice.
To install PHP7.1 on Ubuntu 16.04/17.10, run
sudo apt install php7.1 php7.1-fpm
Install some common PHP7.1 extensions.
sudo apt install php7.1-mysql php7.1-mbstring php7.1-xml php7.1-gd php7.1-curl php7.1-zip
You can install PHP7.0 and PHP5.6 in the same way. Simply replace the version number in these commands.
Switching PHP Version in Nginx Server Block
It’s very easy to switch PHP version in Nginx server block. As you probably know, Nginx runs PHP code via PHP-FPM, which listens on a Unix socket. The socket file is located in /run/php/
directory.
location ~ \.php$ { fastcgi_pass unix:/run/php/php5.6-fpm.sock; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; include snippets/fastcgi-php.conf; }
I’m sure you get the idea now. Save and close the file. Then reload Nginx for the changes to take effect.
sudo systemctl reload nginx