Tutorials  /  Ubuntu

Install and Configure PHP & PHP-FPM on Ubuntu 22.04

Ccentron Redaktion · October 2025 ·8 min read ·Ubuntu, Tutorial

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.
VM

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

Console
$ sudo apt update

Install PHP

Console
$ sudo apt install php -y

Check installed PHP version

Console
$ php -v

Example 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:

Console
$ sudo apt install -y php-mysql php-mbstring php-bcmath php-zip php-gd php-curl php-xml

This 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

Console
$ sudo apt-cache search php | grep "^php8.1"

Check installed PHP extensions

Console
$ php -m

Example 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

Console
$ sudo apt install php-fpm -y

Check PHP-FPM version

Console
$ php-fpm8.1 -v

Output:

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

Console
$ ls /etc/php/8.1/

Example output:

apache2  cli  fpm  mods-available

Modify PHP-FPM Pool Configurations

Move into the pool directory:

Console
$ cd /etc/php/8.1/fpm/pool.d/

Edit the default configuration file:

Console
$ sudo nano www.conf

Verify PHP-FPM Pool Settings

Pool name:

Code
[www]

Ensure user and group settings:

Code
user = www-data
group = www-data

Socket path:

Code
listen = /run/php/php8.1-fpm.sock

Process management settings:

Code
pm = dynamic
pm.max_children = 5
pm.start_servers = 2
pm.min_spare_servers = 1
pm.max_spare_servers = 3

Explanation:

  • 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:

Console
$ sudo systemctl restart php8.1-fpm

Test 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:

Console
$ cd

Create a new file named sample.php:

Console
$ nano sample.php

Add the following content:

Code
<!--?php echo "Greetings from centron! PHP is working correctly on this server"; ?-->

Run the script using the PHP CLI:

Console
$ php sample.php

Expected output:

Greetings from centron! PHP is working correctly on this server

Install and Configure Nginx

Install Nginx to serve PHP files through PHP-FPM:

Console
$ sudo apt install nginx -y

Back up the default configuration:

Console
$ sudo mv /etc/nginx/sites-available/default /etc/nginx/sites-available/default.BAK

Create a new default Nginx configuration:

Console
$ sudo nano /etc/nginx/sites-available/default

Add the following configuration:

Code
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:

Console
$ sudo nginx -t

Expected 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:

Console
$ sudo nano /var/www/html/test.php

Add this code:

Code
<!--?php phpinfo(); ?-->

Stop Apache to free port 80:

Console
$ sudo systemctl stop apache2

Allow HTTP traffic through UFW:

Console
$ sudo ufw allow 80

Restart Nginx:

Console
$ sudo systemctl restart nginx

Open 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

Console
$ sudo add-apt-repository -y ppa:ondrej/php

Install PHP 7.4 and PHP-FPM

Console
$ sudo apt install -y php7.4 php7.4-fpm

Check PHP Version

Console
$ php7.4 -v

Example 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

Console
$ sudo systemctl status php7.4-fpm

Sample 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:

Console
$ sudo mkdir -p /var/www/php74-site

Copy the test file as index.php:

Console
$ sudo cp /var/www/html/test.php /var/www/php74-site/index.php

Create a new Nginx virtual host configuration:

Console
$ sudo nano /etc/nginx/sites-available/php74-site.conf

Add the following content:

Code
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:

Console
$ sudo ln -s /etc/nginx/sites-available/php74-site.conf /etc/nginx/sites-enabled/

Test Nginx:

Console
$ sudo nginx -t

Expected 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:

Console
$ sudo ufw allow 9000/tcp

Reload the firewall:

Console
$ sudo ufw reload

Restart Nginx:

Console
$ sudo systemctl restart nginx

Access 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.

Jetzt 200 € Guthaben sichern

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.

centron Redaktion Technische Redaktion

Das Redaktionsteam von centron schreibt Anleitungen aus dem Betriebsalltag: getestet auf unserer eigenen Plattform, betrieben im Rechenzentrum in Hallstadt bei Bamberg.

Kategorie Ubuntu
Teilen
Noch offene Fragen?

Unser Team hilft Ihnen bei Ihrem konkreten Setup weiter – von Menschen, die die Plattform selbst betreiben.

War dieses Tutorial hilfreich?

Ihre Antwort wird anonym gespeichert und hilft uns, die Tutorials zu verbessern.

Kommentare

Noch keine Kommentare – stellen Sie die erste Frage zu diesem Tutorial.

Zum Kommentieren anmelden

Kommentare stehen centron-Kunden offen. Melden Sie sich in Ihrem Konto an, um eine Frage zu diesem Tutorial zu stellen.

Weiterlesen

Das könnte Sie auch interessieren

Jetzt kostenlos anfangen

Melden Sie sich an und erhalten Sie in den ersten 60 Tagen ein Guthaben von 200 € bei centron.

Dieses Werbeangebot gilt nur für neue Konten. Angebot ausschließlich für Gewerbetreibende.

Jetzt loslegen Sales kontaktieren