Skip to main content

Command Palette

Search for a command to run...

Day 20: Configure Nginx + PHP-FPM Using Unix Sock | 100 Days of DevOps

Updated
4 min read
R
I’m currently working in DevOps and documenting my learning journey along the way. From CI/CD pipelines to cloud and containers, I’m exploring how modern systems are built and deployed. This blog is where I share what I learn, break down concepts in simple terms, and track my progress. Learning one concept at a time, and trying to apply it practically.

Content:

Today I worked on setting up a PHP application stack using Nginx and PHP-FPM on a CentOS server. The goal was to configure Nginx to serve PHP files by integrating it with PHP-FPM using a Unix socket.

This task reflects real-world DevOps practices where proper service integration and configuration are critical for application deployment.


What I Learned

  • How to configure Nginx for PHP applications
  • How PHP-FPM processes dynamic content
  • Why Unix sockets are preferred over TCP for local communication
  • Importance of correct permissions and service alignment

Steps I Followed :


1. Connected to Application Server

ssh steve@stapp02

Why? To perform configuration directly on the target application server.


2. Installed Nginx

sudo yum install nginx -y

Why? Nginx acts as the frontend web server handling HTTP requests.


3. Configured Nginx (Port & Document Root)

Opened config file:

sudo vi /etc/nginx/nginx.conf

Updated server block:

  • Port: 8092
  • Root: /var/www/html
  • Index: index.php index.html

Why? To meet application requirements and point to correct file location.


4. Restarted and Verified Nginx

sudo systemctl restart nginx
sudo systemctl status nginx

5. Verified Operating System

cat /etc/os-release

Output:

NAME="CentOS Stream"
VERSION="9"

Why? To confirm compatibility before installing PHP 8.1, since package availability depends on OS version.


6. Installed PHP 8.1 and PHP-FPM

sudo dnf install -y epel-release
sudo dnf install -y https://rpms.remirepo.net/enterprise/remi-release-9.rpm
sudo dnf module reset php -y
sudo dnf module enable php:remi-8.1 -y
sudo dnf install php php-fpm php-cli -y

Why? Default repos don’t provide required PHP version, so Remi repo is used.


7. Configured PHP-FPM (User, Group & Socket)

Opened file:

sudo vi /etc/php-fpm.d/www.conf

Updated configuration:

user = nginx
group = nginx

listen = /var/run/php-fpm/default.sock

Why?

  • nginx user ensures both services run under same context
  • Unix socket improves performance for local communication

8. Configured Nginx to Work with PHP-FPM

Opened file:

sudo vi /etc/nginx/nginx.conf

Added:

location ~ \.php$ {
    fastcgi_pass unix:/var/run/php-fpm/default.sock;
    fastcgi_index index.php;
    fastcgi_param SCRIPT_FILENAME \(document_root\)fastcgi_script_name;
    include fastcgi_params;
}

Why? Nginx cannot process PHP directly, so it forwards requests to PHP-FPM.


9. Restarted Services After Integration

sudo systemctl restart php-fpm
sudo systemctl restart nginx

Why? To apply updated configurations.


10. Verified Nginx Configuration Syntax

sudo nginx -t

Output:

nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

Why? To ensure there are no syntax errors before serving traffic.


11. Fixed Socket Permissions

Before:

ls -l /var/run/php-fpm/default.sock
srw-rw----+ 1 root root 0 Apr 14 13:51 /var/run/php-fpm/default.sock

Changed:

sudo chown -R nginx:nginx /var/run/php-fpm/default.sock

After:

ls -l /var/run/php-fpm/default.sock
srw-rw----+ 1 nginx nginx 0 Apr 14 13:51 /var/run/php-fpm/default.sock

Why? Nginx must have permission to access the PHP-FPM socket.


12. Tested the Application

From jump host:

curl http://stapp02:8092/index.php

Output:

Welcome to xFusionCorp Industries!

Why? To verify complete integration of Nginx and PHP-FPM.


My Understanding

This task demonstrated how multiple services must work together seamlessly. Nginx handles incoming requests, while PHP-FPM processes backend logic. Proper configuration, permissions, and validation steps are critical to ensure everything works smoothly.


What I Found Interesting

I found it interesting that even though the setup looks simple, small details like OS verification, socket permissions, and service user alignment play a huge role in making the system work correctly.


📌 Full notes: GitHub link