Home / Configs / Servers / nginx PHP-FPM config

nginx PHP-FPM config: example and generator

Run a PHP site (WordPress, Laravel or plain PHP) on nginx with PHP-FPM.

For Debian and Ubuntu servers, where the snippets/fastcgi-php.conf file comes with nginx. Other systems use different paths.

nginx does not run PHP itself. It passes .php files to PHP-FPM, a separate service that runs the code and returns the page. Fill in your domain, site folder and PHP version below to get a ready config for Ubuntu or Debian, then follow the steps to turn it on.

Create your config file

Type your own values. The file below updates as you type, and nothing you type leaves your browser.

Use localhost for local testing
The folder that holds index.php
Run php -v to see it. Use only major.minor, like 8.3
For example 2m, 50m, 1g

/etc/nginx/conf.d/mysite.conf
server {
  listen 80;
  server_name example.com;
  root /var/www/mysite;
  index index.php index.html;
  client_max_body_size 10m;

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

  location ~ \.php$ {
    include snippets/fastcgi-php.conf;
    fastcgi_pass unix:/run/php/php8.3-fpm.sock;
  }

  location ~ /\.ht {
    deny all;
  }
}

How this setup works

Diagram: Browser, then nginx, then PHP-FPM
Browser (your domain) → nginx (port 80) → PHP-FPM (runs your PHP)

Step-by-step setup

  1. Install the software on Ubuntu or Debian: sudo apt install nginx php-fpm.
  2. Put your site in /var/www/mysite.
  3. Save the config above as /etc/nginx/conf.d/mysite.conf.
  4. Check the socket exists: ls /run/php/php8.3-fpm.sock. If it does not, change the PHP version above.
  5. Test it: sudo nginx -t, then apply it: sudo systemctl reload nginx.

What the important lines mean

Common errors and how to fix them

502 Bad Gateway
nginx cannot reach PHP-FPM. Check it is running with sudo systemctl status php8.3-fpm and that the socket path matches your PHP version.
File not found.
nginx reached PHP-FPM but the file does not exist. Check that root is /var/www/mysite and that the file is inside it.
The browser downloads the .php file
The PHP location block is not being used. Check the config was saved in /etc/nginx/conf.d and that you reloaded nginx.
403 Forbidden
nginx cannot read the folder or there is no index file. Check that the folder and its parents are readable by the nginx user (www-data on Ubuntu).

Frequently asked questions

What is the difference between a socket and port 9000 for PHP-FPM?
Both reach PHP-FPM. A Unix socket such as /run/php/php8.3-fpm.sock works only on the same machine and is what Ubuntu and Debian packages use by default. A TCP address such as 127.0.0.1:9000 is used when PHP-FPM listens on a port, for example in some Docker setups.
How do I find my PHP version?
Run php -v in a terminal. Use the first two numbers, such as 8.3, in the PHP version field.
Does this work for WordPress and Laravel?
The try_files line is the part those need for pretty links. For Laravel, set the site folder to the project's public folder.

Written for: nginx with PHP-FPM on Ubuntu 24.04 (run sudo nginx -t before you reload) · Updated 2026-10-11

Free tools that help

Fix a problem with this setup

More server configs

See all 41 config generators