PHP powers everything from personal blogs to enterprise web applications, handling dynamic content generation, form processing, and database interactions. Each Ubuntu LTS release includes a tested PHP version in its default repositories, providing automatic security updates through the Ubuntu security team. This guide walks through installing the default PHP on Ubuntu 22.04, 24.04, and 26.04 LTS with Apache or Nginx, configuring common settings, managing extensions, and switching between PHP versions when needed.
As a result, by the end of these steps, you will have PHP configured with your preferred web server, essential extensions installed for WordPress or Laravel development, and the knowledge to manage PHP versions across multiple projects.
Choose Your PHP Version
Default PHP Versions by Ubuntu Release
Ubuntu’s default repositories provide different PHP versions depending on your release. Consequently, the version you get with sudo apt install php varies by Ubuntu version. The table below shows what each LTS release provides and when to consider alternatives.
| Ubuntu Release | Default PHP | Support Status | Best For |
|---|---|---|---|
| Ubuntu 26.04 LTS | PHP 8.4 | Active support through November 2026 | New projects wanting latest features (property hooks, asymmetric visibility) |
| Ubuntu 24.04 LTS | PHP 8.3 | Active support through December 2025 | Production servers, WordPress 6.x, Laravel 11, Drupal 10 |
| Ubuntu 22.04 LTS | PHP 8.1 | Security-only through December 2025 | Legacy applications requiring PHP 8.1 compatibility |
For most users, the default PHP version is the recommended choice because it receives automatic security updates through Ubuntu’s security team and requires no external repositories. However, if your application requires a specific PHP version not available in your Ubuntu release, see the version-specific guides linked below.
Install a Specific PHP Version via PPA
When your project requires a PHP version different from Ubuntu’s default, the Ondrej Sury PPA provides co-installable PHP versions from 5.6 through 8.5. The comparison below helps you decide which release fits your project requirements and maintenance preferences.
| PHP Version | Primary Focus | Support Status | Best For | Trade-offs |
|---|---|---|---|---|
| Distro Default | Stability with Ubuntu security team maintenance | Matches Ubuntu release lifecycle | Production servers prioritizing official Ubuntu updates and minimal external dependencies | Version varies by release (8.1 on 22.04, 8.3 on 24.04, 8.4 on 26.04) |
| PHP 8.5 | URI extension, pipe operator, clone with properties | Active support through December 2027; security through December 2029 | Modern applications using functional programming patterns, URL parsing, and immutable object patterns | Requires Sury PPA; newest release |
| PHP 8.4 | Property hooks, asymmetric visibility, #[\Deprecated] attribute | Active support through December 2026; security through December 2028 | Production applications using modern OOP patterns, type-safe APIs, and improved deprecation handling | Default on 26.04; requires Sury PPA on older releases |
| PHP 8.3 | Typed class constants, json_validate(), readonly property cloning | Security-only through December 2027 (active support ended) | WordPress 6.x, Laravel 11, Drupal 10 sites needing stable features with long security support | Default on 24.04; security patches only on Sury PPA |
| PHP 8.2 | Readonly classes, DNF types, null/false standalone types | Security-only through December 2026 (active support ended) | Legacy applications requiring PHP 8.2 compatibility | No new features; security patches only |
Recommendation: For most users, the distro default PHP is the safest choice because it receives automatic security updates through Ubuntu’s security team and requires no external repositories. Choose a specific version from the Sury PPA only when your application framework explicitly requires it or you need features unavailable in the default version.
This guide covers Ubuntu 22.04 LTS, 24.04 LTS, and 26.04 LTS installations using the default repository PHP. Commands shown work identically on all supported LTS releases unless otherwise noted, though the PHP version installed will match your Ubuntu release as shown in the table above.
Update System Packages
Before installing PHP, synchronize your package index and upgrade installed packages to ensure compatibility with new installations:
sudo apt update
sudo apt upgrade
The apt update command refreshes the local package database from configured repositories, while apt upgrade applies any available security patches and bug fixes. Running both commands before new installations prevents dependency conflicts.
Install PHP on Ubuntu
PHP integrates with web servers in different ways, each with distinct performance and management characteristics. To proceed, select the method that matches your web server and use case from the options below.
Compare PHP Integration Methods
| 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.
Option 1: Install PHP with Apache mod_php
The mod_php approach embeds PHP directly into Apache worker processes, making it the simplest option for development environments. To install PHP as an Apache module:
sudo apt install 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 output on Ubuntu 26.04 (PHP 8.1 on 22.04, PHP 8.3 on 24.04):
PHP 8.4.11 (cli) (built: Aug 13 2025 01:43:48) (NTS)
Copyright (c) The PHP Group
Built by Ubuntu
Zend Engine v4.4.11, Copyright (c) Zend Technologies
with Zend OPcache v8.4.11, Copyright (c), by Zend Technologies
Option 2: Install PHP with Apache and PHP-FPM
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. First, install the required packages:
sudo apt install php-fpm libapache2-mod-fcgid
If you previously installed mod_php, disable it before enabling PHP-FPM to avoid conflicts:
sudo a2dismod php8.x(replace 8.x with your PHP version)
Next, enable the required Apache modules and the PHP-FPM configuration:
sudo a2enmod proxy_fcgi setenvif
sudo a2enconf php*-fpm
The proxy_fcgi module allows Apache to communicate with PHP-FPM via FastCGI protocol, while the php*-fpm configuration file 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 expected output below confirms PHP-FPM is running with active status:
● php8.x-fpm.service - The PHP 8.x FastCGI Process Manager
Loaded: loaded (/usr/lib/systemd/system/php8.x-fpm.service; enabled; preset: enabled)
Active: active (running) since [date and time]
Main PID: [pid] (php-fpm8.x)
Tasks: [number]
Memory: [size]
CPU: [time]
CGroup: /system.slice/php8.x-fpm.service
Option 3: Install PHP with Nginx and PHP-FPM
Unlike Apache, Nginx does not embed PHP directly, so PHP-FPM is the only option. To begin, install the required packages:
sudo apt install php php-fpm php-cli
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:/var/run/php/php-fpm.sock;
}
The socket path
/var/run/php/php-fpm.sockis a symlink that points to your active PHP-FPM version. If you need version-specific control, use/var/run/php/php8.x-fpm.sockwith your specific version number.
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 (8.1 on 22.04, 8.3 on 24.04, 8.4 on 26.04).
Install Common PHP Extensions
Most web applications require extensions beyond the base PHP installation. The following command installs a comprehensive set covering WordPress, Laravel with Composer, and general PHP development needs:
sudo apt install php-curl php-mysql php-gd php-opcache 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. Thephp-xmlpackage provides DOM, SimpleXML, XMLReader, and XMLWriter support. Thephp-mysqlpackage includes both MySQLi and PDO MySQL drivers.
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
Subsequently, verify the core extensions loaded correctly:
php -m | grep -E 'curl|mysqli|gd|mbstring|xml'
The following output confirms the extensions are active:
curl gd libxml mbstring mysqli xml xmlreader xmlwriter
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
- php-opcache: Bytecode caching to reduce PHP compilation overhead
- 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
- 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, edit the FPM configuration file. The path varies by PHP version:
sudo nano /etc/php/*/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 test file in your document root:
echo '<?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:
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:
sudo a2enmod php*
sudo systemctl restart apache2
Similarly, for Apache with PHP-FPM, verify the proxy configuration is enabled:
sudo a2enconf php*-fpm
sudo a2enmod proxy_fcgi setenvif
sudo systemctl restart apache2
Likewise, 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 /var/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 php.*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 no longer needed, you can remove PHP and all its extensions with the following command:
sudo apt remove php*
During execution, APT will display the packages to be removed before proceeding:
The following packages will be removed: libapache2-mod-php php php-cli php-common php-curl php-fpm php-gd php-mbstring php-mysql php-opcache php-xml php-zip [additional packages...]
After removal, clean up orphaned dependencies that were installed automatically with PHP:
sudo apt autoremove
Purge Configuration Files
To also remove PHP configuration files (php.ini, pool configs, extension configs), use purge instead of remove:
sudo apt purge php*
sudo apt autoremove
The purge command removes all PHP configuration files in
/etc/php/. If you have custom configurations you want to preserve, back them up before running purge.
Verify PHP is completely removed:
php --version
Expected output confirming removal:
bash: php: command not found
Conclusion
You now have PHP installed from Ubuntu’s default repositories with your chosen web server integration. The default version provides automatic security updates through Ubuntu’s security team and works seamlessly with WordPress, Laravel, and other PHP applications. For projects requiring a specific PHP version, the PHP 8.5, PHP 8.4, PHP 8.3, and PHP 8.2 guides cover PPA-based installations.
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 …