Nginx and PHP-FPM Configuration
Set up Nginx as a reverse proxy to PHP-FPM for serving PHP applications.
Why Nginx + PHP-FPM?
Nginx is a high-performance web server and reverse proxy. PHP-FPM (FastCGI Process Manager) manages a pool of PHP worker processes. Nginx passes PHP requests to FPM via FastCGI.
Basic Nginx Server Block
A minimal Nginx configuration for a PHP application.
# /etc/nginx/sites-available/myapp
server {
listen 80;
server_name example.com;
root /srv/myapp/public;
index index.php;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location ~ \.php$ {
fastcgi_pass unix:/run/php/php8.3-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
}
location ~ /\.ht {
deny all;
}
}All lessons in this course
- Nginx and PHP-FPM Configuration
- Environment Variables and .env Files
- Deploying with GitHub Actions
- Dockerizing a PHP Application