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

This step-by-step tutorial will show you how to install and configure Nginx to execute PHP on your server using PHP-FPM.

Nginx + PHP is one of the most popular software groups 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 webserver 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 handle 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, installing it from these repositories using the apt packaging system is possible.

sudo apt install nginx

CentOS / AlmaLinux / Rocky Linux

To get the latest Nginx version, CentOS, AlmaLinux, or Rocky Linux users need first to install the EPEL repository.

EPEL (Extra Packages for Enterprise Linux) is an open-source and free community-based repository project from the Fedora team which provides high-quality add-on software packages for RHEL-based Linux distros.

sudo yum install epel-release

Then you can install the Nginx server.

sudo yum install nginx

Fedora

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

sudo dnf install nginx

Related: How to Install Nginx Web Server on Linux

2. Installing PHP-FPM

Ubuntu / Debian

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

sudo apt install php-fpm

CentOS

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

CentOS 7

sudo yum install http://rpms.remirepo.net/enterprise/remi-release-7.rpm
sudo yum-config-manager --enable remi-php74
sudo yum install php php-fpmCode language: JavaScript (javascript)

CentOS 8

sudo yum install http://rpms.remirepo.net/enterprise/remi-release-8.rpm
sudo yum-config-manager --enable remi-php74
sudo yum install php php-fpmCode language: JavaScript (javascript)

3. Configuring NGINX to Execute PHP Using PHP-FPM

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

We’ll need to create an Nginx server block configuration file to run PHP with FPM. After that, you can create a new VirtualHost as per your requirements, enabling any new VirtualHost.

sudo vim /etc/nginx/sites-available/example.com

Replace example.com with your siteโ€™s domain or IP address, and the root directive with your site’s root directory.

<strong>server</strong> {
<strong>ย ย ย ย ย ย ย ย listen</strong> 80;
<strong>ย ย ย ย ย ย ย ย root</strong> /var/www/html;
<strong>ย ย ย ย ย ย ย ย index</strong> index.php index.html index.htm;
<strong>ย ย ย ย ย ย ย ย server_name</strong> example.com;
ย 
<strong>ย ย ย ย ย ย ย ย location</strong> / {
<strong>ย ย ย ย ย ย ย ย ย ย ย ย try_files</strong> $uri $uri/ =404;
ย ย ย ย ย ย ย ย }
ย 
<strong>ย ย ย ย ย ย ย ย location</strong> ~ \.php$ {
<strong>ย ย ย ย ย ย ย ย ย ย ย ย include</strong> snippets/fastcgi-php.conf;
<strong>ย ย ย ย ย ย ย ย ย ย ย ย fastcgi_pass</strong> unix:/var/run/php/php7.4-fpm.sock;
ย ย ย ย ย ย ย ย }
}Code language: HTML, XML (xml)

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

sudo ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/example.com 

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/Code language: JavaScript (javascript)
total 4
-rw-r--r-- 1 root     root      5 Dec  1 17:43 php7.4-fpm.pid
srw-rw---- 1 www-data www-data  0 Dec  1 17:43 php7.4-fpm.sock
lrwxrwxrwx 1 root     root     30 Dec  1 17:43 php-fpm.sock -> /etc/alternatives/php-fpm.sockCode language: JavaScript (javascript)

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 the Nginx service to reload the changes.

sudo systemctl restart nginx.serviceCode language: CSS (css)

4. Testing the Setup

Create a test PHP file to verify that 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.phpCode language: PHP (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

Conclusion

Congratulations! Youโ€™ve set up Nginx to handle PHP requests through PHP-FPM.

There are some next steps you could take from here. For example, you should ensure that connections to your server are secured. But how to do it?

Follow our step-by-step tutorial to acquire a free Letโ€™s Encrypt SSL certificate for your server.

Bobby Borisov

Bobby Borisov

Bobby, an editor-in-chief at Linuxiac, is a Linux professional with over 20 years of experience. With a strong focus on Linux and open-source software, he has worked as a Senior Linux System Administrator, Software Developer, and DevOps Engineer for small and large multinational companies.

Think You're an Ubuntu Expert? Let's Find Out!

Put your knowledge to the test in our lightning-fast Ubuntu quiz!
Ten questions to challenge yourself to see if you're a Linux legend or just a penguin in the making.

1 / 10

Ubuntu is an ancient African word that means:

2 / 10

Who is the Ubuntu's founder?

3 / 10

What year was the first official Ubuntu release?

4 / 10

What does the Ubuntu logo symbolize?

5 / 10

What package format does Ubuntu use for installing software?

6 / 10

When are Ubuntu's LTS versions released?

7 / 10

What is Unity?

8 / 10

What are Ubuntu versions named after?

9 / 10

What's Ubuntu Core?

10 / 10

Which Ubuntu version is Snap introduced?

The average score is 68%