Support Online
Skip to main content

Nginx PHP-FPM Installation and Configuration

What will you learn in this guide?

This guide explains how to run PHP files safely and efficiently using PHP-FPM with Nginx on Ubuntu.
You will learn how to create separate PHP process pools for each application and establish the correct connection to Nginx.
In the last step you will verify that the system is working correctly by testing the configuration.

Technical Summary

Main topic: Running PHP with PHP-FPM on Nginx
Purpose: To solve the problem that Nginx cannot run PHP directly
Gain: Lower resource consumption, higher performance and isolation

Steps followed:

  1. PHP-FPM installation
  2. Creating an application-specific PHP-FPM pool
  3. Configuring Nginx with PHP-FPM socket
  4. Testing the installation

1. PHP-FPM Installation

  1. Nginx cannot run PHP files by itself.
    That's why PHP-FPM is used.
sudo apt-get install php8.1-fpm
  • This command installs PHP-FPM services for PHP 8.1.

  1. Check the service status after installation.

sudo systemctl status php8.1-fpm
  • This command shows whether the PHP-FPM service is active or not.

2. Creating a Custom PHP-FPM Pool

  1. Using a separate PHP pool for each site ensures security and performance. A problem on one site does not affect the others.

  2. Creating an application-specific user


sudo groupadd genixnode_app
sudo useradd -g genixnode_app genixnode_app
  • This user only works for the relevant PHP repository.

  1. Creating the pool configuration file

cd /etc/php/8.1/fpm/pool.d
sudo nano genixnode_pool.conf

  • This file contains the settings for the new PHP-FPM repository.

[genixnode_site]
user = genixnode_app
group = genixnode_app

listen = /var/run/php8.1-fpm-genixnode-site.sock
listen.owner = www-data
listen.group = www-data

php_admin_value[disable_functions] = exec,passthru,shell_exec,system
php_admin_flag[allow_url_fopen] = off

pm = dynamic
pm.max_children = 50
pm.start_servers = 5
pm.min_spare_servers = 5
pm.max_spare_servers = 35
pm.process_idle_timeout = 10s
  • This configuration creates an isolated PHP pool using dynamic transaction management.

  1. Apply changes

sudo systemctl restart php8.1-fpm
  • This command activates the new pool.

3. Configuring Nginx Server Block

  1. Nginx forwards PHP requests through the PHP-FPM socket.

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

index index.php index.html;

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

location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php8.1-fpm-genixnode-site.sock;
}
}
  • This configuration redirects requests with .php extension to the defined PHP-FPM pool.

Test the configuration and restart Nginx.


sudo nginx -t
sudo systemctl restart nginx
  • These commands check for configuration errors.

4. Testing the Configuration

  1. Create a simple PHP file to verify the installation.

echo "<?php phpinfo(); ?>" | sudo tee /var/www/genixnode_app/info.php
  • This file shows the PHP environment running via PHP-FPM.

  1. If the USER value in the Environment section is genixnode_app, the installation is successful.

  2. For security, delete info.php file after testing.


Frequently Asked Questions

1. Why should I use Unix Socket? It creates less latency than a TCP connection on the same server.

2. Why does the 502 Bad Gateway error occur? It usually occurs when the fastcgi_pass and listen paths do not match.

3. Can multiple PHP versions be run? Yes, separate PHP-FPM pools can be defined for each PHP version.

4. What is the difference between pm = dynamic and pm = static? Dynamic adjusts the number of transactions according to traffic. Static runs a fixed number of processes.


Result

When Nginx and PHP-FPM are used together on Ubuntu, a more secure and high-performance structure is achieved. Thanks to separate PHP pools, applications run isolated and resources are used more efficiently. You can try this architectural, modern and scalable structure on the GenixNode platform.