[UPDATED 2022] Change PHP version on Ubuntu, Linux

By Dillon Smart · · · 1 Comments

PHP

In this post, I will show you how to switch PHP version on Ubuntu, Linux.

If you are developing on Windows, follow this guide to set up Windows Sub System for Linux.

What will be covered:

In this example, we will be changing our PHP version from PHP 8.1 to PHP 5.6. This is a common change if you manage and maintain a legacy website or web application.

Note: PHP 5.6 is in end of life and has not been supported since December 2018.

You can find all supported versions of PHP on the official PHP website.

As of writing this post, the supported versions of PHP are 7.4, 8.0, and 8.1.

Supported PHP versions

Change PHP version on Ubuntu, Linux for Apache2

To update your PHP version for Apache2, install your chosen PHP version. In this example, we want to downgrade PHP to version 5.6 from PHP 8.1. 

sudo apt-get install python-software-properties
sudo add-apt-repository ppa:ondrej/php
sudo apt-get update
sudo apt-get install -y php5.6

Now we need to disable PHP 8.1 and enable PHP 5.6:

sudo a2dismod php8.1 // disables the current version of php
sudo a2enmod php5.6 // enables the new version of php

Don’t forget to restart Apache2.

sudo service apache2 restart

Next, ensure PHP 5.6 is enabled by running:

php -v

Change PHP version on Ubuntu, Linux for Nginx

To change your PHP version for Nginx, you should take a backup of your configuration file. This will make it easier to restore if needed.

sudo cp /etc/nginx/sites-available/default /etc/nginx/sites-available/default.bak

Next, open the file in your favorite editor. In this example, we will be using Nano.

sudo nano /etc/nginx/sites-available/default

Update the PHP-FPM socket to the new version.

fastcgi_pass unix:/run/php/php8.1-fpm.sock;

It’s always best to test your configuration. To test your configuration for Nginx, run:

nginx -t

Finally, restart Nginx by running:

sudo service nginx restart

Change PHP CLI version

The steps to change your PHP CLI version are a little different. Use the commands below to change your PHP CLI version.

sudo update-alternatives --set php /usr/bin/php5.6
sudo update-alternatives --set phar /usr/bin/phar5.6
sudo update-alternatives --set phar.phar /usr/bin/phar.phar5.6

Conclusion

You have successfully changed your PHP version on Ubuntu, Linux.

Do you change PHP versions regularly? Let me know in the comments.

Next, learn how to install Composer and check your Composer version.

LinuxPHPUbuntu

1 Comment

Composer: How to install and use Composer for PHP - IKnowThatNow

[…] Next, learn how to change your PHP version. […]

Was this helpful? Leave a comment!

This site uses Akismet to reduce spam. Learn how your comment data is processed.

What is a Cron Job and how to write to the Crontab

Updated 10th December 2023

The need to schedule tasks on a machine is common for any Software Developer, System Admin or IT professional, so it’s important to learn what a cron job is and how to write to the crontab. What is CRON? Cron is a daemon built-in to most Unix-like operating systems such as: Cron reads the crontab (cron tables)

Find php.ini on Apple Silicon installed with Homebrew

Updated 1st October 2023

Are you having trouble finding your php.ini file which was installed locally with Homebrew? Packages installed with Homebrew can be installed in different locations making it hard to find, especially if you are on new Apple Mac running Apple’s Silicon chips. Here, I will show you home to locate your locally installed php.ini file installed

What is PHP?

Updated 1st August 2022

PHP (PHP: Hypertext Preprocessor) is an open-source, general-purpose, and server-side scripting language widely used in web development. PHP is used to manage databases, dynamic content, session tracking, and much more. Let’s answer the question “What is PHP?”. What is PHP used for? Since its first release in 1995, PHP has been seen used in many