
PHP is a widely-used server-side scripting language for building dynamic web applications. PHP-FPM enhances performance by efficiently managing PHP processes.
This article covers installing PHP and PHP-FPM on Ubuntu 20.04 to set up a PHP environment.
Prerequisites
Before you begin:
Have an Ubuntu 20.04 server.
Set up a new A record for your domain that points to the server IP address.
Access the server using SSH as a non-root user with sudo privileges.
Install PHP
Ubuntu 20.04 includes PHP in its default APT repositories. Update your server packages and install PHP using the latest available version.
Update the server package index.
console$ sudo apt update
Install PHP.
console$ sudo apt install php -y
View the installed PHP version on your server.
console$ php -v
Your output should be similar to the one below:
PHP 7.4.3-4ubuntu2.29 (cli) (built: Mar 25 2025 18:57:03) ( NTS ) Copyright (c) The PHP Group Zend Engine v3.4.0, Copyright (c) Zend Technologies with Zend OPcache v7.4.3-4ubuntu2.29, Copyright (c), by Zend Technologies
Install PHP Extensions
PHP extensions add functionality like database support, file handling, and encryption—commonly required by PHP apps and frameworks. Install them to enhance your server’s PHP capabilities.
Install common PHP extensions.
console$ sudo apt install -y php-mysql php-mbstring php-bcmath php-zip php-gd php-curl php-xml
The above command installs the following PHP extensions:
Common PHP extensions include:
php-mysql
: Connects PHP apps with MySQL.php-mbstring
: Handles UTF-8 text.php-bcmath
: Supports precision math operations.php-zip
: Enables ZIP file support.php-gd
: Adds image processing features.php-curl
: Supports data transfer via URLs.php-xml
: Enables XML handling in PHP.Run the following command to list all PHP extensions you can install based on your server's PHP version.
console$ sudo apt-cache search php | grep "^php7.4"
View all installed PHP extensions on your server.
console$ php -m
Your output should look like 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 included in Ubuntu's APT repositories and aligns with your installed PHP version. Install it to handle PHP requests efficiently and ensure it starts automatically on boot.
Install PHP-FPM.
console$ sudo apt install php-fpm -y
View the installed PHP-FPM version on your server depending on your PHP version. For example,
PHP 7.4
.console$ php-fpm7.4 -v
Output:
PHP 7.4.3-4ubuntu2.29 (fpm-fcgi) (built: Mar 25 2025 18:57:03) Copyright (c) The PHP Group Zend Engine v3.4.0, Copyright (c) Zend Technologies with Zend OPcache v7.4.3-4ubuntu2.29, Copyright (c), by Zend Technologies
List all files in the
/etc/php/7.4
directory depending on your installed PHP version. Then, verify that thefpm
directory is available.console$ ls /etc/php/7.4/
Output:
apache2 cli fpm mods-available
Based on the above output, PHP-FPM
fpm
is available and the necessary configurations that control how PHP processes requests on your server.Switch to the PHP-FPM pool configurations directory
/etc/php/7.4/fpm/pool.d/
.console$ cd /etc/php/7.4/fpm/pool.d/
Open the default PHP-FPM pool configuration
/etc/php/7.4/fpm/pool.d/www.conf
using a text editor such as Nano.console$ sudo nano www.conf
Find the
[www]
option to verify your PHP-FPM pool name.ini[www]
Find the
user
andgroup
options to verify the user PHP-FPM runs as. When using a web server, verify that PHP-FPM runs as thewww-data
user.iniuser = www-data group = www-data
Find the
listen
directive and verify the socket path to access the PHP-FPM service on your server.inilisten = /run/php/php7.4-fpm.sock
Modify the following configuration to match your PHP-FPM processing needs on your server.
inipm = dynamic pm.max_children = 5 pm.start_servers = 2 pm.min_spare_servers = 1 pm.max_spare_servers = 3
PHP-FPM pool settings control how PHP processes handle requests:
pm
: Defines the process manager type (static
,dynamic
, orondemand
).pm.max_children
: Limits total PHP child processes to manage memory.pm.start_servers
,pm.min_spare_servers
,pm.max_spare_servers
: Control how many PHP processes run or stay idle.pm.process_idle_timeout
: Stops idle processes after a set time.access.log
: Logs all incoming requests.slowlog
andrequest_slowlog_timeout
: Track and log slow PHP requests for performance tuning.
Save and close the file.
Restart PHP-FPM to apply your configuration changes.
console$ sudo systemctl restart php7.4-fpm
Test PHP and PHP-FPM
PHP handles dynamic content, while PHP-FPM manages and optimizes request processing. To verify they're working, you can deploy a sample PHP file using the Nginx web server.
Switch to your user home directory.
console$ cd
Create a new sample PHP application file
sample.php
.console$ nano sample.php
Add the following contents to the file.
php<?php echo "Greetings from Vultr! PHP is working correctly on this server"; ?>
Save and close the file.
Run the application using the PHP CLI utility.
console$ php sample.php
Output:
Greetings from Vultr! PHP is working correctly on this server
Based on the above output, PHP processes dynamic web application files on your server correctly.
Install the Nginx web server application to test access to the PHP-FPM service.
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 configurations 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/php7.4-fpm.sock; fastcgi_index index.php; include fastcgi.conf; } }
Save and close the file.
The Nginx config listens on port
80
for all IPs and serves files from/var/www/html
. PHP files are handled by FastCGI via the PHP-FPM socket at/run/php/php7.4-fpm.sock
.Test the Nginx configuration for errors.
console$ sudo nginx -t
Output:
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok nginx: configuration file /etc/nginx/nginx.conf test is successful
Create a new sample PHP application file
test.php
in the web root directory/var/www/html
specified in your Nginx configuration.console$ sudo nano /var/www/html/test.php
Add the following contents to the file.
php<?php phpinfo(); ?>
Save and close the file.
The above PHP application displays the available PHP version information on your server when accessed in a web browser.
Stop the
apache2
service that's pre-installed with PHP to unbind the HTTP port80
.console$ sudo systemctl stop apache2
Allow connections to the HTTP port
80
through the default UFW firewall utility.console$ sudo ufw allow 80
Restart Nginx to apply your configuration changes.
console$ sudo systemctl restart nginx
Access your server IP address using a web browser such as chrome and load the
/test.php
URL path to access your PHP application.http://SERVER-IP/test.php
Install Multiple PHP Versions
To use multiple PHP versions, add the ppa:ondrej/php
repository. This allows installing PHP versions like 8.1 that aren't available in Ubuntu 20.04's default repositories.
Add the
ppa:ondrej/php
PPA to your APT repositories.console$ sudo add-apt-repository -y ppa:ondrej/php
Install a specific PHP and PHP-FPM version on your server. For example,
PHP 8.1
.console$ sudo apt install -y php8.1 php8.1-fpm
View the installed PHP version information on your server.
console$ php8.1 -v
Output:
PHP 8.1.32 (cli) (built: Mar 13 2025 18:26:45) (NTS) Copyright (c) The PHP Group Zend Engine v4.1.32, Copyright (c) Zend Technologies with Zend OPcache v8.1.32, Copyright (c), by Zend Technologies
View the PHP version's PHP-FPM service status and verify that it's running. For example,
php8.1-fpm
.console$ sudo systemctl status php8.1-fpm
Output:
● php8.1-fpm.service - The PHP 8.1 FastCGI Process Manager Loaded: loaded (/lib/systemd/system/php8.1-fpm.service; enabled; vendor preset: enabled) Active: active (running) since Sun 2025-04-06 16:04:43 UTC; 57s ago Docs: man:php-fpm8.1(8) Process: 35358 ExecStartPost=/usr/lib/php/php-fpm-socket-helper install /run/php/php-fpm.sock /etc/php/8.1/fpm/pool.d/www.conf 81 (code=exited, status=0/SUCCESS) Main PID: 35347 (php-fpm8.1) Status: "Processes active: 0, idle: 2, Requests: 0, slow: 0, Traffic: 0req/sec" Tasks: 3 (limit: 9415) Memory: 7.5M CGroup: /system.slice/php8.1-fpm.service ├─35347 php-fpm: master process (/etc/php/8.1/fpm/php-fpm.conf) ├─35356 php-fpm: pool www └─35357 php-fpm: pool www
Create a new web root directory to store different PHP application files. For example,
/var/www/php81-site
.console$ sudo mkdir -p /var/www/php81-site
Copy the
test.php
PHP application file you created earlier to the directory asindex.php
.console$ sudo cp /var/www/html/test.php /var/www/php81-site/index.php
Create a new Nginx virtual host configuration to test access to your installed PHP-FPM version. For example,
php81-site.conf
.console$ sudo nano /etc/nginx/sites-available/php81-site.conf
Add the following configurations to the file.
iniserver { listen 9000; listen [::]:9000; root /var/www/php81-site; index index.php index.html; location ~ \.php$ { try_files $uri =404; fastcgi_pass unix:/run/php/php8.1-fpm.sock; fastcgi_index index.php; include fastcgi.conf; } }
Save and close the file.
This Nginx setup listens on port
9000
, serves files from/var/www/php81-site
, and uses theindex
directive for default files. It forwards.php
requests to the PHP 8.1 FPM socket at/run/php/php8.1-fpm.sock
.Link the Nginx configuration file to the
/etc/nginx/sites-enabled
activate the new virtual host.console$ sudo ln -s /etc/nginx/sites-available/php81-site.conf /etc/nginx/sites-enabled/
Test the Nginx configuration for errors.
console$ sudo nginx -t
Output:
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok nginx: configuration file /etc/nginx/nginx.conf test is successful
Allow network connections to the virtual host TCP port
9000
through the firewall.console$ sudo ufw allow 9000/tcp
Reload the firewall rules to apply changes.
console$ sudo ufw reload
Restart Nginx to apply your configuration changes.
console$ sudo systemctl restart nginx
Access your server IP on port
9000
in a new web browser window to open yourindex.php
web application file.http://SERVER-IP:9000
Verify that your PHP 8.1 version information and modules display in your web browser.
Access the
/test.php
URL path using your server IP in a new web browser tab to verify that your default PHP7.4
version processes requests separately based on your Nginx configuration.
Conclusion
You have installed PHP and PHP-FPM on your Ubuntu 20.04 server, enabling multiple PHP versions for different web applications based on project needs. PHP and PHP-FPM can be integrated with applications like Nginx to handle dynamic web requests securely.
No comments yet.