Install and Configure PHP and PHP-FPM on Ubuntu 22.04

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.

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

Install PHP

Check installed PHP version

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:

$ 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

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

Check installed PHP extensions

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

$ sudo apt install php-fpm -y

Check PHP-FPM version

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

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:

Verify PHP-FPM Pool Settings

Pool name:

Ensure user and group settings:

user = www-data
group = www-data

Socket path:

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

Process management settings:

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:

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

Create a new file named sample.php:

Add the following content:

Run the script using the PHP CLI:

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:

$ sudo apt install nginx -y

Back up the default configuration:

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

Create a new default Nginx configuration:

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

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

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:

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

Add this code:

Stop Apache to free port 80:

$ sudo systemctl stop apache2

Allow HTTP traffic through UFW:

Restart Nginx:

$ 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

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

Install PHP 7.4 and PHP-FPM

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

Check PHP Version

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

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

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

Copy the test file as index.php:

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

Create a new Nginx virtual host configuration:

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

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

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:

Reload the firewall:

Restart Nginx:

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

Source: vultr.com

Create a Free Account

Register now and get access to our Cloud Services.

Posts you might be interested in: