Applies to: Ubuntu 26.04 LTS (PHP 8.5.4, Nginx 1.28.3) and Ubuntu 24.04 LTS (PHP 8.3.6); tested on both

To install PHP on Ubuntu, pick the package that matches how you will use it:

# command line only (scripts, cron jobs, Composer)
sudo apt update
sudo apt install php-cli

# for a website with Nginx (or Apache with PHP-FPM)
sudo apt install php-fpm

# for a website with Apache and mod_php
sudo apt install php libapache2-mod-php

Then check the version with php -v. Ubuntu’s own repositories carry one PHP version per release: PHP 8.5 on Ubuntu 26.04 and PHP 8.3 on 24.04.

$ php -v
PHP 8.5.4 (cli) (built: Sep  2 2026 14:42:18) (NTS)
Copyright (c) The PHP Group
Built by Ubuntu
Zend Engine v4.5.4, Copyright (c) Zend Technologies
    with Zend OPcache v8.5.4, Copyright (c), by Zend Technologies

Watch out: apt install php also installs Apache

The php package needs a way to serve web pages, and the first choice in its dependencies is Apache’s PHP module. A simulated apt install php on a clean system pulled in apache2 and libapache2-mod-php on both releases. That is fine if you want Apache, but on an Nginx server Apache would then try to take port 80 as well. On an Nginx server, install php-fpm instead.

Install the extensions most applications need

WordPress, Laravel and most PHP software need a few extensions beyond the basic set. Install them with the version-less names, which always match the PHP version of your release:

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

Check what is loaded:

$ php -m | grep -iE 'mysqli|pdo_mysql|curl|mbstring|zip|gd|intl'
curl
gd
intl
mbstring
mysqli
pdo_mysql
zip

Search for other extensions with apt search php8.5- (or php8.3- on 24.04). The commands phpenmod and phpdismod switch an installed extension off or on again, for example sudo phpdismod ffi and sudo phpenmod ffi. After installing or changing an extension, restart PHP-FPM or Apache so the website picks it up.

Connect PHP-FPM to Nginx

PHP-FPM runs as its own service (php8.5-fpm on 26.04, php8.3-fpm on 24.04) and listens on a socket, /run/php/php8.5-fpm.sock. Pass PHP files to it in your server block:

server {
    listen 80;
    server_name example.com;
    root /var/www/html;
    index index.php index.html;

    location / {
        try_files $uri $uri/ =404;
    }

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

Test and reload with sudo nginx -t && sudo systemctl reload nginx. With a test file containing <?php echo PHP_VERSION, " via ", php_sapi_name();, our server answered PHP 8.5.4 via fpm-fcgi. If Nginx shows 502 Bad Gateway instead, check that the socket path in fastcgi_pass matches your PHP version and that the FPM service runs: systemctl status php8.5-fpm. A config copied from a 24.04 server gave us exactly that, with this line in /var/log/nginx/error.log:

connect() to unix:/run/php/php8.3-fpm.sock failed (2: No such file or directory) while connecting to upstream

Use PHP-FPM with Apache

When Apache is already installed, the php-fpm package doesn’t switch Apache over by itself. It prints what to do:

NOTICE: Not enabling PHP 8.5 FPM by default.
NOTICE: To enable PHP 8.5 FPM in Apache2 do:
NOTICE: a2enmod proxy_fcgi setenvif
NOTICE: a2enconf php8.5-fpm

Run those two commands and reload Apache with sudo systemctl reload apache2. If libapache2-mod-php is also installed, disable it (sudo a2dismod php8.5) so only one of the two handles PHP.

Where the settings are

Each way of running PHP has its own php.ini:

  • /etc/php/8.5/cli/php.ini: the command line
  • /etc/php/8.5/fpm/php.ini: PHP-FPM
  • /etc/php/8.5/apache2/php.ini: mod_php

(8.3 instead of 8.5 on 24.04.) php --ini shows which file the command line uses. The defaults differ: the FPM file had memory_limit = 128M, the CLI file memory_limit = -1 (no limit), and both upload_max_filesize = 2M and post_max_size = 8M. To change them for a website, edit the FPM or Apache file and restart that service; see increasing the PHP memory limit and the PHP upload limit.

Which PHP version is supported?

According to php.net, PHP 8.5 gets security fixes until 31 December 2029 and PHP 8.3 until 31 December 2027. If an application needs a version your Ubuntu release doesn’t ship, you need a third-party package source, or a server on a newer release. To hide the PHP version from visitors, see hiding Nginx and PHP versions.

Official documentation: PHP supported versions and installing PHP on Debian-based systems.

Related services

KVM VPS hosting

NVMe SSD and full root access, with Free Basic Managed Support.

Unmanaged dedicated servers

full root access and free IPMI KVM, with Free Basic Managed Support.

Managed dedicated servers

our engineers look after the server for you, from $135 a month.

Was this answer helpful?

Still stuck? Open a ticket