Nginx for Laravel Developers
Introduction
Most developers start Laravel using:
php artisan serve
That’s fine for development — but not for production.
In real-world applications, your Laravel app runs behind a web server like:
- Nginx
- Apache HTTP Server
This guide will teach you how Nginx works and how to properly run Laravel with it step by step.
What Is a Web Server?
A web server is responsible for:
- Receiving HTTP requests
- Serving static files (CSS, JS, images)
- Passing PHP requests to the backend (PHP-FPM)
Request Flow
Browser → Nginx → PHP-FPM → Laravel → Response → Browser
👉 Nginx handles traffic
👉 PHP-FPM executes PHP
👉 Laravel processes your logic
Why Use Nginx Instead of php artisan serve?
| Feature | php artisan serve | Nginx |
|---|---|---|
| Performance | ❌ Low | ✅ High |
| Production Ready | ❌ No | ✅ Yes |
| Scalability | ❌ No | ✅ Yes |
| Security | ❌ Basic | ✅ Advanced |
👉 Conclusion: Always use Nginx in production
Prerequisites
Before starting, make sure you have:
- PHP installed
- Laravel project ready
- Terminal access
- Basic command line knowledge
Step 1: Install Nginx
Mac (Homebrew)
brew install nginx
Ubuntu / Linux
sudo apt update
sudo apt install nginx
Start Nginx
sudo service nginx start
👉 Open browser:
http://localhost
You should see the Nginx welcome page.
Step 2: Install PHP & PHP-FPM
Laravel needs PHP-FPM to process PHP files.
Mac
brew install php
Ubuntu
sudo apt install php-fpm
Start PHP-FPM
php-fpm
Step 3: Configure Nginx for Laravel
Navigate to config directory:
# Mac
cd /usr/local/etc/nginx/
# Linux
cd /etc/nginx/
Create a new config file:
sudo nano sites-available/laravel
Step 4: Add Laravel Nginx Configuration
server {
listen 80;
server_name localhost;
root /path-to-your-laravel-project/public;
index index.php index.html;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location ~ \.php$ {
include fastcgi_params;
fastcgi_pass 127.0.0.1:9000;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
location ~ /\.ht {
deny all;
}
}
⚠️ Important
-
rootMUST point to/public -
Replace
/path-to-your-laravel-projectwith your actual path
Step 5: Enable the Configuration
sudo ln -s /etc/nginx/sites-available/laravel /etc/nginx/sites-enabled/
Step 6: Restart Nginx
sudo nginx -t
sudo service nginx restart
Step 7: Run Your Laravel App
Open:
http://localhost
Your Laravel app is now running with Nginx
Step 8: What About Laravel Cloud?
If you deploy using:
- Laravel Cloud
- Laravel Forge
- Laravel Vapor
👉 You do NOT need to configure Nginx manually
These platforms automatically:
- Install and configure Nginx
- Set up PHP-FPM
- Handle SSL (HTTPS)
- Optimize performance
Common Mistakes
Avoid these:
-
❌ Using
php artisan servein production -
❌ Wrong root path (not pointing to
/public) - ❌ PHP-FPM not running
- ❌ Forgetting to restart Nginx after changes
Nginx vs Apache
| Feature | Nginx | Apache |
|---|---|---|
| Speed | ⚡ Fast | Medium |
| Config | Centralized | .htaccess |
| Best For | High traffic | Shared hosting |
👉 Apache HTTP Server is easier
👉 Nginx is faster and modern
Conclusion
Understanding Nginx is essential if you want to:
- Deploy Laravel apps professionally
- Improve performance
- Handle real-world traffic
- Debug production issues
