Skip to content

NGINX with PHP-FPM for PHP Websites

NGINX · Self-help guide

NGINX does not process PHP by itself. Instead, it passes PHP requests to PHP-FPM, which runs the PHP code and sends the result back to NGINX. This setup is fast, reliable, and commonly used for Laravel, WordPress, custom PHP apps, WHMCS-style portals, and hosting control panels.

Step 1: Install NGINX and PHP-FPM:

sudo apt update sudo apt install nginx php-fpm -y

Step 2: Check the PHP-FPM service:

sudo systemctl status php8.3-fpm

Your version may differ, such as php8.2-fpm or php8.1-fpm.

Step 3: Create your website folder:

sudo mkdir -p /var/www/example.com/public

Step 4: Add your PHP files, with index.php inside the public folder.

Step 5: Configure NGINX with a PHP location block that passes .php files to the correct PHP-FPM socket or port.

Step 6: Test the NGINX configuration:

sudo nginx -t

Step 7: Reload services:

sudo systemctl reload nginx sudo systemctl reload php8.3-fpm

Useful troubleshooting commands:

php -v sudo systemctl status php8.3-fpm sudo tail -f /var/log/nginx/error.log sudo journalctl -u php8.3-fpm —no-pager -n 100

If PHP files download instead of running, NGINX is not correctly passing PHP to PHP-FPM. If you see 502 Bad Gateway, PHP-FPM may be stopped, using a different socket, or blocked by permissions.

For Laravel, point the NGINX root to the public directory only. This is very important because files like .env should never be publicly accessible.

Did this solve it?If not, Penny and the support team are ready to help.Start live chatOpen a ticket