PHP is a popular server-side scripting language widely used to develop dynamic web applications. PHP-FPM (FastCGI Process Manager) improves efficiency and optimizes handling of PHP requests when combined with web servers.
This guide walks you through installing PHP and PHP-FPM on Ubuntu 22.04, ensuring you have a fully functional PHP environment for building dynamic applications.
Prerequisites
Before starting, make sure you have:
- An Ubuntu 22.04 server.
- SSH access as a non-root user with sudo rights.
- A fully updated server.
Matching infrastructure at centron
Ubuntu servers without your own hardware: ccloud³ VMs with full root access from €3.12 per month, billed by the hour and ready in seconds. Rent a cloud server →
Install PHP
Ubuntu 22.04 includes PHP in its default APT repositories. Use the steps below to update your server and install PHP.
Update the package index
$ sudo apt updateInstall PHP
$ sudo apt install php -yCheck installed PHP version
$ php -vExample output:
PHP 8.1.2-1ubuntu2.21 (cli) (built: Mar 24 2025 19:04:23) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.1.2, Copyright (c) Zend Technologies
with Zend OPcache v8.1.2-1ubuntu2.21, Copyright (c), by Zend Technologies
Install PHP Extensions
Extensions extend PHP capabilities, providing database integration, encryption, image handling, and more. Install common extensions using the command below:
$ sudo apt install -y php-mysql php-mbstring php-bcmath php-zip php-gd php-curl php-xmlThis command installs:
- php-mysql – Connects PHP with MySQL.
- php-mbstring – UTF-8 and multibyte string support.
- php-bcmath – High-precision math operations.
- php-zip – ZIP archive support.
- php-gd – Image processing library.
- php-curl – Data transfer via URLs.
- php-xml – XML parsing and handling.
List available PHP extensions
$ sudo apt-cache search php | grep "^php8.1"Check installed PHP extensions
$ php -mExample output:
[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 Ubuntu 22.04 repositories and corresponds to your installed PHP version. Follow the steps below:
Install PHP-FPM
$ sudo apt install php-fpm -yCheck PHP-FPM version
$ php-fpm8.1 -vOutput:
PHP 8.1.2-1ubuntu2.21 (fpm-fcgi) (built: Mar 24 2025 19:04:23)
Copyright (c) The PHP Group
Zend Engine v4.1.2, Copyright (c) Zend Technologies
with Zend OPcache v8.1.2-1ubuntu2.21, Copyright (c), by Zend Technologies
Verify PHP-FPM directories
$ ls /etc/php/8.1/Example output:
apache2 cli fpm mods-available
Modify PHP-FPM Pool Configurations
Move into the pool directory:
$ cd /etc/php/8.1/fpm/pool.d/Edit the default configuration file:
$ sudo nano www.confVerify PHP-FPM Pool Settings
Pool name:
[www]Ensure user and group settings:
user = www-data
group = www-dataSocket path:
listen = /run/php/php8.1-fpm.sockProcess management settings:
pm = dynamic
pm.max_children = 5
pm.start_servers = 2
pm.min_spare_servers = 1
pm.max_spare_servers = 3Explanation:
- pm: Process manager mode (static, dynamic, ondemand).
- pm.max_children: Maximum number of child processes.
- pm.start_servers: Processes to start with (for dynamic mode).
- pm.min_spare_servers: Minimum idle processes.
- pm.max_spare_servers: Maximum idle processes.
- pm.process_idle_timeout: Time before stopping idle processes.
- access.log: Logs PHP-FPM requests.
- slowlog: Path for logging slow requests.
- request_slowlog_timeout: Timeout threshold for slow requests.
Restart PHP-FPM
Restart the service to apply your changes:
$ sudo systemctl restart php8.1-fpmTest PHP and PHP-FPM
PHP handles dynamic content, while PHP-FPM connects PHP with the web server to optimize resource usage. Follow these steps to test PHP and PHP-FPM by serving a sample application with Nginx.
Create and Test a Sample PHP File
Switch to your home directory:
$ cdCreate a new file named sample.php:
$ nano sample.phpAdd the following content:
<!--?php echo "Greetings from centron! PHP is working correctly on this server"; ?-->Run the script using the PHP CLI:
$ php sample.phpExpected output:
Greetings from centron! PHP is working correctly on this server
Install and Configure Nginx
Install Nginx to serve PHP files through PHP-FPM:
$ sudo apt install nginx -yBack up the default configuration:
$ sudo mv /etc/nginx/sites-available/default /etc/nginx/sites-available/default.BAKCreate a new default Nginx configuration:
$ sudo nano /etc/nginx/sites-available/defaultAdd the following configuration:
server {
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/php8.1-fpm.sock;
fastcgi_index index.php;
include fastcgi.conf;
}
}Test the Nginx configuration:
$ sudo nginx -tExpected output:
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
Test PHP with Nginx
Create a new file test.php in /var/www/html:
$ sudo nano /var/www/html/test.phpAdd this code:
<!--?php phpinfo(); ?-->Stop Apache to free port 80:
$ sudo systemctl stop apache2Allow HTTP traffic through UFW:
$ sudo ufw allow 80Restart Nginx:
$ sudo systemctl restart nginxOpen your browser and navigate to:
http://SERVER-IP/test.php
Install Multiple PHP Versions
Some projects may require different PHP versions. By default, Ubuntu 22.04 provides limited PHP releases. To install older or alternative versions such as PHP 7.4, use the ppa:ondrej/php repository.
Add the PHP Repository
$ sudo add-apt-repository -y ppa:ondrej/phpInstall PHP 7.4 and PHP-FPM
$ sudo apt install -y php7.4 php7.4-fpmCheck PHP Version
$ php7.4 -vExample output:
PHP 7.4.33 (cli) (built: Jun 6 2024 16:49:34) ( NTS )
Copyright (c) The PHP Group
Zend Engine v3.4.0, Copyright (c) Zend Technologies
with Zend OPcache v7.4.33, Copyright (c), by Zend Technologies
Check PHP-FPM Status
$ sudo systemctl status php7.4-fpmSample output:
● php7.4-fpm.service - The PHP 7.4 FastCGI Process Manager
Loaded: loaded (/lib/systemd/system/php7.4-fpm.service; enabled; vendor preset: enabled)
Active: active (running) since Sun 2025-04-06 16:24:27 UTC; 33s ago
Docs: man:php-fpm7.4(8)
Main PID: 31049 (php-fpm7.4)
Status: "Processes active: 0, idle: 2, Requests: 0, slow: 0, Traffic: 0req/sec"
Create a New Site for PHP 7.4
Create a new directory:
$ sudo mkdir -p /var/www/php74-siteCopy the test file as index.php:
$ sudo cp /var/www/html/test.php /var/www/php74-site/index.phpCreate a new Nginx virtual host configuration:
$ sudo nano /etc/nginx/sites-available/php74-site.confAdd the following content:
server {
listen 9000;
listen [::]:9000;
root /var/www/php74-site;
index index.php index.html;
location ~ \.php$ {
try_files $uri =404;
fastcgi_pass unix:/run/php/php7.4-fpm.sock;
fastcgi_index index.php;
include fastcgi.conf;
}
}Enable the new site:
$ sudo ln -s /etc/nginx/sites-available/php74-site.conf /etc/nginx/sites-enabled/Test Nginx:
$ sudo nginx -tExpected output:
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
Allow port 9000 in the firewall:
$ sudo ufw allow 9000/tcpReload the firewall:
$ sudo ufw reloadRestart Nginx:
$ sudo systemctl restart nginxAccess your PHP 7.4 site in the browser:
http://SERVER-IP:9000
You should see PHP 7.4 details. Access your default site to confirm PHP 8.1 runs separately:
http://SERVER-IP/test.php
Conclusion
In this guide, you installed PHP, PHP-FPM, and multiple PHP versions on Ubuntu 22.04. By integrating PHP-FPM with Nginx, you can efficiently handle dynamic requests and host multiple projects with different PHP requirements. For more details, visit the official PHP documentation.
Testen Sie Ihr Setup auf ccloud³
Registrieren Sie sich in der ccloud³ und erhalten Sie 200 € Startguthaben für Ihr Projekt – z. B. für eine PostgreSQL-VM mit automatischen Backups.