How to Serve PHP with PHP-FPM and NGINX

PHP-FPM (FastCGI Process Manager) is a specific implementation of FastCGI for PHP application. It provides a better way to manage and scale PHP processes separately from the web server. In combination with the NGINX web server, PHP-FPM can help you serve PHP applications with improved performance, reliability and more efficient use of server resources. While Serving PHP with PHP-FPM, the web server can communicate with a pool of PHP processes managed by the PHP-FPM daemon, which can handle multiple requests concurrently. In this tutorial, we will show you how to install, set up, and serve PHP with PHP-FPM and NGINX.

How to Serve PHP with PHP-FPM and NGINX

Steps How to Serve PHP with PHP-FPM and NGINX

Step 1:

Install NGINX :
The first step is to install the NGINX web server. On Ubuntu, you can do this with the following command:

# sudo apt-get update
# sudo apt-get install nginx

On CentOS, you can use the following command:

# sudo yum install epel-release
# sudo yum install nginx
# sudo dnf install epel-release
# sudo dnf install nginx

Step 2:

Install PHP-FPM:
Next, we need to install PHP-FPM. On Ubuntu, you can use the following command:

# sudo apt-get install php-fpm

On CentOS, you can use the following command:

sudo yum install php-fpm
sudo dnf install php-fpm

Step 3:

Configure PHP-FPM:

Now we need to configure PHP-FPM. We will edit the www.conf file to make sure PHP-FPM is set up to work with NGINX. On Ubuntu, the file is located at /etc/php/7.4/fpm/pool.d/www.conf. On CentOS, the file is located at /etc/php-fpm.d/www.conf.

Open the file in your favorite text editor and make the following changes:

listen = /run/php/php7.4-fpm.sock
listen.owner = www-data
listen.group = www-data
listen.mode = 0660

These settings specify that PHP-FPM will listen on a Unix socket, and that the socket file should be owned by the www-data user and group.

See also  What PHP Is Used For: A Comprehensive Guide

Step 4:

Configure NGINX to use PHP-FPM:

Next, we need to configure NGINX to use PHP-FPM. We will create a new server block in the NGINX configuration file to handle PHP requests. On Ubuntu, the file is located at /etc/nginx/sites-available/default. On CentOS, the file is located at /etc/nginx/conf.d/default.conf.

Open the file in your favorite text editor and add the following lines:

location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php7.4-fpm.sock;
}

These lines tell NGINX to use PHP-FPM to handle requests for PHP files.

Step 5:

Test your configuration:

Finally, we need to test our configuration. Restart both the NGINX and PHP-FPM services with the following commands:

# sudo systemctl restart nginx
# sudo systemctl restart php7.4-fpm

Create a new PHP file in your web server’s document root directory with the following code:

<?php
phpinfo();
?>

Visit the PHP file in your web browser, and you should see a page displaying information about your PHP installation.

Conclusion:

Serving PHP with PHP-FPM and NGINX is a powerful combination that can help to deliver high-performance and reliable web applications. By utilizing the FastCGI protocol, NGINX can communicate with the PHP-FPM process manager to handle PHP requests efficiently and with low overhead.

Through the steps outlined in this blog, you can get started with serving PHP using PHP-FPM and NGINX quickly and easily. You can optimize your PHP applications by configuring PHP-FPM to manage processes efficiently and reduce memory usage, while also using NGINX’s caching and load balancing features to improve performance and scalability.

Overall, PHP-FPM and NGINX provide a robust and efficient way to serve PHP applications.

Leave a Comment