Sling Academy
Home/DevOps/NGINX, PHP, and PHP-FPM: The Developer’s Guide

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

Last updated: January 20, 2024

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.

Next Article: NGINX & Let’s Encrypt: The Complete Guide

Previous Article: NGINX & FastCGI: Tutorial with Examples

Series: NGINX Tutorials

DevOps

You May Also Like

  • How to reset Ubuntu to factory settings (4 approaches)
  • Making GET requests with cURL: A practical guide (with examples)
  • Git: What is .DS_Store and should you ignore it?
  • NGINX underscores_in_headers: Explained with examples
  • How to use Jenkins CI with private GitHub repositories
  • Terraform: Understanding State and State Files (with Examples)
  • SHA1, SHA256, and SHA512 in Terraform: A Practical Guide
  • CSRF Protection in Jenkins: An In-depth Guide (with examples)
  • Terraform: How to Merge 2 Maps
  • Terraform: How to extract filename/extension from a path
  • JSON encoding/decoding in Terraform: Explained with examples
  • Sorting Lists in Terraform: A Practical Guide
  • Terraform: How to trigger a Lambda function on resource creation
  • How to use Terraform templates
  • Understanding terraform_remote_state data source: Explained with examples
  • Jenkins Authorization: A Practical Guide (with examples)
  • Solving Jenkins Pipeline NotSerializableException: groovy.json.internal.LazyMap
  • Understanding Artifacts in Jenkins: A Practical Guide (with examples)
  • Using Jenkins with AWS EC2 and S3: A Practical Guide