Support Online
Skip to main content

NGINX PHP-FPM Configuration: Secure and High Performance PHP

NGINX cannot run PHP files directly.
PHP-FPM fills this gap and manages PHP operations fast and secure.

In this guide, you will learn the working logic of PHP-FPM and its correct integration with NGINX.

What Will You Learn in This Guide?

  • How PHP-FPM works
  • PHP-FPM installation
  • Creating a private FPM pool
  • FastCGI connection with NGINX
  • 502 error and performance tips

Technical Summary

This guide describes NGINX + PHP-FPM integration on Ubuntu-based systems.
The goal is to run PHP applications with high performance and isolation.

Scope:

  • PHP-FPM installation
  • Pool configuration
  • NGINX FastCGI settings
  • Testing and optimization

How Does PHP-FPM Work?

PHP-FPM runs PHP as a daemon.

The flow is as follows:

  1. NGINX receives PHP request
  2. Transmits to PHP-FPM via FastCGI
  3. PHP-FPM selects appropriate worker process
  4. Output returns to NGINX

Process Types

  • Master Process: Manages Worker processes
  • Worker Process: Runs PHP code

This structure reduces memory leaks and preserves performance.


Prerequisites

  • Ubuntu server (example: tr1-node01)
  • NGINX installed
  • PHP installed
  • SSH access

1. PHP-FPM Installation

apt install php-fpm -y
  • This command installs the PHP-FPM service.


systemctl status php-fpm
  • This command verifies that the service is running.

2. Creating a Custom PHP-FPM Pool

  1. Separate pools are recommended for each application.
  • This method increases security and source control.

Creating Users


groupadd wordpress_user
useradd -g wordpress_user wordpress_user
  • This command creates an application-specific user.

Pool Configuration


nano /etc/php/*/fpm/pool.d/wordpress_pool.conf

[wordpress_site]
user = wordpress_user
group = wordpress_user

listen = /var/run/php-fpm-wordpress.sock
listen.owner = www-data
listen.group = www-data

pm = dynamic
pm.max_children = 50
pm.start_servers = 5
pm.min_spare_servers = 5
pm.max_spare_servers = 20
pm.max_requests = 500
  • These settings create a dynamic and balanced pool.

💡 Tip:

  1. Each PHP process consumes an average of 30–50 MB of RAM.
  2. Adjust pm.max_children accordingly.

systemctl restart php-fpm
  • This command applies the new settings.

3. NGINX PHP-FPM Configuration


server {
listen 80;
server_name ornek.com;
root /var/www/wordpress;

index index.php index.html;

location / {
try_files $uri $uri/ /index.php?$args;
}

location ~ \.php$ {
include fastcgi.conf;
fastcgi_pass unix:/var/run/php-fpm-wordpress.sock;
}
}
  • This configuration directs PHP requests to the correct pool.


nginx -t
systemctl restart nginx
  • This command verifies the configuration.

4. Testing the Configuration


echo "<?php phpinfo(); ?>" > /var/www/wordpress/info.php
  • This file shows PHP-FPM user information.

Open from browser:


http://ornek.com/info.php
  • If the USER value is wordpress_user, the configuration is correct.

Frequently Asked Questions (FAQ)

1. What causes 502 Bad Gateway error? PHP-FPM is not working or the socket path is incorrect.

2. Should I use socket or TCP port? On the same server, socket is faster.

3. How do I improve performance? Adjust pm.max_children according to RAM.

4. Can I run more than one PHP version? Yes, you can create separate pools for each version.


Result

Using NGINX with PHP-FPM:

Consumes less resources

Isolates applications

Provides stability in high traffic

You can safely apply this configuration on your servers in GenixNode infrastructure 🚀