If you are a system or web server administrator, it's very important for you to know how to install and use multiple PHP versions on a single server. This is very useful when you have multiple PHP applications on your server and each application is compatible with the different PHP versions. In this case, you can configure NGINX to define your desired PHP version.
In this article, we'll show you how to install PHP 7.2, 7.4, 8.0 and 8.1 on Ubuntu 20.04.
Prerequisites
- A server running Ubuntu 20.04.
- A root password is configured on the server.
Add a PHP Repository
By default, Ubuntu 20.04 ships with the PHP version 7.4. So you'll need to add the PHP repository in your system to install the multiple PHP versions.
First, install the required dependencies with the following command:
# apt-get install software-properties-common gnupg2 -y
Once all the dependencies are installed, add the PHP repository using the following command:
# add-apt-repository ppa:ondrej/php
Next, update the repository with the following command:
# apt-get update -y
Once the repository is up-to-date, you can proceed to install multiple PHP versions.
Install Multiple Versions of PHP (7.2, 7.4, 8.0 and 8.1)
To install the PHP version 7.2 with PHP-FPM support, run the following command:
# apt-get install php7.2 php7.2-fpm php7.2-cli -y
To install the PHP version 7.4 with PHP-FPM support, run the following command:
# apt-get install php7.4 php7.4-fpm php7.4-cli -y
To install the PHP version 8.0 with PHP-FPM support, run the following command:
# apt-get install php8.0 php8.0-fpm php8.0-cli -y
To install the PHP version 8.1 with PHP-FPM support, run the following command:
# apt-get install php8.1 php8.1-fpm php8.1-cli -y
At this point, all PHP versions are installed in your system.
Set the Default PHP Version for the Command Line
First, check the current version of command-line PHP running the following command:
# php --version
You should see the following output:
PHP 8.0.5 (cli) (built: May 3 2021 11:30:57) ( NTS )
Copyright (c) The PHP Group
Zend Engine v4.0.5, Copyright (c) Zend Technologies
with Zend OPcache v8.0.5, Copyright (c), by Zend Technologies
As you can see, PHP 8.0 is the default PHP command line version.
Now, set the default command line PHP version to PHP 7.4 using the following command:
# update-alternatives --config php
You will be asked to set the default PHP version as shown below:
There are 3 choices for the alternative php (providing /usr/bin/php).
Selection Path Priority Status
------------------------------------------------------------
* 0 /usr/bin/php8.0 80 auto mode
1 /usr/bin/php7.2 72 manual mode
2 /usr/bin/php7.4 74 manual mode
3 /usr/bin/php8.0 80 manual mode
Press "enter" to keep the current choice[*], or type selection number: 2
update-alternatives: using /usr/bin/php7.4 to provide /usr/bin/php (php) in manual mode
Select your desired PHP version and hit "Enter" to set it to the default version.
Now, verify your default PHP version using the following command:
# php --version
You should see the following output:
PHP 7.4.18 (cli) (built: May 3 2021 11:27:06) ( NTS )
Copyright (c) The PHP Group
Zend Engine v3.4.0, Copyright (c) Zend Technologies
with Zend OPcache v7.4.18, Copyright (c), by Zend Technologies
Switch Between Multiple PHP Versions with Nginx
You can switch between multiple PHP versions easily by configuring the Nginx virtual host and PHP-FPM.
First, install the Nginx server package with the following command:
# apt-get install nginx -y
Next, you will need to test which PHP version is used by your Nginx server.
To do so, create a info.php file in your Nginx web root directory:
# nano /var/www/html/info.php
Add the following line:
<?php phpinfo(); ?>
Save and close the file when you are finished.
To use PHP version 7.2 with Nginx server, edit the Nginx default virtual host configuration file as shown below:
# nano /etc/nginx/sites-enabled/default
Remove all lines and add the following lines:
server { # php/fastcgi
listen 80;
server_name _;
root /var/www/html/;
index info.php;
access_log /var/log/nginx/access_log;
error_log /var/log/nginx/error_log;
location ~ \.php$ {
try_files $uri =404;
fastcgi_pass unix:/run/php/php7.2-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
Save and close the file. Then verify the Nginx for any syntax error with the following command:
# nginx -t
You should get the following output:
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
Next, restart Nginx to apply the changes:
# systemctl restart nginx
Now, open your web browser and access the info.php page using the URL http://your-server-ip. You should see the PHP version used by Nginx server in the following screen:
To use PHP version 7.4 with Nginx server, edit the Nginx default virtual host configuration file as shown below:
# nano /etc/nginx/sites-enabled/default
Just find the following line:
fastcgi_pass unix:/run/php/php7.2-fpm.sock;
And replaced it by the following line:
fastcgi_pass unix:/run/php/php7.4-fpm.sock;
Save and close the file then restart the Nginx to apply the changes:
# systemctl restart nginx
Now open your web browser and access the info.php page using the URL http://your-server-ip. You should see the PHP version used by Nginx server in the following screen:
Conclusion
In the above guide, you've learned how to deal with multiple PHP versions on Ubuntu 20.04. We hope this will help you to host multiple web applications with different PHP versions.