DEV Community

Cover image for How to Update PHP in Linux to any version

How to Update PHP in Linux to any version

Antonio Silva on January 13, 2024

Over time, PHP releases a new version, making the previous ones obsolete. This means that they no longer receive updates or security patches, makin...
Collapse
 
dogers profile image
Dogers

In Debian/Ubuntu, anyway :)

Collapse
 
xtreme2020 profile image
José Francisco

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...

Collapse
 
xxzeroxx profile image
Antonio Silva

Thank you for the information

Collapse
 
dev-alamin profile image
Al Amin

Helpful post. Liked it.

Collapse
 
xxzeroxx profile image
Antonio Silva

I'm glad you found it helpful.

Collapse
 
simshard profile image
Simon K

Thanks for the clear info

Collapse
 
detzam profile image
webstuff

GOOD but how you make the change so that apache or nginx see you changed php?
phpinfo() sometimes will show you the old version

Collapse
 
xxzeroxx profile image
Antonio Silva

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:

php -v
Enter fullscreen mode Exit fullscreen mode

Disable the old PHP module:

sudo a2dismod phpX.Y
Enter fullscreen mode Exit fullscreen mode

Replace X.Y with the version you want to disable (e.g., php7.4).

Enable the new PHP module:

sudo a2enmod phpZ.W
Enter fullscreen mode Exit fullscreen mode

Replace Z.W with the version you want to enable (e.g., php8.2).

Restart Apache:

sudo systemctl restart apache2
Enter fullscreen mode Exit fullscreen mode

Verify using phpinfo(): Create or update your phpinfo.php file in the web server's document root:

phpinfo();
Enter fullscreen mode Exit fullscreen mode

Steps for Nginx

Modify the PHP-FPM socket:

Edit the Nginx configuration file (commonly /etc/nginx/sites-available/default) and update the fastcgi_pass directive to point to the new PHP-FPM version socket. For example:

fastcgi_pass unix:/run/php/php8.2-fpm.sock;
Enter fullscreen mode Exit fullscreen mode

Restart PHP-FPM service:

sudo systemctl restart phpX.Y-fpm
Enter fullscreen mode Exit fullscreen mode

Restart Nginx:

sudo systemctl restart nginx
Enter fullscreen mode Exit fullscreen mode

Verify using phpinfo(): Create or update your phpinfo.php file in the web server's document root:


Troubleshooting Tips

  • Clear browser cache: Sometimes, the browser cache may cause you to see old information. Clear your cache or test in an incognito window.
  • Check CLI vs. server versions: The PHP version in the CLI may differ from the one used by the web server. Use the following to confirm the web server version:
phpinfo();
Enter fullscreen mode Exit fullscreen mode
  • Reinstall PHP extensions: Ensure all necessary extensions are installed for the new PHP version.