
🐘 How to install PHP on Ubuntu 26.04 and 24.04: versions 8.4 and 8.5 with Nginx and Apache
PHP, the language that powers more than half the web. WordPress, Laravel, Magento, Drupal: all built on PHP. When your server delivers a page in 200 ms instead of 800, it usually comes down to the PHP version and how it is configured.
The problem is that Ubuntu's default repositories always lag behind. Ubuntu 26.04 ships with PHP 8.5 by default (current as of June 2026). But if your server still runs Ubuntu 24.04, the repository only offers PHP 8.3. Meanwhile, PHP 8.5 delivers up to 25-30% better performance compared to 8.2, plus new capabilities: asynchronous I/O, an improved JIT compiler, and native WebSocket support.
Below is a proven method for installing the latest PHP on Ubuntu 26.04 (or 24.04) and linking it with Nginx or Apache. Every command has been tested on clean installations; in practice, these steps cover the vast majority of scenarios.
💡 Quick overview:
- Identify your Ubuntu version: on 26.04 PHP 8.5 installs from the standard repository with a single command; on 24.04 you need a PPA
- Choose your web server: Apache (module or PHP-FPM) or Nginx (PHP-FPM only)
- Install extensions with one package manager and verify everything works via a test
info.php - Delete
info.phpimmediately after verification; it is a data leak on a live server
PHP 8.5 on Ubuntu 26.04: two minutes and done
Ubuntu 26.04 LTS shipped in April 2026 and includes PHP 8.5 in the main repositories. No PPAs, no third-party keys: installation takes exactly two commands.
But if your server runs Ubuntu 24.04, keep reading: PHP 8.5 installs just as easily via a PPA, and you can run multiple versions side by side.
First, a quick table of PHP version status as of June 2026:
Version | Active support until | Security support until | Key features | Who should use it |
|---|---|---|---|---|
PHP 8.3 | Dec 2025 (ended) | Dec 2027 | Typed class constants, | Production on WordPress/Laravel with full compatibility |
PHP 8.4 | Dec 2026 | Dec 2028 | Lazy objects, property hooks, async parser | Projects on Laravel 11+ and new development |
PHP 8.5 | Dec 2027 | Dec 2029 | Async I/O, native WebSockets, PIE extensions, improved JIT | New projects and those seeking maximum performance |
For the vast majority of sites on WordPress, Magento, or Drupal I recommend PHP 8.4: it is one and a half to two times faster than 8.1 on real pages, compatible with all modern plugins, and receives active support through the end of 2026. PHP 8.5 is for those starting a new project or who know for certain that their codebase is compatible.
Installing PHP with Apache
Apache works with PHP in two ways: as a built-in module or through PHP-FPM. The module is simpler; FPM is faster and more memory-efficient.
PHP as an Apache module
On Ubuntu 26.04, installation takes a single command:
1 sudo apt update 2 sudo apt install -y php libapache2-mod-php
On Ubuntu 24.04, first add the PPA (Ondřej Surý's repository contains all current PHP versions and is updated on official release day):
1 sudo apt update 2 sudo apt install -y software-properties-common 3 sudo add-apt-repository -y ppa:ondrej/php 4 sudo apt update 5 sudo apt install -y php8.4 libapache2-mod-php8.4
After installation, restart Apache so the module loads:
1 sudo systemctl restart apache2
Apache now processes .php files through the built-in module. You can verify the active version with php -v.
PHP-FPM for Apache
PHP-FPM (FastCGI Process Manager) is a separate process that maintains a pool of ready PHP workers. Apache communicates with it via a socket, avoiding the need to load the PHP interpreter into every thread. In practice this produces noticeable memory savings on servers with many virtual hosts.
Ubuntu 26.04:
1 sudo apt install -y php-fpm
Ubuntu 24.04 (after adding the PPA, see above):
1 sudo apt install -y php8.4-fpm
By default, PHP-FPM is not enabled in Apache. Enable it:
Ubuntu 26.04:
1 sudo a2enmod proxy_fcgi setenvif 2 sudo a2enconf php-fpm 3 sudo systemctl restart apache2
Ubuntu 24.04:
1 sudo a2enmod proxy_fcgi setenvif 2 sudo a2enconf php8.4-fpm 3 sudo systemctl restart apache2
The first command enables the FastCGI proxy modules, the second activates the PHP-FPM configuration for Apache, and the third applies the changes. After this, Apache routes PHP requests to the socket: /run/php/php-fpm.sock on 26.04 or /run/php/php8.4-fpm.sock on 24.04.
Installing PHP with Nginx
Nginx cannot process PHP on its own; it needs an external handler. The Nginx + PHP-FPM stack is the de facto standard for high-traffic sites.
Ubuntu 26.04:
1 sudo apt install -y php-fpm
Ubuntu 24.04 (after adding the PPA):
1 sudo apt install -y php8.4-fpm
The FPM service starts automatically. Verify:
1 systemctl status php8.4-fpm
On Ubuntu 26.04, use this instead:
1 systemctl status php-fpm
You should see active (running). Now edit your Nginx server block. Inside the server {} section, add PHP handling:
Ubuntu 26.04:
1 server { 2 3 location ~ \.php$ { 4 include snippets/fastcgi-php.conf; 5 fastcgi_pass unix:/run/php/php-fpm.sock; 6 } 7 }
Ubuntu 24.04:
1 server { 2 3 location ~ \.php$ { 4 include snippets/fastcgi-php.conf; 5 fastcgi_pass unix:/run/php/php8.4-fpm.sock; 6 } 7 }
After modifying the configuration, restart Nginx:
1 sudo nginx -t && sudo systemctl restart nginx
nginx -t validates the configuration first, so a syntax error will not bring down the server.
Installing PHP extensions
PHP extensions are compiled libraries that add functions for interacting with databases, graphics, caches, and dozens of other systems.
On Ubuntu 26.04, packages follow the naming pattern php-extension-name:
1 sudo apt install -y php-mysql php-gd php-curl php-mbstring php-xml php-zip php-intl php-opcache php-bcmath
On Ubuntu 24.04 (via the PPA), packages are named php8.4-extension-name:
1 sudo apt install -y php8.4-mysql php8.4-gd php8.4-curl php8.4-mbstring php8.4-xml php8.4-zip php8.4-intl php8.4-opcache php8.4-bcmath
What each one provides:
mysql: working with MySQL/MariaDB (PDO + mysqli);gd: image processing (WordPress thumbnails);curl: HTTP requests from code;mbstring: working with UTF-8 and multibyte strings;xml: parsing XML (RSS, sitemaps);zip: archiving (plugin updates, backups);intl: internationalization (dates, currencies, translations);opcache: bytecode cache (mandatory for performance);bcmath: precise math (required by some ecommerce plugins).
For a WordPress site in 2026, add imagick (handles WebP faster than GD) and redis (object cache) to the base set:
1 sudo apt install -y php-imagick php-redis
On Ubuntu 26.04 the command looks exactly like this. For Ubuntu 24.04:
1 sudo apt install -y php8.4-imagick php8.4-redis
After installing extensions, restart the PHP handler:
1 sudo systemctl restart apache2
For PHP-FPM (with Nginx or Apache in FPM mode):
1 sudo systemctl restart php8.4-fpm
On Ubuntu 26.04 the service is named differently:
1 sudo systemctl restart php-fpm
Verifying PHP processing
Create a test file in the web server root:
1 echo "<?php phpinfo(); ?>" | sudo tee /var/www/html/info.php
Open http://<server-IP-address>/info.php in a browser. You should see a purple table with PHP information: version, loaded extensions, memory settings, and configuration file paths. Make sure the "Loaded Configuration File" row shows the correct php.ini.
After verification, delete the test file; phpinfo() on a live site is a data leak exposing versions and paths:
1 sudo rm /var/www/html/info.php
How to run multiple PHP versions on one server
Different PHP versions from Ondřej Surý's repository coexist without issues. You can run PHP 7.4, 8.3, and 8.4 simultaneously and switch sites between them.
Installing an additional version (for example, PHP 8.3 alongside 8.4):
1 sudo apt install -y php8.3 php8.3-fpm php8.3-mysql php8.3-gd php8.3-curl php8.3-mbstring php8.3-xml php8.3-zip php8.3-intl
Each version runs on its own socket: /run/php/php8.3-fpm.sock, /run/php/php8.4-fpm.sock. In the Nginx server block, simply specify the desired socket in fastcgi_pass, and that particular site will use the chosen version.
Switching the PHP version on the command line:
1 sudo update-alternatives --config php
This is convenient for gradual migration: move one site to 8.4, verify it works, then move the next.
⁉️🤔 Frequently asked questions
Do I need to remove the old PHP version before installing a new one?
No. Different PHP versions from the Ondřej Surý PPA coexist without conflicts. You can install 8.4 alongside 8.1 and switch sites one at a time using
sudo update-alternatives --config php. On Ubuntu 26.04, system PHP 8.5 also does not interfere with installing other versions via the PPA.
What should I do if Nginx returns 502 Bad Gateway after configuring PHP?
A 502 with Nginx + PHP-FPM almost always means Nginx cannot reach the PHP-FPM socket. Check three things: is the service running (
systemctl status php-fpmorsystemctl status php8.4-fpm), does the socket path infastcgi_passmatch the actual file (ls -la /run/php/), and does the user running Nginx (www-data) match the socket owner. In most cases,sudo systemctl restart php-fpmfixes the problem.
Can I install PHP 8.5 on Ubuntu 24.04?
Yes, via the Ondřej Surý PPA:
sudo apt install -y php8.5 php8.5-fpm(after adding the repository). PHP 8.5 will run on its own socket/run/php/php8.5-fpm.sockalongside other versions. But note: if your plugins or framework have not been tested under 8.5, stick with 8.4, which is safer for production.
Which PHP extensions are critical for WordPress in 2026?
The minimum set: mysql, gd, curl, mbstring, xml, zip, intl, and opcache. For performance, add redis (object cache) and imagick (processes images faster than GD, especially WebP). If your site works with WebP, imagick is mandatory: GD in older builds does not support WebP resizing.
Is it worth upgrading Ubuntu from 24.04 to 26.04 just for PHP 8.5?
If your server runs Ubuntu 24.04, PHP 8.5 is available via the PPA in under a minute; there is no urgent need to upgrade the OS. Standard support for 24.04 ends in April 2029, so there is plenty of time. On 26.04 you get fresh versions of Nginx, MySQL 8.4, and system libraries out of the box, without any PPA. The upgrade process uses
do-release-upgrade, but make a full backup first.
PHP 8.5 changed package names. What is the difference from 8.4?
On Ubuntu 26.04, packages are named php, php-fpm, php-mysql (without a version number) because 8.5 is the only version in the system repository. These are metapackages: php always pulls in the current system version. When installing other versions via the PPA, the old naming scheme applies: php8.4, php8.4-fpm, php8.4-mysql.
PHP on a server in 2026: the bottom line
Installing current PHP on Ubuntu is not rocket science. On 26.04, PHP 8.5 installs with a single command from the system repository. On 24.04, one PPA and two commands give you version 8.4 or 8.5 instead of the outdated 8.3.
The performance difference is real: on the same server, PHP 8.4 processes a WordPress page one and a half to two times faster than 8.1. That means fewer timeout errors and better search rankings (Core Web Vitals are still a ranking factor).
The key takeaway: use PHP-FPM instead of the Apache module, choose the PHP version that matches your site's compatibility requirements (8.4 is the safe choice as of June 2026), and do not forget to restart the service after installing extensions. Install, test, delete info.php.
If this material was useful, watch the video tutorial below showing the same steps live on a clean Ubuntu 26.04.



