How to Serve PHP with PHP-FPM and NGINX

Learn how to serve PHP with PHP-FPM and NGINX for better performance, security, and scalability. Follow this step-by-step setup guide.

When it comes to serving PHP applications in a high-performance, scalable, and secure manner, using PHP-FPM (FastCGI Process Manager) with NGINX is the preferred solution for many system administrators and developers. Unlike traditional configurations that rely on Apache’s mod_php, this setup leverages NGINX as a reverse proxy and PHP-FPM as the FastCGI handler, resulting in better resource management, improved security, and increased flexibility. Whether you’re hosting a WordPress website, Laravel application, or any other PHP-based project, understanding how to properly configure PHP-FPM with NGINX is crucial to achieving optimal performance.

This guide will walk you through the complete process of setting up PHP with PHP-FPM and NGINX, including basic and advanced configurations. By the end of this article, you’ll have a fully functional and optimized PHP environment ready to handle high traffic with efficiency.

How to Serve PHP with PHP-FPM and NGINX


Why Use PHP-FPM with NGINX?

Before we dive into the setup, let’s explore some key benefits of using PHP-FPM with NGINX:

  1. Better Performance: NGINX is event-driven and handles multiple concurrent connections efficiently. Combined with PHP-FPM, which manages PHP processes separately, this setup significantly improves response times.
  2. Lower Resource Usage: Unlike Apache’s prefork model, which spawns PHP processes for every request, PHP-FPM maintains a pool of PHP worker processes, reducing memory consumption.
  3. Enhanced Security: Running PHP as a separate user under PHP-FPM provides better isolation and security compared to running PHP within the web server.
  4. Granular Process Management: PHP-FPM allows you to configure pools for different websites, each with its own settings, improving resource allocation.
  5. Scalability: This configuration handles high traffic efficiently, making it suitable for large-scale applications.
See also  How to Install ioncube on Linux CentOS/Oracle Linux 7

Step 1: Install NGINX and PHP-FPM

To begin, install NGINX and PHP-FPM on your Linux server. Here’s how you can do it on Ubuntu/Debian:

sudo apt update
sudo apt install nginx php-fpm

For CentOS/RHEL, use:

sudo yum install epel-release
sudo yum install nginx php-fpm

Once installed, start and enable both services:

sudo systemctl start nginx
sudo systemctl enable nginx
sudo systemctl start php-fpm
sudo systemctl enable php-fpm

Step 2: Configure PHP-FPM

By default, PHP-FPM runs as the www-data user and listens on a Unix socket (/run/php/php-fpm.sock). You can verify this by checking the PHP-FPM pool configuration file:

sudo nano /etc/php/8.1/fpm/pool.d/www.conf  # Adjust the PHP version accordingly

Ensure the following lines are correctly set:

listen = /run/php/php8.1-fpm.sock
listen.owner = www-data
listen.group = www-data
listen.mode = 0660

If you want PHP-FPM to listen on a TCP socket instead (useful for load balancing):

listen = 127.0.0.1:9000

Restart PHP-FPM for changes to take effect:

sudo systemctl restart php-fpm

Step 3: Configure NGINX to Use PHP-FPM

Now, configure NGINX to process PHP requests via PHP-FPM. Open the default site configuration:

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

Modify the server block to include the following PHP handler:

server {
    listen 80;
    server_name example.com;
    root /var/www/html;
    index index.php index.html index.htm;

    location / {
        try_files $uri $uri/ =404;
    }

    location ~ \.php$ {
        include fastcgi_params;
        fastcgi_pass unix:/run/php/php8.1-fpm.sock;  # Adjust the PHP version accordingly
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    }

    location ~ /\.ht {
        deny all;
    }
}

Test and restart NGINX:

sudo nginx -t
sudo systemctl restart nginx

Advanced PHP-FPM Configuration

For high-traffic websites, fine-tuning PHP-FPM is essential. Edit the www.conf pool file:

sudo nano /etc/php/8.1/fpm/pool.d/www.conf

Modify the following parameters for better performance:

pm = dynamic
pm.max_children = 50
pm.start_servers = 10
pm.min_spare_servers = 5
pm.max_spare_servers = 15
pm.process_idle_timeout = 10s;
pm.max_requests = 500
  • pm = dynamic allows PHP-FPM to scale dynamically.
  • pm.max_children controls the max number of PHP processes.
  • pm.max_requests restarts workers after serving 500 requests to prevent memory leaks.
See also  Why are Apache event MPM and PHP-FPM recommended for Production?

Restart PHP-FPM:

sudo systemctl restart php-fpm

Monitoring and Troubleshooting

To check PHP-FPM status:

sudo systemctl status php-fpm

Check logs for errors:

sudo journalctl -u php-fpm --no-pager | tail -n 20
sudo journalctl -u nginx --no-pager | tail -n 20

Use htop or top to monitor resource usage in real-time:

htop

Conclusion

Running PHP with PHP-FPM and NGINX provides a fast, secure, and efficient way to serve PHP applications. By properly configuring PHP-FPM pools and optimizing NGINX, you can significantly improve performance, reduce resource usage, and ensure your server can handle high traffic loads. This setup is ideal for hosting WordPress, Laravel, or any PHP-based web application with modern scalability requirements.

By following this guide, you now have a fully functional PHP environment with advanced configurations. Continue monitoring and tweaking settings based on your workload to achieve the best results. Happy hosting!

Leave a Comment