Server stacks depend on the PHP branch that a CMS, framework, or legacy application supports, and Ubuntu’s default repositories give each LTS release a maintained PHP package without adding a third-party repository. To install PHP on Ubuntu 26.04 (Resolute Raccoon), 24.04 (Noble Numbat), or 22.04 (Jammy Jellyfish), use the default packages when your application accepts the release’s PHP version, then choose Apache mod_php, Apache with PHP-FPM, or Nginx with PHP-FPM based on your web server.
Ubuntu 26.04 currently installs PHP 8.5, Ubuntu 24.04 installs PHP 8.3, and Ubuntu 22.04 installs PHP 8.1 from the standard repositories. If you arrived looking for PHP 8.1 or PHP 7.4 on Ubuntu 24.04, treat that as a specific-version compatibility task instead of the default install path. PHP 8.1 and PHP 7.4 are upstream end-of-life branches, so keep those workloads isolated and prefer an application upgrade when possible.
Install PHP on Ubuntu
Update Ubuntu Packages Before Installing PHP
Start by refreshing the package index and applying available updates. This reduces the chance of dependency conflicts when APT installs PHP, web server modules, or PHP-FPM services:
sudo apt update
sudo apt upgrade
These commands use
sudofor package management tasks that need root privileges. On a fresh server account, confirm your user can run sudo or follow the guide to add a user to sudoers on Ubuntu.
The apt update command refreshes package metadata, while apt upgrade applies available security patches and bug fixes before the PHP packages are installed.
PHP integrates with web servers in different ways, each with distinct performance and management characteristics. Select the method that matches your web server and use case.
Choose a PHP Handler for Apache or Nginx
| Method | Web Server | Process Model | Best For |
|---|---|---|---|
| Apache mod_php | Apache only | Embedded in Apache workers | Development environments; simplest setup |
| Apache + PHP-FPM | Apache | Separate FastCGI pool | Production sites; better memory management |
| Nginx + PHP-FPM | Nginx | Separate FastCGI pool | High-traffic production; static file performance |
For production environments, PHP-FPM is recommended because it runs PHP in a separate process pool with independent memory management, timeout settings, and the ability to run different PHP versions per virtual host. In contrast, use mod_php only for development or low-traffic sites where simplicity matters more than resource efficiency.
Install PHP with Apache mod_php on Ubuntu
The mod_php approach embeds PHP directly into Apache worker processes, making it the simplest option for development environments or small internal sites. Install Apache, the default PHP metapackage, and the matching Apache PHP module:
sudo apt install apache2 php libapache2-mod-php
This command installs the PHP interpreter and the Apache module that loads PHP into each worker process. After installation, restart Apache to activate the module:
sudo systemctl restart apache2
Then verify PHP is active:
php --version
Expected first line on Ubuntu 26.04:
PHP 8.5.4 (cli) (built: Apr 1 2026 09:36:11) (NTS)
Ubuntu 24.04 reports PHP 8.3.x, while Ubuntu 22.04 reports PHP 8.1.x from the standard repositories.
Install PHP-FPM with Apache on Ubuntu
PHP-FPM (FastCGI Process Manager) runs PHP as a separate service, providing better memory isolation and the ability to configure process pools independently from Apache. Install Apache, the PHP CLI, PHP-FPM, and Apache’s FastCGI module:
sudo apt install apache2 php-cli php-fpm libapache2-mod-fcgid
If you previously installed mod_php, disable the matching Apache PHP module before enabling PHP-FPM. Use the same PHP major and minor version reported by
php --version.
If that applies to your server, detect the active PHP branch and disable the matching Apache module:
PHP_VERSION=$(php -r 'echo PHP_MAJOR_VERSION.".".PHP_MINOR_VERSION;')
sudo a2dismod "php${PHP_VERSION}"
Next, enable the required Apache modules and the PHP-FPM configuration:
PHP_VERSION=$(php -r 'echo PHP_MAJOR_VERSION.".".PHP_MINOR_VERSION;')
sudo a2enmod proxy_fcgi setenvif
sudo a2enconf "php${PHP_VERSION}-fpm"
The proxy_fcgi module allows Apache to communicate with PHP-FPM via FastCGI protocol, while the versioned PHP-FPM Apache configuration tells Apache where to find the PHP-FPM socket. Now restart both services:
sudo systemctl restart php*-fpm
sudo systemctl restart apache2
Finally, confirm PHP-FPM is running:
sudo systemctl status php*-fpm --no-pager
The status output confirms PHP-FPM is enabled and running. Look for Active: active (running) and the versioned service name for your release, such as php8.5-fpm.service on Ubuntu 26.04, php8.3-fpm.service on Ubuntu 24.04, or php8.1-fpm.service on Ubuntu 22.04.
Install PHP-FPM with Nginx on Ubuntu
Unlike Apache, Nginx does not embed PHP directly, so PHP-FPM is the correct handler. Install Nginx, the PHP CLI, and PHP-FPM without the broad php metapackage:
sudo apt install nginx php-cli php-fpm
The Nginx command intentionally uses
php-cli php-fpminstead ofphp. On some supported Ubuntu releases, the broadphpmetapackage can also install Apache-side helpers that an Nginx-only stack does not need.
PHP-FPM starts automatically after installation. Next, verify it is running:
sudo systemctl status php*-fpm --no-pager
Configure Nginx Server Block for PHP
To enable PHP processing, add the following location block to your Nginx server configuration (typically in /etc/nginx/sites-available/your-site):
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php-fpm.sock;
}
The socket path
/run/php/php-fpm.sockis a symlink to the active PHP-FPM version, such as/run/php/php8.5-fpm.sockon Ubuntu 26.04. Use the version-specific socket only when you intentionally run multiple PHP-FPM pools side by side.
Before applying changes, test the configuration syntax:
sudo nginx -t
Expected output for valid configuration:
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok nginx: configuration file /etc/nginx/nginx.conf test is successful
Once validated, apply the configuration:
sudo systemctl reload nginx
Verify PHP Installation
Regardless of which method you chose, confirm PHP installed correctly by checking the version:
php --version
This output confirms your installed PHP version, which matches your Ubuntu release: PHP 8.5 on 26.04, PHP 8.3 on 24.04, and PHP 8.1 on 22.04.
Compare PHP Versions on Ubuntu
Default PHP Versions by Ubuntu Release
The unversioned php, php-cli, and php-fpm packages track the default PHP branch for each Ubuntu release. Use these default packages unless your application explicitly requires a different PHP major or minor version.
| Ubuntu Release | Default PHP Branch | Verified Package Version | Best Fit |
|---|---|---|---|
| Ubuntu 26.04 LTS (Resolute Raccoon) | PHP 8.5 | php8.5 8.5.4-0ubuntu1 | New deployments that can use the current PHP stable branch from Ubuntu’s repositories |
| Ubuntu 24.04 LTS (Noble Numbat) | PHP 8.3 | php8.3 8.3.6-0ubuntu0.24.04.8 | Production applications that support PHP 8.3 and prefer official Ubuntu maintenance |
| Ubuntu 22.04 LTS (Jammy Jellyfish) | PHP 8.1 | php8.1 8.1.2-1ubuntu2.23 | Legacy applications already validated on Jammy’s maintained PHP branch |
PHP 8.1 is upstream end-of-life, but Ubuntu 22.04 still carries patched PHP 8.1 packages as part of Ubuntu’s supported package set. That distinction matters for servers where Ubuntu repository maintenance is acceptable but upstream PHP branch support has ended.
When to Use a Specific PHP Version PPA
Use the Ondrej Sury PHP PPA only when a project requires a PHP branch that your Ubuntu release does not provide in its default repositories. The PPA currently publishes Ubuntu packages for Noble and Jammy, so do not add it to Ubuntu 26.04 just to install PHP 8.5, which is already the default branch there.
| PHP Branch | Support Status | Ubuntu Guidance |
|---|---|---|
| PHP 8.5 | Active support through December 2027; security support through December 2029 | Default on Ubuntu 26.04. Use a PPA only on older supported releases when an app specifically needs PHP 8.5. |
| PHP 8.4 | Active support through December 2026; security support through December 2028 | Use a specific-version PPA path on Ubuntu 24.04 or 22.04 when the application requires PHP 8.4. |
| PHP 8.3 | Security support through December 2027 | Default on Ubuntu 24.04. Use a PPA on Ubuntu 22.04 only when you need PHP 8.3 on Jammy. |
| PHP 8.2 | Security support through December 2026 | Use only for legacy compatibility when the application has not moved to PHP 8.3 or newer. |
| PHP 8.1 | Upstream end-of-life since December 2025 | Default on Ubuntu 22.04 with Ubuntu package maintenance. On Ubuntu 24.04, it is not in the default repositories and should be treated as a legacy compatibility branch. |
| PHP 7.4 | Upstream end-of-life since November 2022 | Not available from supported Ubuntu default repositories. Avoid it for new deployments; isolate any short-term legacy workload while planning an upgrade. |
For most servers, the default Ubuntu PHP branch is the safer baseline because it receives updates through the normal Ubuntu package workflow and avoids an extra repository. Choose a specific-version PPA only when an application framework, vendor requirement, or migration window makes that branch necessary.
Install Common PHP Extensions
Most web applications require extensions beyond the base PHP installation. This package set covers WordPress with Nginx, MariaDB, and PHP on Ubuntu, Laravel projects that use Composer on Ubuntu, and general PHP development needs:
sudo apt install php-curl php-mysql php-gd php-zip php-intl php-common php-bcmath php-imagick php-xmlrpc php-readline php-memcached php-redis php-mbstring php-apcu php-xml php-soap
JSON support is built into PHP 8.0 and later, so no separate
php-jsonpackage is needed. OPcache is tied to the versioned PHP branch packaging rather than a genericphp-opcachepackage, so verify it withphp -minstead of addingphp-opcacheto the generic extension command.
The php-xml package provides DOM, SimpleXML, XMLReader, and XMLWriter support. The php-mysql package includes both MySQLi and PDO MySQL drivers for MariaDB and MySQL.
After installation, restart your PHP handler to load the new extensions. For PHP-FPM users:
sudo systemctl restart php*-fpm
Alternatively, for Apache mod_php users, restart Apache instead:
sudo systemctl restart apache2
Verify the core extensions loaded correctly. The grep command filters the module list to the extension names that matter for this check, and sort -u removes duplicate matches such as OPcache entries from separate module sections:
php -m | grep -E '^(curl|gd|mbstring|mysqli|mysqlnd|xml|xmlreader|xmlwriter|Zend OPcache)$' | sort -u
The following output confirms the extensions are active:
curl gd mbstring mysqli mysqlnd xml xmlreader xmlwriter Zend OPcache
PHP Extension Reference
For reference, the extensions above provide these capabilities:
- php-curl: HTTP client for API requests and remote file operations
- php-mysql: MariaDB and MySQL database connectivity via MySQLi and MySQLnd
- php-gd: Image creation and manipulation for thumbnails, watermarks, and captchas
- OPcache: Bytecode caching to reduce PHP compilation overhead; verify it with
php -mbecause package naming is tied to the active PHP branch - php-zip: ZIP archive creation and extraction
- php-intl: Internationalization including number formatting, date handling, and collation
- php-bcmath: Arbitrary precision mathematics for financial calculations
- php-imagick: Advanced image processing through ImageMagick; see the dedicated guide to install PHP Imagick on Ubuntu when image handling is the main task
- php-memcached and php-redis: Distributed caching backends for session storage and object caching
- php-mbstring: Multibyte string handling for UTF-8 and international text processing
- php-apcu: In-memory user data cache for frequently accessed application data
- php-xml: XML parsing including DOM, SimpleXML, and XSL support
- php-soap: SOAP web services client and server functionality
Search Available Extensions
To explore additional options, list all available PHP extension packages:
apt search ^php.*- | head -30
List Loaded Modules
Alternatively, to display all currently active PHP modules:
php -m
Configure PHP Settings
Locate PHP Configuration Files
PHP maintains separate configuration files for CLI (command line) and web server contexts (PHP-FPM or Apache). To find the active CLI configuration file:
php --ini | head -3
The following output shows CLI configuration paths:
Configuration File (php.ini) Path: /etc/php/8.x/cli Loaded Configuration File: /etc/php/8.x/cli/php.ini Scan for additional .ini files in: /etc/php/8.x/cli/conf.d
For PHP-FPM (used by Nginx or Apache with PHP-FPM), the configuration file is at /etc/php/8.x/fpm/php.ini instead. Notably, changes to CLI settings do not affect web applications, and vice versa.
Adjust Common Settings
To modify settings for web applications using PHP-FPM, detect your active PHP branch and edit that branch’s FPM configuration file:
PHP_VERSION=$(php -r 'echo PHP_MAJOR_VERSION.".".PHP_MINOR_VERSION;')
sudo nano "/etc/php/${PHP_VERSION}/fpm/php.ini"
Below are common settings to adjust based on your application requirements:
; Maximum upload file size (default: 2M)
upload_max_filesize = 64M
; Maximum POST data size (must be >= upload_max_filesize)
post_max_size = 64M
; Maximum memory per script (default: 128M)
memory_limit = 256M
; Maximum script execution time in seconds (default: 30)
max_execution_time = 120
; Maximum input variables (default: 1000)
max_input_vars = 3000
After making changes, restart PHP-FPM to apply them:
sudo systemctl restart php*-fpm
Then verify the new settings are active:
php -i | grep upload_max_filesize
The following output confirms the new value:
upload_max_filesize => 64M => 64M
The CLI command above reads CLI settings. To verify web server settings, create a
phpinfo.phpfile in your web root and access it through your browser, then delete it when finished for security.
Test PHP with a Sample Script
To confirm PHP processes correctly through your web server, create a small test file in your document root. The sudo tee portion writes the file as root because the default web root is not writable by regular users:
printf '%s\n' '<?php echo "PHP is working!"; ?>' | sudo tee /var/www/html/test.php
Access the file through your browser at http://your-server/test.php. If PHP is configured correctly, you will see “PHP is working!” in plain text. Alternatively, test from the command line:
php /var/www/html/test.php
Expected output:
PHP is working!
After testing, remove the file for security:
Remove only the temporary
test.phpfile created in this section. Do not run recursive delete commands in a real application document root.
sudo rm /var/www/html/test.php
Troubleshoot Common PHP Issues
PHP Files Download Instead of Executing
This issue typically means the web server is not configured to process PHP files. For Apache mod_php, first verify the module is enabled:
apache2ctl -M | grep php
If no output appears, enable the PHP module:
PHP_VERSION=$(php -r 'echo PHP_MAJOR_VERSION.".".PHP_MINOR_VERSION;')
sudo a2enmod "php${PHP_VERSION}"
sudo systemctl restart apache2
Similarly, for Apache with PHP-FPM, verify the proxy configuration is enabled:
PHP_VERSION=$(php -r 'echo PHP_MAJOR_VERSION.".".PHP_MINOR_VERSION;')
sudo a2enconf "php${PHP_VERSION}-fpm"
sudo a2enmod proxy_fcgi setenvif
sudo systemctl restart apache2
For Nginx, verify your server block includes the PHP location directive and points to the correct socket path.
PHP-FPM Socket Not Found
If Nginx returns a 502 Bad Gateway error, the PHP-FPM socket may not exist or PHP-FPM may not be running. First, check if PHP-FPM is active:
sudo systemctl status php*-fpm
If inactive, start it:
sudo systemctl start php*-fpm
Then verify the socket exists:
ls -la /run/php/
In the output, you should see php-fpm.sock or a version-specific socket like php8.3-fpm.sock. Ensure your Nginx configuration matches the actual socket filename.
Extension Not Loading
If a required extension is not appearing in php -m output after installation, confirm the extension package is installed:
dpkg -l | grep -E 'php([0-9.]+)?-curl'
If installed but not loading, check for configuration issues:
php -i | grep -i "additional .ini"
Then verify the extension’s .ini file exists in that directory. After troubleshooting, always restart PHP-FPM or Apache to apply changes.
Remove PHP from Ubuntu
Uninstall PHP Packages
When PHP is no longer needed, remove the default PHP metapackages, Apache integration packages, and common extensions installed during this setup. The command leaves Apache, Nginx, databases, and application files in place:
sudo apt remove php php-cli php-fpm libapache2-mod-php libapache2-mod-fcgid php-curl php-mysql php-gd php-zip php-intl php-common php-bcmath php-imagick php-xmlrpc php-readline php-memcached php-redis php-mbstring php-apcu php-xml php-soap
Review APT’s removal list before confirming. Do not remove Apache, Nginx, MariaDB, MySQL, Redis, or Memcached packages if other sites or services still depend on them.
After removal, clean up dependencies that were installed automatically with PHP and are no longer needed. APT will show the list before it proceeds:
sudo apt autoremove
Purge Configuration Files
To also remove PHP configuration files such as php.ini, PHP-FPM pool configs, and extension INI files, use purge instead of remove:
sudo apt purge php php-cli php-fpm libapache2-mod-php libapache2-mod-fcgid php-curl php-mysql php-gd php-zip php-intl php-common php-bcmath php-imagick php-xmlrpc php-readline php-memcached php-redis php-mbstring php-apcu php-xml php-soap
sudo apt autoremove --purge
The purge command removes packaged PHP configuration under
/etc/php/. Back up custom pool files, INI overrides, and application-specific PHP settings before running purge on a production server.
Verify the unversioned PHP command is no longer available:
command -v php || echo "php command not found"
Expected output confirming removal:
php command not found
Next Steps After Installing PHP
PHP is installed from Ubuntu’s default repositories with the Apache or Nginx handler that matches the server stack. From here, secure the web layer with Let’s Encrypt for Nginx on Ubuntu, or add database administration with phpMyAdmin on an Ubuntu LEMP stack when the server needs a browser-based MySQL or MariaDB tool.


Thank you from the heart taming and whipping Sudo something neat and smooth I needed it a while ago thank you from the depths rust without borders …