Integrate PHP-FPM with Nginx on RHEL 9
Prerequisites
PHP-FPM installed and running. See PHP-FPM install guide.
Socket path: /run/php-fpm/www.sock
Step 1 – Nginx server block
server {
listen 80;
server_name example.com;
root /var/www/example.com/html;
index index.php index.html;
location / {
try_files $uri $uri/ /index.php?$args;
}
location ~ \.php$ {
include fastcgi_params;
fastcgi_pass unix:/run/php-fpm/www.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
location ~ /\.ht { deny all; }
}
Step 2 – Test
nginx -t && systemctl reload nginx 2>/dev/null || nginx -t && rc-service nginx reload 2>/dev/null || nginx -t && rcctl reload nginx
echo '<?php phpinfo(); ?>' > /var/www/example.com/html/info.php
curl http://localhost/info.php | grep 'PHP Version'
rm /var/www/example.com/html/info.php