How to configure Nginx with PHP-FPM for Laravel?
Aug 04, 2025 am 07:59 AMTo properly configure Nginx and PHP-FPM to run Laravel applications, make sure that the request is correctly routed to public/index.php. 1. Install PHP-FPM and confirm its socket path, such as /var/run/php/php8.1-fpm.sock; 2. Configure the Nginx server block, root points to the public directory, use try_files $uri $uri/ /index.php?$query_string to handle the route, location ~ \.php$ fastcgi_pass points to the correct PHP-FPM socket, and set a secure fastcgi_param SCRIPT_FILENAME; 3. Set file permissions: Set the project directory to the www-data user, storage and bootstrap/cache directory permissions to 775; 4. Optionally configure HTTPS, and use Certbot to automatically configure SSL; 5. Complete the Laravel environment settings, configure APP_ENV=production, APP_DEBUG=false, APP_URL in the .env file, and run php artisan key:generate and config:cache. After ensuring that PHP-FPM is running, Nginx is configured correctly, and permissions are correct, Laravel applications can run efficiently through Nginx and PHP-FPM.
Setting up Nginx with PHP-FPM for a Laravel application is straightforward once you understand the components involved. Laravel relies on clean URLs (via index.php
routing), so your Nginx configuration must properly route requests to the public/index.php
front controller. Here's how to do it correctly.

? 1. Install and Configure PHP-FPM
First, make sure PHP-FPM is installed and running. Laravel typically requires PHP 8.0 or higher.
# On Ubuntu/Debian sudo apt update sudo apt install php-fpm php-mysql php-curl php-mbstring php-xml php-bcmath
After installation, check the active PHP-FPM socket. Most systems use:

/var/run/php/php8.1-fpm.sock # Adjust version as needed
You can verify this by checking the pool config:
sudo cat /etc/php/8.1/fpm/pool.d/www.conf | grep "listen ="
It should show something like:

listen = /var/run/php/php8.1-fpm.sock
Keep this path handy — you'll need it in the Nginx config.
? 2. Nginx Server Block for Laravel
Create or edit your site configuration in Nginx (usually under /etc/nginx/sites-available/your-site
):
server { listen 80; server_name yourdomain.com; root /var/www/laravel/public; # Point to Laravel's public directory index index.php index.html; # Handle all incoming requests location / { try_files $uri $uri/ /index.php?$query_string; } # PHP processing location ~ \.php$ { include snippets/fastcgi-php.conf; fastcgi_pass unix:/var/run/php/php8.1-fpm.sock; # Match your PHP-FPM socket fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name; include fastcgi_params; } # Deny access to sensitive files location ~ /\.(env|git) { deny all; } # Optional: Deny access to hidden files outside public location ~ /\. { access_log off; log_not_found off; deny all; } }
Key Points:
-
root
points topublic/
, not the Laravel root. -
try_files
ensures all non-file/directory requests go toindex.php
for Laravel's router. -
fastcgi_pass
must match your PHP-FPM socket. -
fastcgi_param SCRIPT_FILENAME
uses$realpath_root
for security (prevents path traversal).
Enable the site:
sudo ln -s /etc/nginx/sites-available/your-site /etc/nginx/sites-enabled/ sudo nginx -t # Test config sudo systemctl reload nginx
? 3. Set Proper File Permissions
Ensure Nginx can read your Laravel app:
sudo chown -R www-data:www-data /var/www/laravel sudo chmod -R 755 /var/www/laravel sudo chmod -R 775 /var/www/laravel/storage /var/www/laravel/bootstrap/cache
Replace
/var/www/laravel
with your actual Laravel project path.
? 4. (Optional) HTTPS with Let's Encrypt
Use Certbot to secure your site:
sudo apt install certbot python3-certbot-nginx sudo certbot --nginx -d yourdomain.com
This automatically updates your Nginx config to use SSL.
? 5. Laravel Environment Setup
Make sure your .env
file is set up:
APP_ENV=production APP_DEBUG=false # Never enable in production APP_URL=https://yourdomain.com
And generate the key:
php artisan key:generate php artisan config:cache
That's it. Your Laravel app should now be served efficiently via Nginx and PHP-FPM. Just make sure:
- PHP-FPM is running:
sudo systemctl status php8.1-fpm
- Nginx has no config errors
- File permissions are correct
Basically, it's not complicated — but the root
, try_files
, and fastcgi_param
lines are easy to mess up. Get those right, and Laravel will run smoothly.
The above is the detailed content of How to configure Nginx with PHP-FPM for Laravel?. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

NGINX is more suitable for handling high concurrent connections, while Apache is more suitable for scenarios where complex configurations and module extensions are required. 1.NGINX is known for its high performance and low resource consumption, and is suitable for high concurrency. 2.Apache is known for its stability and rich module extensions, which are suitable for complex configuration needs.

NGINX and Apache each have their own advantages and disadvantages, and the choice should be based on specific needs. 1.NGINX is suitable for high concurrency scenarios because of its asynchronous non-blocking architecture. 2. Apache is suitable for low-concurrency scenarios that require complex configurations, because of its modular design.

PHP code can be executed in many ways: 1. Use the command line to directly enter the "php file name" to execute the script; 2. Put the file into the document root directory and access it through the browser through the web server; 3. Run it in the IDE and use the built-in debugging tool; 4. Use the online PHP sandbox or code execution platform for testing.

Understanding Nginx's configuration file path and initial settings is very important because it is the first step in optimizing and managing a web server. 1) The configuration file path is usually /etc/nginx/nginx.conf. The syntax can be found and tested using the nginx-t command. 2) The initial settings include global settings (such as user, worker_processes) and HTTP settings (such as include, log_format). These settings allow customization and extension according to requirements. Incorrect configuration may lead to performance issues and security vulnerabilities.

Linux system restricts user resources through the ulimit command to prevent excessive use of resources. 1.ulimit is a built-in shell command that can limit the number of file descriptors (-n), memory size (-v), thread count (-u), etc., which are divided into soft limit (current effective value) and hard limit (maximum upper limit). 2. Use the ulimit command directly for temporary modification, such as ulimit-n2048, but it is only valid for the current session. 3. For permanent effect, you need to modify /etc/security/limits.conf and PAM configuration files, and add sessionrequiredpam_limits.so. 4. The systemd service needs to set Lim in the unit file

When configuring Nginx on Debian system, the following are some practical tips: The basic structure of the configuration file global settings: Define behavioral parameters that affect the entire Nginx service, such as the number of worker threads and the permissions of running users. Event handling part: Deciding how Nginx deals with network connections is a key configuration for improving performance. HTTP service part: contains a large number of settings related to HTTP service, and can embed multiple servers and location blocks. Core configuration options worker_connections: Define the maximum number of connections that each worker thread can handle, usually set to 1024. multi_accept: Activate the multi-connection reception mode and enhance the ability of concurrent processing. s

NGINXserveswebcontentandactsasareverseproxy,loadbalancer,andmore.1)ItefficientlyservesstaticcontentlikeHTMLandimages.2)Itfunctionsasareverseproxyandloadbalancer,distributingtrafficacrossservers.3)NGINXenhancesperformancethroughcaching.4)Itofferssecur

Diagnosis and solutions for common errors of Nginx include: 1. View log files, 2. Adjust configuration files, 3. Optimize performance. By analyzing logs, adjusting timeout settings and optimizing cache and load balancing, errors such as 404, 502, 504 can be effectively resolved to improve website stability and performance.
