How to Change PHP Version

Sometimes it is necessary to change the PHP version used by one or many web applications on a server. This could be due to a whole myriad of reasons, though it is likely because either the code in the app is too old for newer versions of PHP, or it features utilities not available in older PHP versions.


In this tutorial, we will learn how to change PHP versions and configure them to be used on different web applications.


Check Which Version Your Site is Using

You are going to need to know what version of PHP your site is currently using and validate it has changed. Please read about how to do this here.


List PHP Versions Available

Now let's list the versions of PHP available on your system. We can do this using the update-alternatives command-line utility with the --list flag. It will return a list of paths to the PHP versions on your system. 


sudo update-alternatives --list php


Set Default PHP Version

To set the default PHP version your system use the following update-alternatives command, passing the PHP version you wish to use at the end.


sudo update-alternatives --set php /usr/bin/php7.2


If it works, the above command should return a message stating the new default PHP version:


update-alternatives: using /usr/bin/php7.2 to provide /usr/bin/php (php) in manual mode


To confirm this run php -v:


php -v
PHP (cli) (built: Oct 6 2020 12:01:28) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.2.0, Copyright (c) 1998-2018 Zend Technologies
 with Zend OPcache, Copyright (c) 1999-2018, by Zend Technologies



Enable a PHP Version in Apache

To enable a new version of PHP in Apache, use the a2enmod utility in sudo mode, passing the name of the PHP version as the first argument.


sudo a2enmod php7.2


Then restart the server to complete:


sudo service apache2 restart



Setting PHP Version For a Single Site in Apache

To set a PHP version for a specific website in Apache, open the .htaccess file in its web root and paste the following line, changing php72 to the version you wish to use:


AddHandler application/x-httpd-php72 .php



Change the PHP Version for a Site in Nginx

The PHP version for a site in Nginx can be changed in its configuration file. If you don't know the name of the applications configuration file list them using the following command:


ls /etc/nginx/sites-available


Now open the configuration file using nano in sudo mode, replacing with the name of your configuration file:


sudo nano /etc/nginx/sites-available/


In the file, there should be a block of code that looks like this:


location ~ \.php$ {    
  include snippets/fastcgi-php.conf;
  fastcgi_pass unix:/run/php/php7.2-fpm.sock;


Change PHP version number to the desired one and save the file.


Now test Nginx for an configuration errors:


nginx -t


If no errors are reported, restart Nginx to apply the changes using the following command:


sudo service nginx restart



You now know how to change the default PHP version for your system and how to apply a particular version to specific sites on both Nginx and Apache HTTP servers.

version change