Over time, PHP releases a new version, making the previous ones obsolete. This means that they no longer receive updates or security patches, making them more vulnerable. It is therefore very important to use the minimum supported version of PHP and to keep all PHP packages up to date.
PHP is currently in its 8.3 version, bringing with it many new features and improvements in performance, syntax, security and stability.
Check Your Current Version of PHP
To find out your current PHP version, use the following command:
php -v
The result will look something like this:
PHP 8.3.1 (cli) (built: Dec 21 2023 20:12:13) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.3.1, Copyright (c) Zend Technologies
with Zend OPcache v8.3.1, Copyright (c), by Zend Technologies
with Xdebug v3.3.0, Copyright (c) 2002-2023, by Derick Rethans
Add ondrej/php PPA
If you try to install PHP version 8.3 you will now get a package not found error.
Run the following command to add the repository with all the PHP versions present:
sudo add-apt-repository ppa:ondrej/php
Install PHP 8
Use the following command to install php:
sudo apt install php8.3
Change php8.3
part of the command above with the appropriate PHP version.
You can also install older versions of PHP.
Purge old PHP versions
If the new installation is working as expected, you can remove the old PHP packages from the system:
sudo apt purge '^php7.4.*'
This assumes you are using PHP 7.4 as the previous version. Change php7.4
part of the command above with the appropriate PHP version.
Running PHP 8 with Other Versions
If you want to keep more than one version and switch between versions, use it on the system.
run the following command:
which php
As a result, we have this directory:
/usr/bin/php
Let's see what's in that directory:
ls -la /usr/bin/php
rwxrwxrwx 1 root root 21 jan 1 21:50 /usr/bin/php -> /etc/alternatives/php
We have a link to another directory, let's check that one too:
lrwxrwxrwx 1 root root 15 jan 1 21:50 /etc/alternatives/php -> /usr/bin/php8.3
Again we have a link to another directory so let's see what's in this new directory:
ls /usr/bin/php*
/usr/bin/php /usr/bin/php8.3 /usr/bin/php7.4
We have all versions of PHP installed. Let's change it so that the system uses PHP version 7.4. Use the following command:
sudo update-alternatives --config php
There are 2 choices for the alternative php (providing /usr/bin/php).
Selection Path Priority Status
------------------------------------------------------------
* 0 /usr/bin/php8.3 83 auto mode
1 /usr/bin/php7.4 74 manual mode
2 /usr/bin/php8.3 83 manual mode
Press <enter> to keep the current choice[*], or type selection number:
Select the number that corresponds to the version you want to use.
Run the command again:
php -v
PHP 7.4 (cli) (built: Dec 21 2023 20:12:13) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.3.1, Copyright (c) Zend Technologies
with Zend OPcache v8.3.1, Copyright (c), by Zend Technologies
with Xdebug v3.3.0, Copyright (c) 2002-2023, by Derick Rethans
So the system is using PHP version 7.4
.
Don't forget to add a reaction if the post has helped.
Top comments (8)
In Debian/Ubuntu, anyway :)
Super important info before any upgrade, check your code compatibility, test in your staging site or local environment the version you are going to upgrade, to avoid down time, also if you are using NGINX with PHP-FPM, there is an extra step to do and that is change your site config file to PHP-FPM version you upgrade and restart the service...
Thank you for the information
Helpful post. Liked it.
I'm glad you found it helpful.
GOOD but how you make the change so that apache or nginx see you changed php?
phpinfo() sometimes will show you the old version
When you change the PHP version, Apache or Nginx may not immediately recognize the update. This usually happens if the web server is still using an older version of PHP, even though the new one is installed. Here's how to ensure the change is correctly applied:
Steps for Apache
Check the installed PHP versions:
Disable the old PHP module:
Replace
X.Y
with the version you want to disable (e.g.,php7.4
).Enable the new PHP module:
Replace
Z.W
with the version you want to enable (e.g.,php8.2
).Restart Apache:
Verify using
phpinfo()
: Create or update yourphpinfo.php
file in the web server's document root:Steps for Nginx
Modify the PHP-FPM socket:
Edit the Nginx configuration file (commonly
/etc/nginx/sites-available/default
) and update thefastcgi_pass
directive to point to the new PHP-FPM version socket. For example:Restart PHP-FPM service:
Restart Nginx:
Verify using
phpinfo()
: Create or update yourphpinfo.php
file in the web server's document root:Troubleshooting Tips
Thanks for the clear info