NGINX, PHP, and PHP-FPM: The Developer’s Guide

Updated: January 20, 2024 By: Guest Contributor Post a comment

Overview

This developer’s guide will take a deep dive into setting up a high-performance web server using NGINX, interfacing with PHP via PHP-FPM. We shall discuss what these components are, how they interact, and then walk through a step-by-step setup together with some code examples. This configuration offers a scalable and efficient environment for running PHP applications and delivers content quickly, handling high loads with ease.

What is NGINX?

NGINX is an open-source web server that is known for its high performance and low memory footprint. Beyond serving as a web or proxy server, it is often used for reverse proxying, caching, and load balancing.

What is PHP-FPM?

PHP FastCGI Process Manager (PHP-FPM) is an alternative PHP FastCGI implementation that has additional features useful for websites of any size, especially sites with high-traffic. It is highly customizable and can significantly increase the speed of PHP execution through a proper configuration.

Step-by-Step Guide

Step 1: Installing NGINX

Begin by updating your package list and installing NGINX:

sudo apt update
sudo apt install nginx

Once the installation is complete, you can start the NGINX service:

sudo systemctl start nginx
sudo systemctl enable nginx

Verify that NGINX is running by accessing your server’s IP address in a web browser; you should see the default NGINX welcome page.

Step 2: Installing PHP and PHP-FPM

Continuing with the setup, install PHP along with PHP-FPM. On Ubuntu systems, this would typically be:

sudo apt install php-fpm php-mysql

After installation, PHP-FPM will be started automatically. Verify it’s running:

sudo systemctl status php7.4-fpm

You might need to replace ‘7.4’ with the PHP version that got installed.

Step 3: Configuring NGINX and PHP-FPM

Edit the default NGINX server block configuration:

sudo nano /etc/nginx/sites-available/default

Within the ‘server’ block, add:

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

Make sure the ‘fastcgi_pass’ directive matches the PHP version installed.

Then, test to ensure no syntax errors:

sudo nginx -t

And reload NGINX to apply changes:

sudo systemctl reload nginx

Step 4: Creating a PHP file to test

Create a PHP file in your web server directory:

sudo nano /var/www/html/info.php

And insert the following PHP code:

<?php phpinfo(); ?>

When you navigate to http://your_server_ip/info.php, a PHP information page should appear, indicating successful integration.

Step 5: Tuning PHP-FPM

To enhance performance or adjust to high traffic, configure ‘/etc/php/7.4/fpm/pool.d/www.conf’ (again, replace ‘7.4’ with your installed PHP version).

Basic parameters to tune could be:

pm = dynamic
pm.max_children = 5
pm.start_servers = 2
pm.min_spare_servers = 1
pm.max_spare_servers = 3

This configuration determines how PHP-FPM allocates child processes. Afterwards, restart PHP-FPM to apply changes:

sudo systemctl restart php7.4-fpm

Step 6: Security Considerations

NGINX and PHP-FPM can expose potential security issues if not properly configured. It’s recommended to:

  • Apply restrictions on php-fpm to run as a non-root user.
  • Limit PHP access to essential directories using ‘open_basedir’.
  • Regularly apply updates to NGINX, PHP and system packages.

Conclusion

Integrating NGINX with PHP via PHP-FPM serves as a powerful stack for running PHP applications. You have a flexible and robust system designed to handle heavy loads efficiently. With proper tuning and security practices, this setup is suitable for both development and production environments.

For more advanced scenarios, explore NGINX’s full capabilities for load balancing and take advantage of PHP-FPM’s advanced options to fine-tune your PHP application performance to the optimal level.