How to Install PHP on Ubuntu 24.04

PHP is one of the most used server-side programming languages. Many popular CMS and frameworks such as WordPress, Magento, and Laravel are written in PHP.
This guide covers the steps necessary to install PHP on Ubuntu 24.04 and integrate it with Nginx and Apache. The default Ubuntu 24.04 repositories include PHP 8.3. We will also show you how to install other PHP versions.
Quick Reference
For a printable quick reference, see the PHP cheatsheet .
| Task | Command |
|---|---|
| Install PHP 8.3 with Apache | sudo apt install libapache2-mod-php8.3 |
| Install PHP 8.3 with Nginx | sudo apt install php8.3-fpm |
| Install the MySQL extension for PHP 8.3 | sudo apt install php8.3-mysql |
| Check the default CLI version | php -v |
| List modules loaded by the PHP 8.3 CLI | php8.3 -m |
| Restart PHP-FPM | sudo systemctl restart php8.3-fpm |
| Switch PHP version (CLI) | sudo update-alternatives --config php |
| Install PHP 8.5 (Sury repository) | sudo apt install php8.5-fpm |
Prerequisites
To follow this guide, you need to be logged in as a user with sudo privileges .
Installing PHP 8.3 with Apache
If you are using Apache as your web server, install the PHP 8.3 Apache module. The package also installs the PHP 8.3 command-line interpreter.
sudo apt update
sudo apt install libapache2-mod-php8.3Once the packages are installed, restart Apache for the PHP module to get loaded:
sudo systemctl restart apache2Installing PHP 8.3 with Nginx
Unlike Apache, Nginx does not have built-in support for processing PHP files. We will use PHP-FPM (FastCGI Process Manager) to handle PHP files.
Run the following commands to install PHP 8.3 and the PHP-FPM package:
sudo apt update
sudo apt install php8.3-fpmOnce the installation is completed, the FPM service starts automatically. To check the status of the service, run:
sudo systemctl status php8.3-fpm● php8.3-fpm.service - The PHP 8.3 FastCGI Process Manager
Loaded: loaded (/usr/lib/systemd/system/php8.3-fpm.service; enabled; preset: enabled)
Active: active (running) since Thu 2026-12-03 20:54:23 UTC; 4s agoIn the output, enabled means PHP-FPM starts at boot, and active (running) means the service is running now.
Edit the Nginx server block
for your site and add the PHP location shown below. If your site uses index.php as its home page, add it to the existing index directive so a request for / can reach it.
server {
# . . . other code
index index.php index.html;
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php8.3-fpm.sock;
}
}Test the configuration and reload Nginx if the test passes.
sudo nginx -t && sudo systemctl reload nginxInstalling PHP Extensions
PHP extensions are compiled libraries that extend the core functionality of PHP. Use apt
to install the extension packages for your PHP version. For example, install the MySQL and GD extensions for PHP 8.3 with the following command.
sudo apt install php8.3-mysql php8.3-gdHere are some of the most commonly used PHP extensions:
| Extension | Package | Description |
|---|---|---|
| MySQL | php8.3-mysql | MySQL and MariaDB database support |
| cURL | php8.3-curl | URL transfer library |
| GD | php8.3-gd | Image processing |
| Mbstring | php8.3-mbstring | Multibyte string support |
| XML | php8.3-xml | XML parsing and manipulation |
| ZIP | php8.3-zip | ZIP archive support |
| Intl | php8.3-intl | Internationalization functions |
| BCMath | php8.3-bcmath | Arbitrary precision math |
You do not need to install OPcache separately. The PHP 8.3 CLI, FPM, and Apache packages all depend on php8.3-opcache, so APT installs it together with PHP.
To install all commonly needed extensions at once:
sudo apt install php8.3-mysql php8.3-curl php8.3-gd php8.3-mbstring \
php8.3-xml php8.3-zip php8.3-intl php8.3-bcmathAfter installing a new PHP extension, restart Apache or PHP-FPM depending on your setup:
sudo systemctl restart apache2Or for Nginx with PHP-FPM:
sudo systemctl restart php8.3-fpmTo see which modules the PHP 8.3 CLI loads, run the versioned executable.
php8.3 -mTesting PHP Processing
Create a new file named php-check.php in the document root of the site you configured. Use the path in Apache’s DocumentRoot or Nginx’s root directive. For a site rooted at /var/www/html, open the file with the following command.
sudoedit /var/www/html/php-check.phpAdd this code to print the PHP version and the interface your web server uses to run it.
<?php
echo PHP_VERSION, ' (', PHP_SAPI, ')', PHP_EOL;Open /php-check.php on the hostname you configured, for example http://example.com/php-check.php. On a standard Ubuntu 24.04 installation with the Apache PHP module, the page shows the following line.
8.3.6 (apache2handler)The number shows which PHP release served the request. You will see fpm-fcgi with PHP-FPM. After testing, remove the file from your site’s document root. For the example above, use the following command.
sudo rm /var/www/html/php-check.phpInstalling Other PHP Versions
Ondřej Surý, a Debian developer, maintains a repository that includes multiple PHP versions. The ppa:ondrej/php PPA that older guides use is frozen and no longer receives updates. Ondřej now publishes the Ubuntu packages at packages.sury.org.
This is a third-party source, so use it only if you need a different PHP version than what Ubuntu provides. If you already use ppa:ondrej/php, go straight to Migrating from the PPA
. If packages.sury.org is already enabled, skip to Installing PHP 8.5
.
Adding the Sury Repository
For a fresh setup, refresh APT and install the packages needed to download the Sury signing key.
sudo apt update
sudo apt install ca-certificates curlDownload and install the Sury archive keyring.
keyring=$(mktemp --suffix=.deb) &&
curl -fsSLo "$keyring" https://packages.sury.org/debsuryorg-archive-keyring.deb &&
sudo dpkg -i "$keyring" &&
rm -- "$keyring"Add the repository
for noble, the Ubuntu 24.04 codename, and refresh the package index. The Signed-By field tells APT which keyring to use for this source.
sudo tee /etc/apt/sources.list.d/php.sources > /dev/null <<'EOF'
Types: deb
URIs: https://packages.sury.org/php/
Suites: noble
Components: main
Signed-By: /usr/share/keyrings/debsuryorg-archive-keyring.gpg
EOF
sudo apt updateContinue to Installing PHP 8.5 .
Migrating from the PPA
The frozen PPA ships a package that enables the new repository. When you run apt update, APT may ask you to accept the label change to PPA for PHP (deprecated). Accept that change and install the migration package.
sudo apt update
sudo apt install debsuryorg-repository-phpOnce the package has enabled Sury, remove the old PPA and refresh APT.
sudo add-apt-repository --remove ppa:ondrej/php
sudo apt updateYour installed PHP packages stay in place. APT can fetch their next updates from the new repository.
Installing PHP 8.5
Sury carries multiple PHP versions for Ubuntu 24.04. Once you enable it, a system upgrade can replace Ubuntu’s PHP 8.3 packages and libraries such as libgd3 with Sury’s builds. If APT upgrades an unversioned package such as php-fpm or php-mysql to Sury’s version, it can also pull in PHP 8.6. Preview those changes separately with the following command.
sudo apt -s upgradeUse versioned package names to pick a PHP branch. The following command previews installing PHP 8.5 with PHP-FPM and several common extensions. For Apache, use libapache2-mod-php8.5 in place of php8.5-fpm in the preview and install commands.
sudo apt -s install php8.5-fpm php8.5-cli php8.5-gd php8.5-curl \
php8.5-mysql php8.5-mbstring php8.5-xmlThe -s option simulates the transaction. Check what APT would install, upgrade, or remove. When the list looks right, run the installation.
sudo apt install php8.5-fpm php8.5-cli php8.5-gd php8.5-curl \
php8.5-mysql php8.5-mbstring php8.5-xmlPHP 8.5 includes OPcache. For PHP 8.4, substitute 8.4 in the package names above. On an Nginx server, use the versioned FPM package. The plain php8.5 metapackage can pull in Apache when PHP-FPM and CGI are both absent.
Check the installed PHP 8.5 CLI directly, regardless of which version the php command selects.
php8.5 -vSwitching Between PHP Versions
In automatic mode, update-alternatives points the php command to the installed version with the highest priority. With PHP 8.3 and 8.5 installed, php -v reports 8.5 in automatic mode. To set PHP 8.3 as the default CLI version, run the following command.
sudo update-alternatives --set php /usr/bin/php8.3To interactively choose the default version:
sudo update-alternatives --config phpFor Apache, disable the current PHP module and enable the one you want:
sudo a2dismod php8.3
sudo a2enmod php8.5
sudo systemctl restart apache2For Nginx, update the PHP-FPM socket path in your server block to point to the correct version:
fastcgi_pass unix:/run/php/php8.5-fpm.sock;Then test the configuration and reload Nginx.
sudo nginx -t && sudo systemctl reload nginxChecking the PHP Version
To check the version used by the default PHP command, run:
php -vPHP 8.3.6 (cli) (built: Nov 18 2026 23:42:07) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.3.6, Copyright (c) Zend Technologies
with Zend OPcache v8.3.6, Copyright (c), by Zend TechnologiesIn the output, 8.3.6 is the PHP release, and cli means this is the command-line build. To find the version that Apache or PHP-FPM uses, see how to check the PHP version
.
PHP Configuration Files
PHP configuration files are located at:
- Main config:
/etc/php/8.3/cli/php.ini(CLI) and/etc/php/8.3/apache2/php.ini(Apache) or/etc/php/8.3/fpm/php.ini(FPM) - FPM pool config:
/etc/php/8.3/fpm/pool.d/www.conf - Extension configs:
/etc/php/8.3/mods-available/
After changing any configuration, restart the relevant service:
sudo systemctl restart php8.3-fpmUninstalling PHP
To remove PHP and related packages, you can purge them and clean up dependencies. The 'php*' pattern matches every package whose name starts with php, including tools such as phpMyAdmin. To preview what would be removed, run:
sudo apt -s purge 'php*'If the list contains only packages you want to remove, run the purge:
sudo apt purge 'php*'
sudo apt autoremoveTroubleshooting
Unable to locate package php8.5
Ubuntu 24.04 includes only PHP 8.3. Other versions come from the Sury repository. Add it as shown in the Installing Other PHP Versions section and try again.
502 Bad Gateway in Nginx
Nginx cannot reach the PHP-FPM socket set in fastcgi_pass. This usually happens after switching versions when the server block still points to the old socket. It also happens when the FPM service for that version is stopped. List the sockets with ls /run/php/ and compare them with your server block. For other causes, see how to fix 502 Bad Gateway in Nginx
.
FAQ
What PHP version does Ubuntu 24.04 include?
Ubuntu 24.04 ships with PHP 8.3 in its default repositories. The exact upstream version is 8.3.6.
Why does Ubuntu 24.04 report PHP 8.3.6 when newer 8.3 releases exist?
Ubuntu 24.04 stays on PHP 8.3.6 for the life of the release. Security fixes from later 8.3 releases are backported to it. Each round of fixes changes only the Ubuntu part of the package version, such as 8.3.6-0ubuntu0.24.04.14. To see the package version on your system, run apt policy php8.3. If you need the latest upstream 8.3 release, install PHP from the Sury repository.
How do I upgrade PHP 8.3 to PHP 8.5 on Ubuntu 24.04?
Add the Sury repository and install PHP 8.5 next to 8.3 with versioned packages, such as php8.5-fpm and php8.5-mysql. Point your site to the new version and test it. On Nginx, change the PHP-FPM socket in fastcgi_pass. On Apache, disable the php8.3 module and enable php8.5. Once everything works, remove the PHP 8.3 packages.
What is the difference between php and php-fpm?
From Ubuntu’s repositories, the php metapackage installs PHP 8.3 with the Apache module, PHP-FPM, or CGI. On a server that has none of them, APT picks the Apache module. The php-fpm package installs PHP-FPM, the process manager Nginx uses to run PHP code.
Can I run multiple PHP versions at the same time?
Yes. With PHP-FPM, you can install multiple versions from Sury and point each virtual host or server block to the socket for the version it needs.
Do I need to restart Apache or Nginx after installing an extension?
Yes. After installing a PHP extension, restart Apache (sudo systemctl restart apache2) or PHP-FPM (sudo systemctl restart php8.3-fpm) for the extension to be loaded.
Where is the php.ini file located?
PHP uses separate php.ini files for each SAPI. The CLI config is at /etc/php/8.3/cli/php.ini, the Apache config at /etc/php/8.3/apache2/php.ini, and the FPM config at /etc/php/8.3/fpm/php.ini.
Conclusion
We have shown you how to install PHP 8.3 on Ubuntu 24.04 with both Apache and Nginx. To show and log PHP errors while you develop, see our guide on PHP error reporting .
Linuxize Weekly Newsletter
A quick weekly roundup of new tutorials, news, and tips.
About the authors

Dejan Panovski
Dejan Panovski is the founder of Linuxize, an RHCSA-certified Linux system administrator and DevOps engineer based in Skopje, Macedonia. Author of 1000+ Linux tutorials with 20+ years of experience turning complex Linux tasks into clear, reliable guides.
View author page