Upgrading or Downgrading PHP Versions

Last updated: October 24th 2023

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

Note: You can easily change PHP versions in Webdock using the "Manage PHP" interface if you are on our LEMP/LAMP stacks. This saves you having to run the below commands manually in order to change PHP versions.

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.4 you would go to the 7.4 guide and find the section titled Install PHP7.4 and common PHP packages - the command you are looking for would look something like:

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

If you get an error claiming the appropriate PHP packages cannot be found, then you are on a really old version of our stacks which does not have the ondrej/php PPA added. In which case, execute:

sudo apt-add-repository ppa:ondrej/php -y

In general, 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.4-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. Look for this line and replace the PHP version number with the one you want:

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

For Apache you would edit /etc/apache2/sites-available/webdock.conf and /etc/apache2/sites-available/webdock-le-ssl.conf. Look for this line and replace the PHP version number with the one you want:

SetHandler "proxy:unix:/run/php/php7.4-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.4-fpm; sudo 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.4 you would do:

sudo systemctl mask php7.2-fpm
We use cookies. Please see our Privacy Policy.