In high-performance web architectures, Nginx and PHP-FPM can communicate using either TCP sockets (e.g., 127.0.0.1:9000) or Unix domain sockets (e.g., unix:/var/run/php-fpm/default.sock). Unix domain sockets are local to the host and bypass the network stack, avoiding the overhead of loopback routing, TCP headers, and port allocation limits. This yields lower latency and higher throughput, making it the preferred setup for applications deployed on single servers.
This guide outlines the steps to install Nginx and PHP-FPM 8.3 on App Server 2 (stapp02), configure them to communicate over a Unix socket, and verify the setup from the Jump Host.
stapp02)jump_host)8092/var/www/html8.3 (Remi Repository)/var/run/php-fpm/default.sockindex.php and info.php (already present under /var/www/html)Access App Server 2 (stapp02) from the Jump Host via SSH:
ssh steve@stapp02
Update packages and install the Nginx web server:
sudo yum install -y nginx
Open the main Nginx configuration file:
sudo vi /etc/nginx/nginx.conf
Locate the default server block in /etc/nginx/nginx.conf and update it to listen on port 8092, set the document root, and configure PHP-FPM request routing via FastCGI.
[!WARNING] Ensure you only modify the main HTTP server block. Do not modify any TLS or SSL server blocks if present.
Update the server block to look like the following:
server {
listen 8092;
listen [::]:8092;
server_name stapp02;
root /var/www/html;
index index.php index.html;
location / {
try_files $uri $uri/ =404;
}
# Pass PHP scripts to FastCGI server listening on Unix socket
location ~ \.php$ {
include fastcgi_params;
fastcgi_pass unix:/var/run/php-fpm/default.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
error_page 404 /404.html;
error_page 500 502 503 504 /50x.html;
}
Start and enable the Nginx service:
sudo systemctl enable nginx
sudo systemctl start nginx
Verify that Nginx is active and listening on port 8092:
sudo systemctl status nginx
sudo ss -tulpn | grep 8092
By default, the standard CentOS/RHEL app stream may contain older PHP versions. To install PHP-FPM 8.3, enable the EPEL and Remi repositories:
# Install EPEL repository
sudo dnf install epel-release -y
# Install Remi RPM repository
sudo dnf install https://rpms.remirepo.net/enterprise/remi-release-9.rpm -y
# Enable the PHP 8.3 module from Remi
sudo dnf module list php
sudo dnf module enable php:remi-8.3 -y
# Install PHP, PHP-FPM, and necessary extensions
sudo dnf install php-fpm php php-cli php-common php-mysqlnd php-gd php-xml php-mbstring php-pdo php-opcache -y
By default, PHP-FPM is configured to run as the apache user and listen on a TCP port. Update these settings to match Nginx’s privileges and use the Unix socket:
Open the default www pool configuration file:
sudo vi /etc/php-fpm.d/www.conf
Locate and modify the following parameters:
nginx
user = nginx
group = nginx
listen = /var/run/php-fpm/default.sock
listen.owner = nginx
listen.group = nginx
listen.mode = 0660
Unix sockets are generated dynamically by the PHP-FPM process on startup. If the parent directory /var/run/php-fpm does not exist or has incorrect permissions, PHP-FPM will fail to start or cannot initialize the socket.
Create the parent directory and configure Nginx as the owner:
# Create target directory
sudo mkdir -p /var/run/php-fpm
# Set owner/group to nginx
sudo chown -R nginx:nginx /var/run/php-fpm
Start and enable the PHP-FPM service, then restart Nginx to bind to the newly created Unix socket:
# Start and enable PHP-FPM
sudo systemctl enable --now php-fpm
sudo systemctl restart php-fpm
# Restart Nginx
sudo systemctl restart nginx
Verify that the socket file has been successfully created with the correct permissions:
ls -la /var/run/php-fpm/default.sock
Expected output snippet:
srw-rw---- 1 nginx nginx 0 Jun 20 17:35 /var/run/php-fpm/default.sock
Log out of App Server 2 to return to the Jump Host:
exit
From the Jump Host, query the PHP application endpoints using curl:
# Test the index.php page
curl http://stapp02:8092/index.php
# Test the info.php page (if validation required)
curl http://stapp02:8092/info.php
The first curl request should output the content of the index.php page, typically containing:
Welcome to xFusionCorp Industries!
If the responses return successfully, the Nginx and PHP-FPM Unix Socket configuration is complete and operational.