How to Install PHP and PHP-FPM on Ubuntu 26.04

PHP (Hypertext Preprocessor) is a widely used server-side scripting language designed for building dynamic web applications. PHP-FPM (FastCGI Process Manager) is an alternative PHP FastCGI implementation that manages worker processes efficiently, offering features such as adaptive process spawning, graceful restarts, and improved resource handling compared to traditional CGI methods.
This article explains how to install PHP and PHP-FPM on an Ubuntu 26.04 server. It covers installing common PHP extensions, configuring the PHP-FPM pool settings, testing PHP with Nginx, and running multiple PHP versions side by side.
Prerequisites
Before you begin, you need to:
- Have access to an Ubuntu 26.04 server instance as a non-root user with sudo privileges.
Install PHP
The default Ubuntu 26.04 APT repositories include the latest PHP package. The following steps update the package index and install the PHP command-line interpreter.
Update the APT package index.
console$ sudo apt update
Install PHP.
console$ sudo apt install php -y
Confirm the installed PHP version.
console$ php -v
Your output should be similar to the one below:
PHP 8.5.2 (cli) (built: Jan 21 2026 17:35:28) (NTS) Copyright (c) The PHP Group Built by Ubuntu Zend Engine v4.5.2, Copyright (c) Zend Technologies with Zend OPcache v8.5.2, Copyright (c), by Zend Technologies
Install PHP Extensions
PHP extensions add capabilities such as database connectivity, image processing, and encryption support that most web applications and frameworks require. The following steps install commonly used extensions and verify the active modules.
Install common PHP extensions.
console$ sudo apt install -y php-mysql php-mbstring php-bcmath php-zip php-gd php-curl php-xml
The command installs the following extensions:
php-mysql: Provides connectivity to MySQL and MariaDB databases.php-mbstring: Adds support for multibyte string encoding such as UTF-8.php-bcmath: Enables arbitrary-precision arithmetic operations.php-zip: Adds support for reading and writing ZIP archives.php-gd: Provides image creation and manipulation functions.php-curl: Enables HTTP requests and URL transfers from PHP.php-xml: Adds XML parsing and generation support.
Run the following command to list all available PHP extensions for your installed version.
console$ sudo apt-cache search php | grep "^php8.5"
View all active PHP modules.
console$ php -m
Your output should be similar to the one below:
[PHP Modules] bcmath calendar Core ctype curl date dom exif FFI ...... [Zend Modules] Zend OPcache
Install and Configure PHP-FPM
PHP-FPM is available in the default APT repositories and matches the installed PHP version. The following steps install PHP-FPM, verify the installation, and configure the worker pool settings.
Install PHP-FPM.
console$ sudo apt install php-fpm -y
Confirm the installed PHP-FPM version.
console$ php-fpm8.5 -v
Your output should be similar to the one below:
PHP 8.5.2 (fpm-fcgi) (built: Jan 21 2026 17:35:28) (NTS) Copyright (c) The PHP Group Built by Ubuntu Zend Engine v4.5.2, Copyright (c) Zend Technologies with Zend OPcache v8.5.2, Copyright (c), by Zend TechnologiesList the PHP configuration directories to verify that the
fpmdirectory exists.console$ ls /etc/php/8.5/
Your output should be similar to the one below:
apache2 cli fpm mods-availableOpen the default PHP-FPM pool configuration file using a text editor such as Nano.
console$ sudo nano /etc/php/8.5/fpm/pool.d/www.conf
Find the
[www]directive to verify the pool name.ini[www]Find the
userandgroupdirectives to confirm that PHP-FPM runs as thewww-datauser. Web servers such as Nginx require this user for proper file access.iniuser = www-data group = www-data
Find the
listendirective to verify the Unix socket path used to connect to the PHP-FPM service.inilisten = /run/php/php8.5-fpm.sock
Adjust the following process manager directives based on your server resources and expected traffic.
inipm = dynamic pm.max_children = 5 pm.start_servers = 2 pm.min_spare_servers = 1 pm.max_spare_servers = 3
Within the configuration:
pm: Sets the process manager mode. Options includestatic(fixed number of workers),dynamic(scales workers based on demand), andondemand(spawns workers only when requests arrive).pm.max_children: Defines the maximum number of child processes that can run simultaneously.pm.start_servers: Sets how many child processes to create at startup.pm.min_spare_servers: Maintains a minimum number of idle worker processes.pm.max_spare_servers: Limits the maximum number of idle worker processes.
Save and close the file.
Restart PHP-FPM to apply the configuration changes.
console$ sudo systemctl restart php8.5-fpm
Test PHP and PHP-FPM
PHP processes scripts through the command-line interpreter, while PHP-FPM handles requests forwarded by a web server. The following steps verify both by running a PHP script from the terminal and then configuring Nginx to serve a PHP application through PHP-FPM.
Create a sample PHP script.
console$ nano sample.php
Add the following content to the file.
php<?php echo "Hello World! PHP is running correctly on this server."; ?>
Save and close the file.
Run the script using the PHP CLI.
console$ php sample.php
Your output should be similar to the one below:
Hello World! PHP is running correctly on this server.Stop and disable the Apache service that is pre-installed with PHP.
console$ sudo systemctl disable --now apache2
Install the Nginx web server to test PHP-FPM integration.
console$ sudo apt install nginx -y
Back up the default Nginx virtual host configuration.
console$ sudo mv /etc/nginx/sites-available/default /etc/nginx/sites-available/default.BAK
Create a new default Nginx configuration file.
console$ sudo nano /etc/nginx/sites-available/default
Add the following configuration to the file.
iniserver { listen 80 default_server; listen [::]:80 default_server; root /var/www/html/; index index.html index.php index.nginx-debian.html; location ~ \.php$ { try_files $uri =404; fastcgi_pass unix:/run/php/php-fpm.sock; fastcgi_index index.php; include fastcgi.conf; } }
Save and close the file.
This configuration listens on port
80and serves files from the/var/www/htmldirectory. Nginx forwards all.phprequests to the PHP-FPM socket at/run/php/php-fpm.sockfor processing through FastCGI.Test the Nginx configuration for syntax errors.
console$ sudo nginx -t
Create a PHP test file in the Nginx web root directory.
console$ sudo nano /var/www/html/test.php
Add the following content to the file.
php<?php phpinfo(); ?>
Save and close the file.
This script displays detailed PHP version and configuration information when accessed through a web browser.
Allow HTTP traffic on port
80through the firewall.console$ sudo ufw allow 80
Restart Nginx to apply the configuration.
console$ sudo systemctl restart nginx
Access the PHP test page in a web browser. Replace
SERVER-IPwith your server's public IP address.http://SERVER-IP/test.phpThe browser displays the PHP information page, confirming that Nginx processes PHP requests through PHP-FPM.
Conclusion
You have installed PHP and PHP-FPM on an Ubuntu 26.04 server, configured the FPM worker pool, and verified PHP processing through Nginx. PHP-FPM integrates with web servers such as Nginx and Apache to handle dynamic application requests efficiently. For more information, refer to the official PHP documentation.