How to Configure Nginx to Work with PHP via PHP-FPM

How To Configure Nginx To Work With PHP Via PHP-FPM

Nginx + PHP is one of the most popular groups of software that you can use to build your website. This step-by-step tutorial will show you how to install and configure Nginx to execute PHP on your server using PHP-FPM.

Nginx is the ideal combination with PHP-FPM. It’s a stable web server recognized for its impressive performance and low resource-consumption.

PHP runs as an isolated service when you use PHP-FPM. Employing this PHP version as the language interpreter means requests will be processed via a unix socket. Nginx server will handles HTTP requests only, while PHP-FPM interprets the PHP code.

1. Installing Nginx

Ubuntu / Debian

Because Nginx is available in Debian’s and Ubuntu‘s default repositories, it is possible to install it from these repositories using the apt packaging system.

sudo apt install nginx


To get the latest Nginx version, CentOS users need first to install the EPEL repository. It contains additional software for the CentOS.

sudo yum install epel-release

Then you can install the Nginx server.

sudo yum install nginx


Nginx is available on the default Fedora repositories and hence can be installed directly using the dnf package manager.

sudo dnf install nginx

2. Installing PHP-FPM

Ubuntu / Debian

To install PHP on Ubuntu or Debian, just run the following command:

sudo apt install php-fpm


The PHP version available by default within CentOS servers is outdated. For that reason, we’ll need to install a third-party package repository in order to obtain PHP 7.

CentOS 7

sudo yum install
sudo yum-config-manager --enable remi-php74
sudo yum install php php-fpm

CentOS 8

sudo yum install
sudo yum-config-manager --enable remi-php74
sudo yum install php php-fpm

3. Configuring NGINX to execute PHP using FPM-FPM

Once Nginx and PHP are installed, we can configure Nginx to send PHP requests off to PHP-FPM.

Now, we’ll need to create a Nginx server block configuration file to run PHP with FPM. You can create new VirtualHost as per your requirements, so make sure to enable any new VirtualHost.

sudo vim /etc/nginx/sites-available/

Replace with your site’s domain or IP address, and the root directive with your site’s root directory.

server {
        listen 80;
        root /var/www/html;
        index index.php index.html index.htm;
        location / {
            try_files $uri $uri/ =404;
        location ~ \.php$ {
            include snippets/fastcgi-php.conf;
            fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;

Save your changes to the configuration file and create a link to site enabled directory.

sudo ln -s /etc/nginx/sites-available/ /etc/nginx/sites-enabled/ 

Make sure that the name of the PHP socket file (in our case php7.4-fpm.sock) specified in the fastcgi_pass directive is correct.

ls -l /var/run/php/
total 4
-rw-r--r-- 1 root     root      5 мар 29 19:18
srw-rw---- 1 www-data www-data  0 мар 29 19:18 php7.4-fpm.sock
lrwxrwxrwx 1 root     root     30 мар 29 19:18 php-fpm.sock -> /etc/alternatives/php-fpm.sock

If the name is different from php7.4-fpm.sock, just replace it in the server block shown above with the current one from your server.

Then restart Nginx service to reload the changes.

sudo systemctl restart nginx.service

4. Testing the setup

Create a test PHP file so you can verify PHP-FPM works and is integrated with Nginx. In the Server Block above, our site is being served from /var/www/html, so we’ll create a test file there:

echo "<?php phpinfo(); ?>" | sudo tee /var/www/html/info.php

Finally, access info.php from a web browser, using your site’s domain or server’s IP address. You should see the PHP configuration page:

PHP info page


Congratulations! You’ve set up Nginx to handle PHP requests through PHP-FPM.

There are a number of next steps you could take from here. For example, you should ensure that connections to your server are secured. But how to do it? Just follow our step-by-step tutorial to acquire a free Let’s Encrypt SSL certificate for your server.

If this guide has helped you, please consider buying us a coffee.

Buy me a coffee!

Your support and encouragement are greatly appreciated!

Leave a Reply

Your email address will not be published.

Latest from Tutorials