Operating System Ubuntu

How to Install and Configure PHP 8.3 on Ubuntu 24.04 (Replacing PHP 7.0/7.1)

Install and configure PHP 8.3 on Ubuntu 24.04 with Apache or Nginx (PHP-FPM), and add other supported PHP branches when an application needs them.

PHP 7.0 and 7.1 reached end of life years ago, and so did Ubuntu 16.04. This guide installs and configures a supported PHP on Ubuntu 24.04 LTS: PHP 8.3 from the Ubuntu archive, with either Apache or Nginx, plus the steps to add a different supported branch (8.2, 8.4 or 8.5) from a third-party repository when an application needs it. The same commands work on Ubuntu 22.04, where the default version is PHP 8.1.

Which PHP version to use

The PHP project supports each branch for four years: two years of bug fixes, then two years of security fixes only. According to php.net's supported-versions page, these branches are supported at the time of writing:

BranchBug fixes untilSecurity fixes until
8.2ended 31 Dec 202431 Dec 2026
8.3ended 31 Dec 202531 Dec 2027
8.431 Dec 202631 Dec 2028
8.531 Dec 202731 Dec 2029

Everything older, including all of PHP 7, receives no fixes from the PHP project. If an application still requires PHP 7.0 or 7.1, upgrade the application rather than the other way round.

Ubuntu 24.04 ships PHP 8.3, so the distribution package is the right default. Use another branch only when an application requires it.

Requirements

  • A server running Ubuntu 24.04 LTS (or 22.04 LTS).
  • A user with sudo rights. The examples use deploy.

Update the package index before you start:

sudo apt update

Install PHP 8.3 with Apache

The simplest Apache setup uses mod_php, which runs PHP inside the Apache processes:

sudo apt install -y apache2 php libapache2-mod-php

The php package is a metapackage that depends on the release's default version, so on 24.04 this installs php8.3 and the matching Apache module. Add the extensions your application needs. A typical set for a database-backed web application:

sudo apt install -y php-cli php-mysql php-mbstring php-xml php-gd php-intl php-zip php-curl

Restart Apache so that it loads the module and the extensions:

sudo systemctl restart apache2

The old php7.0-mcrypt package has no equivalent: the mcrypt extension was removed from PHP in 7.2. Current applications use the OpenSSL or Sodium extensions instead.

Install PHP 8.3 with Nginx (PHP-FPM)

Nginx has no PHP module. It passes requests to PHP-FPM, a separate service. Install php-fpm rather than php, so that Apache is not pulled in as a dependency:

sudo apt install -y nginx php-fpm php-cli php-mysql php-mbstring php-xml php-gd php-intl php-zip php-curl

PHP-FPM listens on a Unix socket, /run/php/php8.3-fpm.sock. Tell Nginx to send .php requests there by adding these lines inside the server { } block of your site, for example in /etc/nginx/sites-available/default:

index index.php index.html;

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

Check the configuration and reload Nginx:

sudo nginx -t
sudo systemctl reload nginx

Verify the installation

Check the command-line version and the loaded extensions:

php -v
php -m

To test PHP through the web server, create a small test file in the document root:

echo '<?php phpinfo();' | sudo tee /var/www/html/info.php

Open http://203.0.113.10/info.php in a browser, replacing the address with your server's. The page shows the PHP version, the server API and the path of the loaded php.ini. Delete the file afterwards, because it exposes details about your server:

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

Configure PHP

Each way of running PHP has its own php.ini, so a change in one does not affect the others:

Used byFile
Apache mod_php/etc/php/8.3/apache2/php.ini
PHP-FPM (Nginx)/etc/php/8.3/fpm/php.ini
Command line/etc/php/8.3/cli/php.ini

Edit the file that matches your setup:

sudo nano /etc/php/8.3/fpm/php.ini

Settings that most often need changing on a new server:

memory_limit = 256M
upload_max_filesize = 32M
post_max_size = 32M
max_execution_time = 60
date.timezone = UTC

Keep post_max_size at least as large as upload_max_filesize. On Nginx, also raise client_max_body_size in the Nginx configuration, or large uploads are rejected before they reach PHP.

PHP-FPM process settings (the user, the socket, the number of worker processes) are in /etc/php/8.3/fpm/pool.d/www.conf.

Apply the changes by restarting the service that runs PHP. For Apache:

sudo systemctl restart apache2

For PHP-FPM:

sudo systemctl restart php8.3-fpm

Extensions are switched on and off with phpenmod and phpdismod, for example sudo phpenmod mbstring, followed by the same restart.

Install a different PHP version

The Ubuntu archive carries one PHP version per release. Other branches are packaged by Ondřej Surý in the ppa:ondrej/php repository, and install side by side under /etc/php/<version>/. This is a third-party repository that Ubuntu does not support, so add it only if you need it.

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

The PPA page states that it still serves Ubuntu 22.04 and 24.04, and that the packages are being merged into the maintainer's own repository at packages.sury.org. Read the notice on that page before adding the PPA, in case the instructions for your release have changed.

Install the branch you need with versioned package names. For PHP 8.4 with PHP-FPM:

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

List what is available for a branch with:

apt-cache search --names-only '^php8\.4'

Each PHP-FPM version runs as its own service with its own socket (php8.4-fpm, /run/php/php8.4-fpm.sock). Point a site at another version by changing the fastcgi_pass line in its Nginx configuration and reloading Nginx. With Apache mod_php, only one version can be loaded at a time:

sudo apt install -y libapache2-mod-php8.4
sudo a2dismod php8.3
sudo a2enmod php8.4
sudo systemctl restart apache2

The php command is managed by the alternatives system. Choose which version it runs:

sudo update-alternatives --set php /usr/bin/php8.4

Keep PHP updated

PHP security fixes arrive as normal package updates:

sudo apt update
sudo apt upgrade

A package update stays within the same branch. Moving to a newer branch, for example away from 8.2 before its security support ends, means installing the new versioned packages, copying your php.ini changes across, switching the web server over and then removing the old packages.

Frequently asked questions

Can I still install PHP 7.0 or 7.1?

Not from the Ubuntu 24.04 archive, and you should not run them on a public server: neither branch has received security fixes for years. If old code cannot be upgraded yet, isolate it and plan the migration. The PHP manual has a migration guide for each version that lists the incompatible changes.

Should I use mod_php or PHP-FPM with Apache?

mod_php is the quickest to set up. PHP-FPM runs PHP as a separate service, which lets you run different PHP versions or system users per site. For a single small site either works; for several sites on one server PHP-FPM is the better fit.

Why does php -v show a different version from my website?

The command line and the web server are configured independently. php -v reports the version selected by update-alternatives, while the site uses whichever Apache module is enabled or whichever PHP-FPM socket its configuration points to. Check the web side with a phpinfo() page.

Where do I check when a PHP version stops being supported?

The supported versions page on php.net lists every branch with the dates on which bug-fix and security support end. Check it before you choose a branch for a new project.

Share story:

Continue reading other guides on How VPS.