How to configure PHP on Nginx with fastCGI sample (PHP-FPM)

Why are Nginx, PHP and fastCGI so popular?
Nginx is the most popular http web server in the DevOps community. And developers love the PHP programming language because it lets them build and deploy interactive websites quickly.
As such, it’s no wonder so many sysadmins need to configure Nginx, PHP, and PHP-FPM on Linux and Windows servers.
This quick tutorial shows you how to configure PHP and Nginx on Ubuntu Linux with the fastCGI Process Manager (PHP-FPM) configured as Nginx’s PHP engine.
How to configure Nginx, PHP and PHP-FPM
To install and configure fastCGI (FPM), PHP and Nginx on Ubuntu Linux, follow these steps:
- Update apt-get to ensure access to the latest packages.
- Install Nginx on Ubuntu.
- Install the php-fpm package for Nginx.
- Change server default configuration file to support PHP in Nginx.
- Restart the Nginx server configured in PHP.
- Add PHP file to Nginx html phone book.
- Test PHP, Nginx and PHP-FPM configuration.
Download the latest Nginx and PHP packages
Every software installation in Ubuntu should start with a quick apt-get update
and possibly a apt-get upgrade
ordered.
sudo apt-get update -y Reading package lists... Done sudo apt-get upgrade -y Calculating upgrade... Done
Install Nginx on Ubuntu
To install PHP on Nginx, you must first install Nginx, which you can achieve through a simple apt-get install
ordered:
sudo apt-get install nginx -y The following Nginx packages will be installed: libnginx-mod-http-geoip2 nginx-common nginx-core Setting up nginx 1.18.0-6 ubuntu... Done
Check Running Nginx Server
To verify the successful installation and configuration of Nginx on Ubuntu, query the status of the HTTP server:
sudo systemctl status nginx ● nginx.service - A high performance web server and a reverse proxy server Loaded: loaded (/lib/systemd/system/nginx.service; enabled;) Active: active (Nginx running)
You can visually verify that the Nginx landing page is showing on http://localhost:80 of a web browser.
Install PHP for Nginx with PHP-FPM
To install PHP for Nginx, use the PHP-FPM library. You can install PHP-FPM support with another apt-get install
ordered:
sudo apt-get install php8.1-fpm -y
In this case, we have installed version 8.1 of the PHP and PHP-FPM packages.
A common mistake is to install the PHP package, not PHP-FPM. The problem with this approach is that unlike PHP-FPM, the PHP package installs the Apache HTTP server and its httpd process, which conflicts with Nginx.
Why does a basic PHP installation require Apache?
PHP requires one of three dependencies to exist on a machine:
- libapache2-mod-php
- php-fpm
- php-cgi
A simple PHP installation uses the default libapache2-mod-php module, which requires installing the full Apache HTTP server software suite. To avoid this, install the php-cgi or php-fpm module for Nginx.
Check if PHP-FPM is running
After PHP-FPM setup is complete, check if it is running:
sudo systemctl status php8.1-fpm ● php8.1-fpm.service - PHP 8.1 FastCGI Process Manager FPM for Ubuntu Loaded: loaded (/lib/systemd/system/php8.1-fpm.service) Active: active (php-fpm running)
Add PHP support to Nginx
With Nginx and PHP-FPM installed, you need to modify the default Nginx configuration file. This allows the PHP FastCGI Process Manager to handle requests that have a .php extension.
The default Nginx file can be opened with any text editor. This command will open it with Nano:
sudo nano /etc/nginx/sites-available/default
Make the following changes to the Nginx configuration to support PHP and PHP-FPM on the server:
- Add index.php to the index list.
- Uncomment PHP scripts in the FastCGI input block.
- Uncomment the line to include snippets/fastcgi-php.conf.
- Uncomment the line to enable fastcgi_pass and php8.1-fpm. sock.
- Uncomment the section to deny all access to Apache .htaccess files.
To configure PHP, Nginx and FTP (fastCGI), you need to update the Nginx configuration file.
Enable PHP in Nginx configuration file
The server section of the Nginx, PHP, and PHP-FPM configuration file will look like this when complete. Changes are highlighted in bold:
server { # Example PHP Nginx FPM config file listen 80 default_server; listen [::]:80 default_server; root /var/www/html; # Add index.php to setup Nginx, PHP & PHP-FPM config index index.php index.html index.htm index.nginx-debian.html; server_name _; location / { try_files $uri $uri/ =404; } # pass PHP scripts on Nginx to FastCGI (PHP-FPM) server location ~ .php$ { include snippets/fastcgi-php.conf; # Nginx php-fpm sock config: fastcgi_pass unix:/run/php/php8.1-fpm.sock; # Nginx php-cgi config : # Nginx PHP fastcgi_pass 127.0.0.1:9000; } # deny access to Apache .htaccess on Nginx with PHP, # if Apache and Nginx document roots concur location ~ /.ht { deny all; } } # End of PHP FPM Nginx config example
How to Validate an Nginx Configuration File
The following command commits the updated Nginx configuration file to ensure the changes don’t create syntax errors:
sudo nginx -t nginx php config: the configuration file /etc/nginx/nginx.conf syntax is ok nginx php-fpm config: configuration file /etc/nginx/nginx.conf test is successful
To enable the Nginx PHP fastCGI configuration, restart the server:
sudo systemctl restart nginx
Create a PHP page in Nginx
After reboot, PHP is fully enabled on Nginx. To prove it, create a PHP file in Nginx /var/www/html folder and test to make sure the page displays correctly on the server.
You may need to change the permissions on the folder with a CHMOD
command to create a file:
sudo chmod -R 777 /var/www/html
Then add a new PHP file to the Nginx web hosting directory. The easiest way to do this is to perform a quick echo
ordered:
echo "" >> /var/www/html/info.php
This creates the most basic PHP file outside of a “Hello World” example you might create. If you don’t like the echo
command, use an editor to create a file named info.php in the /var/www/html folder with the following content:
phpinfo();
?>
Test Nginx, PHP integration
With Nginx setup, PHP and PHP-FPM module completed, and a new file named info.php added to the web server, just open a browser to http://localhost/info.php to test the configuration. The PHP info page, attesting to the fact that installing PHP 8.1 on Nginx, will appear.

When PHP, FPM and Nginx are fully configured, the server will be able to render PHP pages.
Commands to configure PHP and Nginx in Ubuntu
Let’s quickly review this PHP and Nginx tutorial. Here are all the commands we used to enable the fastCGI process manager for PHP in Nginx:
-
sudo apt-get update -y
-
sudo apt-get upgrade -y
-
sudo apt-get install nginx -y
-
sudo systemctl status nginx
-
sudo apt-get install php8.1-fpm -y
-
sudo systemctl status php8.1-fpm
-
sudo nano /etc/nginx/sites-available/default
-
sudo nginx -t
-
sudo systemctl restart nginx
-
sudo chmod -R 777 /var/www/html
-
echo "" >> /var/www/html/info.php
And it’s that easy to install and configure fastCGI (FTP), Nginx and PHP support on your web server.