How to Install PHP 8.4 on Ubuntu with Nginx and PHP-FPM

How to Install PHP 8.4 on Ubuntu with Nginx and PHP-FPM

PHP 8.4 brings property hooks, asymmetric visibility, and a few performance gains over 8.3, but Ubuntu 24.04 ships with PHP 8.3 in its default repos. To install PHP 8.4 on Ubuntu you need a third-party PPA, then wire it into Nginx through PHP-FPM. Below is the whole setup on a fresh Ubuntu 24.04 server: the PPA, the install, the Nginx wiring, and the tests. The steps also work on Ubuntu 22.04.

Before You Start: What You Need

You need a server running Ubuntu 22.04 or 24.04, a non-root user with sudo access, and Nginx already installed. If Nginx is not there yet, run sudo apt install nginx first and confirm it serves its default page. You also need about 10 minutes and a stable SSH session, because the install pulls in quite a few packages. Back up before you touch a live server. A PHP version change can break a site that depends on a changed function, so test on a staging copy first when you can. If your site still runs on PHP 8.1 or 8.2, read the migration notes in the PHP manual for removed functions before you switch.

Also check what is already on the box with php -v and nginx -v. On stock Ubuntu 24.04, php -v returns “command not found” or shows PHP 8.3. The PPA install does not remove the old version, so both can sit side by side, and Nginx uses whichever PHP-FPM socket you point it at. A leftover 8.3 pool can keep serving requests if you forget to update the Nginx config, so note the socket path, usually /run/php/php8.4-fpm.sock.

Add the Ondrej PHP PPA

Ubuntu’s default repos do not carry PHP 8.4, so add the Ondrej Sury PPA, the source most Ubuntu users rely on for newer PHP builds. First install the helper tools, then add the repo:

sudo apt update
sudo apt install -y software-properties-common
sudo add-apt-repository ppa:ondrej/php -y
sudo apt update

The first apt update refreshes the package lists, and the second one picks up the new PPA. The ubuntu php 8.4 ppa is maintained by the Debian PHP maintainer, which is why it is the usual pick over random third-party repos, but it is still a third-party source. Pin it only on servers you manage yourself, and keep an eye on its updates the same way you watch Ubuntu security updates. If add-apt-repository fails with a key error, check that your server can reach the PPA over HTTPS and that your system clock is correct, since a wrong clock breaks certificate checks.

Install PHP 8.4 on Ubuntu: Packages and Extensions

With the PPA in place, install PHP 8.4 with FPM and the extensions most WordPress and Laravel sites need:

sudo apt install -y php8.4-fpm php8.4-cli php8.4-common php8.4-mysql php8.4-xml php8.4-curl php8.4-gd php8.4-mbstring php8.4-zip php8.4-intl php8.4-bcmath php8.4-opcache php8.4-readline

Each package has a job. php8.4-fpm is the process manager Nginx talks to. php8.4-mysql connects to MySQL and MariaDB. php8.4-gd handles image resizing, php8.4-intl covers internationalisation, and php8.4-opcache caches compiled scripts in memory so repeat requests run faster. Skip extensions your app never uses, since every extra module adds load time and a wider surface for bugs. A plain static site with one contact form needs far fewer packages than a WooCommerce store. After the install, confirm the version with php8.4 -v and check the service with sudo systemctl status php8.4-fpm. It should show active and running.

Configure PHP-FPM for Nginx

PHP-FPM listens on a Unix socket by default, and Nginx passes PHP requests to that socket. Open the pool config at /etc/php/8.4/fpm/pool.d/www.conf and check the listen line. It should read listen = /run/php/php8.4-fpm.sock. On a standard php-fpm ubuntu 24.04 install, the nginx php-fpm configuration works best with a socket rather than a TCP port on the same machine, since sockets skip the network stack and are a little faster. Next, look at the process manager settings. The defaults use pm = dynamic with a small pool, which suits a low-traffic site. On a 2 GB VPS, keep pm.max_children low, around 5 to 10, so PHP workers cannot eat all your RAM. If you need help sizing the pool for a small server, the guide on tuning PHP-FPM pm settings for a 2 GB VPS walks through the numbers. After any edit, restart the service with sudo systemctl restart php8.4-fpm.

Point Nginx at PHP 8.4

