Upgrading or Downgrading PHP Versions

Last updated: September 15th 2019

Introduction

Upgrading or Downgrading PHP versions on a Webdock stack is essentially the same procedure. In short what you need to do is:

  • Ensure the php packages for the version you want are installed
  • Update any configuration for that PHP version
  • Update your web server configuration to point to the correct PHP version
  • Point PHP CLI to the correct PHP version
  • Restart your web server and php-fpm
  • Mask the old php-fpm daemon

Before performing this operation, it is always a good idea to take a snapshot of your server in case something goes wrong!

1. Make sure packages for your target version of PHP are installed

You need to make sure the correct packages are available on your system for the PHP version you want.  First make sure your system is up to date:

sudo apt update
sudo apt upgrade

If it prompts you what to do with config files, usually you should just go with the default option and leave the current config as-is.

Next, install the packages - the packages you want are listed on our Perfect Server stack pages. For example say you want to use PHP7.3 you would go to the 7.3 guide and find the section titled Install PHP7.3 and common PHP packages - the command you are looking for would look something like:

sudo apt install -y php7.3-cli php7.3-dev php7.3-pgsql php7.3-sqlite3 php7.3-gd php7.3-curl php-memcached php7.3-imap php7.3-mysql php7.3-mbstring php7.3-xml php-imagick php7.3-zip php7.3-bcmath php7.3-soap php7.3-intl php7.3-readline php7.3-common php7.3-pspell php7.3-tidy php7.3-xmlrpc php7.3-xsl php7.3-opcache php-apcu

Please copy/paste commands from our Perfect Server guides as these are always kept up to date, and commands shown here are for example purposes only!

Finally, you should install php-fpm in the target version you want:

sudo apt install php7.3-fpm

2. Make sure PHP configuration for your target version is updated

Again, here you copy/paste the commands from the Perfect Server guide in the sections titled 

  • Update PHP CLI configuration
  • Tweak PHP-FPM settings
  • Tune PHP-FPM pool settings

3. Update your web server configuration to point to the correct PHP version

For Nginx you would edit /etc/nginx/sites-available/webdock and for Apache you would edit /etc/apache2/sites-available/webdock.conf 

For Nginx, look for this line and replace the PHP version number with the one you want:

fastcgi_pass unix:/var/run/php/php7.3-fpm.sock;

For Apache, look for this line and replace the PHP version number with the one you want:

SetHandler "proxy:unix:/run/php/php7.3-fpm.sock|fcgi://localhost"

4. Make sure PHP-CLI points to the right PHP version

Especially if you are downgrading, you need to check that php-cli is pointing to the correct version. First check what version the cli is using right now by executing:

sudo php -v

If the version number reported is different than the one you want, execute the following command replacing the version number with the one you want:

sudo rm /etc/alternatives/php; sudo ln -s /usr/bin/php5.6 /etc/alternatives/php;

Confirm by running php -v once more.

5. Restart PHP-FPM and your webserver

For example if your web server is nginx, you would execute:

sudo systemctl restart php7.3-fpm; systemctl restart nginx;

Again, look in the Perfect Server guides for your version and webserver for the correct command here.

Congratulations, you should now be running on the PHP version you want. You can check it by creating a small php file in your web root with the following contents and visiting it in your web browser:

<?php

 phpinfo();

?>

6. Mask the old PHP-FPM daemon

Here you essentially just tell the system to not start the old php-fpm daemon - replace the version number below with the previous version you are moving away from, say if you are going from php7.2 to php7.3 you would do:

sudo systemctl mask php7.2-fpm