How to Configure PHP-FPM with NGINX
Introduction
PHP-FPM (FastCGI Process Manager) is an alternative to FastCGI implementation of PHP with some additional features useful for sites with high traffic. It is the preferred method of processing PHP pages with NGINX and is faster than traditional CGI based methods such as SUPHP or mod_php for running a PHP script. The main advantage of using PHP-FPM is that it uses a considerable amount of less memory and CPU as compared with any other methods of running PHP. The primary reason is that it demonizes PHP, thereby transforming it to a background process while providing a CLI script for managing PHP request.
PHP-FPM NGINX Configuration Prerequisites
- You can open a SSH session to your Ubuntu 18.04 system using root or a sudo enabled user.
- You have already installed NGINX and PHP in your Ubuntu 18.04 system.
NGINX PHP-FPM Configuration Steps
1. Install PHP-FPM
Nginx doesn’t know how to run a PHP script of its own. It needs a PHP module like PHP-FPM to efficiently manage PHP scripts. PHP-FPM, on the other hand, runs outside the NGINX environment by creating its own process. Therefore when a user requests a PHP page the nginx server will pass the request to PHP-FPM service using FastCGI. The installation of php-fpm in Ubuntu 18.04 depends on PHP and its version. Check the documentation of installed PHP before proceeding with installing FPM in your server. Assuming you have already installed the latest PHP 7.3, then you can install FPM using the following apt-get command.
# apt-get install php7.3-fpm
The FPM service will start automatically, once the installation is over. You can verify that using the following systemd command:
# systemctl status php7.3-fpm
● php7.3-fpm.service - The PHP 7.3 FastCGI Process Manager
Loaded: loaded (/lib/systemd/system/php7.3-fpm.service; enabled; vendor preset: enabled)
Active: active (running) since Sun 2019-02-17 06:29:31 UTC; 30s ago
Docs: man:php-fpm7.3(8)
Main PID: 32210 (php-fpm7.3)
Status: "Processes active: 0, idle: 2, Requests: 0, slow: 0, Traffic: 0req/sec"
Tasks: 3 (limit: 1152)
CGroup: /system.slice/php7.3-fpm.service
├─32210 php-fpm: master process (/etc/php/7.3/fpm/php-fpm.conf)
├─32235 php-fpm: pool www
└─32236 php-fpm: pool www
2. Configure the Pool
The php-fpm service creates a default pool, the to Configure PHP-FPM (www.conf) for which can be found in /etc/php/7.3/fpm/pool.d folder. You can customize the default pool as per your requirements. But it is a standard practice to create separate pools to have better control over resource allocation to each FPM processes. Furthermore, segregating FPM pool will enable them to run independently by creating its own master process. That means each php application can be configured with its own cache settings using PHP-FPM. A change in one pool’s configuration does not require you to start or stop the rest of the FPM pools. Let us create an FPM pool for running a PHP application effectively through a separate user.
3. Configure NGINX for PHP-FPM
Now create an NGINX server block that will make use of the above FPM pool. To do that, edit your NGINX configuration file and pass the path of pool’s socket file using the option fastcgi_pass inside location block for php.
server {
listen 80;
server_name example.journaldev.com;
root /var/www/html/wordpress;
access_log /var/log/nginx/example.journaldev.com-access.log;
error_log /var/log/nginx/example.journaldev.com-error.log error;
index index.html index.htm index.php;
location / {
try_files $uri $uri/ /index.php$is_args$args;
}
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php7.2-fpm-wordpress-site.sock;
fastcgi_index index.php;
include fastcgi.conf;
}
}
4. Test PHP-FPM NGINX Configuration
To test if the above NGINX configuration file is indeed using the newly created FPM pool, create a php info file inside the web root. I have used /var/www/html/wordpress as a web root in the above NGINX configuration file. Adjust this value according to your environment.
# cd /var/www/html/wordpress
# echo "" > info.php
Once you are done with creating the PHP info page, point your favorite web browser to it. You will notice that the value of $_SERVER[‘USER’] and $_SERVER[‘HOME’] variable are pointing to wordpress_user and /home/wordpress_user respectively that we set in the FPM configuration file previously and thus confirms that the NGINX is serving the php pages using our desired FPM pool.
Summary
In this article, we learned how to install php-fpm and configure separate pools for different users and applications. We also learned how to configure an NGINX server block to connect to a PHP-FPM service. PHP-FPM provides reliability, security, scalability, and speed along with a lot of performance tuning options. You can now split the default PHP-FPM pool into multiple resource pools to serve different applications. This will not only enhance your server security but also enable you to allocate server resources optimally!