Now tell Nginx to send PHP files to the 8.4 socket. Open your server block, usually in /etc/nginx/sites-available/default or a file under /etc/nginx/sites-available/, and find the PHP location block. It should look like this:

location ~ .php$ {
    include snippets/fastcgi-php.conf;
    fastcgi_pass unix:/run/php/php8.4-fpm.sock;
}

If the block still points at php8.3-fpm.sock or php8.2-fpm.sock, change the version number and save. That one line is the heart of the php 8.4 nginx php-fpm wiring. If you are building the server block from scratch, the walkthrough for setting up Nginx server blocks on Ubuntu 24.04 covers the full file layout. Then test the config with sudo nginx -t and reload with sudo systemctl reload nginx. A reload, not a restart, keeps existing connections alive while Nginx picks up the new config. If nginx -t reports an error, fix the file before reloading, or Nginx keeps serving the old config and your change never takes effect.

Test That PHP 8.4 Works

Create a small test file in your web root, for example /var/www/html/info.php, with one line: <?php phpinfo(); ?>. Visit http://your-server-ip/info.php in a browser. The page should show PHP Version 8.4 and the loaded config file path. Check the extensions list on that page too, and confirm the ones your app needs are present. Once it works, delete the file right away with sudo rm /var/www/html/info.php. Leaving phpinfo() public hands strangers a full map of your server: versions, paths, and loaded modules. For a real check, put up a tiny script that prints the version and one database query instead, or test with your actual site and watch the Nginx error log at /var/log/nginx/error.log for PHP errors.

Tune OPcache and Common PHP Settings

Stock PHP settings are safe defaults, not fast ones. Open /etc/php/8.4/fpm/php.ini and adjust a few values to fit your server. Set memory_limit to 256M for a typical WordPress site, upload_max_filesize to 64M if you upload media, and post_max_size slightly above that. Then open /etc/php/8.4/fpm/conf.d/10-opcache.ini and make sure OPcache is on with opcache.enable=1 and a memory size your server can spare, such as 128 MB on a 2 GB VPS. The OPcache setup guide for PHP 8.3 on Ubuntu still applies to 8.4, since the settings did not change between the two releases. After editing, restart PHP-FPM. Change one value at a time, test the site, and note what you changed so you can undo it.

Run Two PHP Versions Side by Side

Sometimes one site needs 8.4 while another still needs 8.3. You can run both, since each version has its own FPM pool and socket. Install the second version the same way, then point each Nginx server block at its own socket: one at php8.4-fpm.sock, the other at php8.3-fpm.sock. Keep the pools separate and give each its own pm.max_children budget, or the two pools will fight over RAM. The detailed steps for running two PHP versions on one VPS with Nginx and PHP-FPM cover the pool files and the per-site Nginx blocks. This setup is useful during a migration, when you move sites one at a time and keep the old version as a fallback. Once the last site moves, remove the old PHP packages with sudo apt remove php8.3* and delete its pool to avoid confusion later.

Common Problems and Fixes

Most failures after this install fall into a few buckets. A 502 Bad Gateway usually means Nginx points at the wrong socket or PHP-FPM is not running, so check sudo systemctl status php8.4-fpm and match the socket path in the Nginx block. A blank page often means a PHP fatal error, so check /var/log/php/8.4/fpm.log and the Nginx error log. A “file not found” error from FastCGI usually means the root path in the server block does not match the real web root, or the SCRIPT_FILENAME parameter is wrong. Missing extension errors, such as “Call to undefined function mb_strlen()”, mean you forgot a package, so install it with sudo apt install php8.4-mbstring and restart FPM. If the site worked on 8.3 and breaks on 8.4, check the error against the PHP 8.4 migration guide before rolling back, since the fix is often one line. If you are building a full LEMP stack around this install, the WordPress LEMP stack guide for Nginx, PHP, and MariaDB shows how the pieces fit together. Keep the test file deleted, keep the PPA updated, and check the logs after the first real traffic hits.

Ditulis oleh

Firdaus Safari Putra

Founder, PT FirKaizen Digital Solutions

Firdaus Safari Putra adalah founder PT FirKaizen Digital Solutions. Ia membangun dan mengoperasikan infrastruktur server di balik panduan ini, dan menulis berdasarkan apa yang benar-benar ia jalankan, uji, dan ukur — bukan teori.