Install or Upgrade to PHP 8.4 in Ubuntu 24.04 | 22.04

Last updated: November 28, 2024 — Leave a comment

PHP, the popular scripting language for web development, announced new 8.4 release last week. Here’s how to install or upgrade to Php 8.4 in Ubuntu 22.04 or Ubuntu 24.04 to work with Apache2 or Nginx web server.

PHP 8.4 introduced a number of new features such as property hooks, asymmetric visibility, lazy objects, an object API for BCMath, new array functions, and new JIT implementation based on IR Framework. See the release page for more.

Why you need to upgrade PHP

The most obvious reason to upgrade php is security! Older versions are more likely to have security vulnerabilities and outdated code that is not longer supported.

Newer versions may support new features and functionality great for web development. It usually includes performance improvements to make your website load faster!

And, you can generally expect to see a decrease in memory usage when upgrading php from older to newer versions, which is really helpful for server with small amount of RAM.

Step 1: Install PHP 8.4 in Ubuntu 22.04 | 24.04

PHP 8.4 is easy to install in all current Ubuntu LTS releases through Ondrej Sury’s PPA, which is practically “official”, as the maintainer also manages the official php package in Debian.

NOTE 1: the PPA also supports Ubuntu 20.04 LTS. However, the amd64 build is broken at the moment of writing. Check the link above before trying this on 20.04.

NOTE 2: If you’re trying to upgrade from PHP 7.x or even older. You needs to do some searches first, as it may break your site!!

1. First, connect to your Ubuntu server, then run command to install the package for the add-apt-repository tool.

sudo apt install software-properties-common

2. Next, run command to add the PPA repository and hit Enter to continue:

LC_ALL=C.UTF-8 sudo add-apt-repository ppa:ondrej/php

3. Adding PPA now automatically refresh package cache. Just in case, you may run sudo apt update to update cache manually.

Finally, install PHP 8.4 depends on your web server:

  • To install php 8.4 for Apache 2, use command:
    sudo apt install php8.4 libapache2-mod-php8.4
  • Or run this command instead for Nginx:
    sudo apt install php8.4 php8.4-fpm

4. You may also need to install some modules, e.g., php8.4-tidy, php8.4-mysql, php8.4-xml, php8.4-zip, and more by running command:

sudo apt install php8.4-tidy php8.4-mysql php8.4-xml php8.4-zip

If you’re trying to upgrade php, and don’t even know what modules you need to install, then run command apt list --installed |grep php to tell all the modules you installed for old php version, and install the corresponding ones for php8.4.

Step 2: Apply Php 8.4 for Apache 2 or Nginx

After installed php 8.4, you also need to do something to make your web server to take use the new php version.

For Apache 2, if you have an old php in use, then run the command below to disable it:

sudo a2dismod php8.3

In command replace php8.3 with current running php version. For Ubuntu 22.04, it may be php8.1.

Then, enable the new php version via command:

sudo a2enmod php8.4

Finally, replace the apache2 service to apply change:

sudo systemctl restart apache2.service

For Nginx, you may first run the command below to configure php-fpm:

sudo nano /etc/php/8.4/fpm/pool.d/www.conf

If upgrading from old php, you may just edit according to /etc/php/8.3/fpm/pool.d/www.conf file, though replace number 8.3 accordingly.

Then, edit the nginx configuration file for you website:

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

Here, replace default depends on which file in the directory you use for your website.

When file opens, scroll down and find out the location ~ \.php$ {} section, and set fastcgi_pass unix:/run/php/php8.4-fpm.sock; If you configured php-fpm to listen on the TCP port, then you may just leave the file unchanged.

Finally, press Ctrl+S to save file and Ctrl+X to exit. And, restart both nginx and php-fpm to apply changes:

sudo systemctl restart nginx php8.4-fpm

Also, stop the old php-fpm service (replace number 8.3 accordingly):

sudo systemctl stop php8.3-fpm

Verify:

To verify if the new php 8.4 is working, just create a simple php file under your domain’s root directory:

sudo nano /var/www/html/info.php

Here replace /var/www/html if you set the domain root to another directory.

When file opens, write the 3 lines below then save (press Ctrl+S, then Ctrl+X)

<?php
phpinfo ();
?>

Finally, visit either localhost/info.php from server itself or your_domain/info.php in web browser to check php details.

Twitter

I'm a freelance blogger who started using Ubuntu in 2007 and wishes to share my experiences and some useful tips with Ubuntu beginners and lovers. Please comment to let me know if the tutorial is outdated! And, notify me if you find any typo/grammar/language mistakes. English is not my native language. Contact me via [email protected] Buy me a coffee: https://ko-fi.com/ubuntuhandbook1 |

No Comments

Be the first to start the conversation.

Leave a Reply

Text formatting is available via select HTML.

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> 

*