Brieflyn
Navigation Menu
Home › Tutorials & How-To › How to Configure Laravel with Nginx: The 2026 Guide

How to Configure Laravel with Nginx: The 2026 Guide

How to Configure Laravel with Nginx: The 2026 Guide
By Brieflyn Editorial Team • Published: August 17, 2026 • 9 min read (1,742 words) • 44 views
Learn how to configure Laravel with Nginx in 2026 for optimal performance, security, and easy SSL setup. Step‑by‑step guide for production servers today.

How to Configure Laravel with Nginx is the exact phrase that lands on the search bar when a developer wants a lean, fast, and secure production stack. In 2026, Laravel still relies on PHP‑FPM for dynamic content, while Nginx delivers static assets with minimal overhead. This guide walks you through the entire process: from server preparation to fine‑tuning, security hardening, and performance optimization. By the end you’ll have a battle‑tested configuration that keeps your application running smoothly under real world traffic.

Whether you’re running a hobby project on a single VPS or scaling a multi‑tenant SaaS, the steps below are applicable. We’ll use Ubuntu 24.04 LTS as the base OS, the latest PHP 8.3, and a modern NVMe SSD to illustrate best practices. All commands are ready to paste into your terminal. If you’re on a different distro, the concepts remain the same; just adjust package names.

What is Laravel with Nginx? Quick Answer

Laravel is a PHP framework that follows MVC and offers an expressive syntax for routing, database access, and templating. Nginx, an event‑driven web server, excels at serving static files and acting as a reverse proxy to PHP‑FPM. The combination yields high throughput with low memory usage.

Laravel's Architecture

  • Routes defined in routes/web.php map to controller actions.
  • Controllers reside in app/Http/Controllers and return views or JSON.
  • Blade templates live in resources/views and are compiled to PHP.
  • Configuration lives in .env and config/* files.

Why Nginx?

Unlike Apache’s mod_php that runs PHP in each worker, Nginx delegates PHP execution to a separate php-fpm pool. This separation reduces context switches, improves cache hit rates, and keeps the web server responsive even under heavy load.

Common Deployment Scenarios

  1. Single‑tenant Laravel app on a VPS.
  2. Multi‑domain app served by a shared Nginx instance.
  3. Dockerized stack with separate containers for web, PHP‑FPM, and database.

Why It Matters in 2026: Current Trends & Performance Gains

Focused view of a computer screen displaying code and debug information.
Photo by Daniil Komov via Pexels. Laravel With Nginx Technology.

Web traffic continues to grow, and VPS hosting plans now offer NVMe storage for the first time. In 2026, the average Laravel application sees a 30% reduction in page load times when Nginx is properly tuned, compared to a 2015 Apache baseline.

2026 Server

High‑density CPUs, 32 GB RAM, and 8 vCPU cores are now common on entry‑level VPSs. NVMe storage delivers 5–6 × faster I/O than SATA SSDs, beneficial for database and file‑based caching.

Nginx vs Apache in 2026

Apache remains popular for legacy reasons, but Nginx’s event model and lean footprint dominate new Laravel deployments. Benchmarks from real‑world tests show Nginx handling 50k concurrent connections with less than 200 ms latency.

Security Evolution

Modern web servers enforce TLS 1.2/1.3 only, disable SSLv3, and employ HSTS with OCSP stapling. Nginx’s ssl_protocols directive and Let’s Encrypt integration make these defaults trivial to implement.

Prerequisites & Requirements

Before you touch any configuration files, double‑check the following:

  • Ubuntu 24.04 LTS VPS with root SSH access.
  • PHP 8.3 and PHP‑FPM installed.
  • Database (MySQL/MariaDB or PostgreSQL) and proper extensions (pdo_mysql, pdo_pgsql, redis).
  • Domain name pointing to your VPS IP and a sandboxed SSL cert (Let’s Encrypt or a catch‑all self‑signed cert).
  • Composer and Git installed for pulling your Laravel repo.

Preparing Your VPS

A close-up of a person holding an NGINX sticker with a blurred background.
Photo by RealToughCandy.com via Pexels. Laravel With Nginx Concept.

Below are the exact commands you’ll run on a fresh machine. Execute them in order to set up a clean environment.

# Update package lists
sudo apt update

# Install PHP 8.3 and common extensions
sudo apt install -y php8.3 php8.3-fpm php8.3-mysql php8.3-pgsql php8.3-redis php8.3-curl php8.3-xml php8.3-mbstring php8.3-zip

# Verify PHP works
php -r 'echo "PHP is working\n";'

Next, install Nginx and configure PHP‑FPM for Laravel:

# Install Nginx
sudo apt install -y nginx

# Set up a dedicated user for the app
sudo adduser --system --group --disabled-login laravel

# Create a PHP‑FPM pool for Laravel
sudo nano /etc/php/8.3/fpm/pool.d/laravel.conf

Paste the following into laravel.conf:

[laravel]
user = laravel
group = laravel
listen = /run/php/php8.3-fpm-laravel.sock
listen.owner = www-data
listen.group = www-data
pm = dynamic
pm.max_children = 20
pm.start_servers = 5
pm.min_spare_servers = 3
pm.max_spare_servers = 7
chdir = /var/www/laravel

Restart PHP‑FPM:

sudo systemctl restart php8.3-fpm

Configuring PHP Settings

Open /etc/php/8.3/fpm/php.ini and fine‑tune the following values for production:

memory_limit = 256M
upload_max_filesize = 20M
post_max_size = 25M
max_execution_time = 120

Optimizing NVMe Storage

Ensure the filesystem uses ext4 or btrfs with noatime to reduce disk writes. Run:

sudo tune2fs -o noatime /dev/nvme0n1p1

Core Nginx Config Steps for Laravel

Now that the server is ready, create an Nginx server block that points to your Laravel app’s public directory and forwards PHP requests to the pool you just defined.

# Create a new server block
sudo nano /etc/nginx/sites-available/laravel

# Add the following configuration
server {
 listen 80;
 listen [::]:80;
 server_name example.com www.example.com;

 root /var/www/laravel/public;
 index index.php index.html index.htm;

 # Hide Nginx version
 server_tokens off;

 # Security headers
 add_header X-Frame-Options SAMEORIGIN;
 add_header X-Content-Type-Options nosniff;
 add_header Referrer-Policy no-referrer-when-downgrade;
 add_header Strict-Transport-Security "max-age=31536000; includeSubDomains; preload";

 # Prevent access to sensitive files
 location ~ /\.(?!well-known).* {
 deny all;
 }

 # Pass PHP files to PHP‑FPM
 location ~ \.php$ {
 include snippets/fastcgi-php.conf;
 fastcgi_pass unix:/run/php/php8.3-fpm-laravel.sock;
 fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
 fastcgi_param PHP_VALUE "user_ini.cache_ttl=0";
 }

 # Handle static assets and Laravel routing
 location / {
 try_files $uri $uri/ /index.php?$query_string;
 }

 # Enable gzip compression
 gzip on;
 gzip_types text/plain text/css application/javascript application/json application/xml;
 gzip_vary on;
 gzip_min_length 256;
}

Enable the site and reload Nginx:

sudo ln -s /etc/nginx/sites-available/laravel /etc/nginx/sites-enabled/
sudo nginx -t
sudo systemctl reload nginx

FastCGI Parameters

FastCGI inherits many environment variables from the web server. The snippets/fastcgi-php.conf file provided by Ubuntu includes the essential fastcgi_param directives. Make sure SCRIPT_FILENAME points to the correct document root; otherwise, PHP will look in the wrong place and return 404.

Static File Handling

By serving assets directly from /public, Nginx bypasses PHP‑FPM entirely for CSS, JS, and image requests. This reduces CPU usage and improves caching opportunities. Set proper Cache-Control headers if you plan to use a CDN.

Secure Your App with Certbot

HTTPS is non‑negotiable for modern applications. Let’s Encrypt offers free, automated certificates that integrate cleanly with Nginx.

Self‑Signed vs Let’s Encrypt

For local or staging environments, a self‑signed cert is quick. On production, obtain a Let’s Encrypt cert:

sudo apt install -y certbot python3-certbot-nginx
sudo certbot --nginx -d example.com -d www.example.com

Follow the prompts to configure automatic redirect from HTTP to HTTPS and enable HSTS.

Automated Renewal Script

Certbot installs a cron job by default. Verify it runs:

sudo systemctl list-timers | grep certbot

To troubleshoot, check logs at /var/log/letsencrypt/letsencrypt.log.

Handling HSTS & OCSP

In the server block, add:

add_header Strict-Transport-Security "max-age=63072000; includeSubDomains; preload" always;
add_header Public-Key-Pins "pin-sha256="..."; max-age=5184000; includeSubDomains;" always;

OCSP stapling is enabled by default on Ubuntu 24.04, but you can force it with:

ssl_stapling on;
ssl_stapling_verify on;

Maximizing Throughput: Performance Tuning & Caching

Even with a solid foundation, small tweaks can yield noticeable speed gains.

OPcache & PHP‑FPM Pools

Enable OPcache in /etc/php/8.3/fpm/php.ini:

opcache.enable=1
opcache.memory_consumption=256
opcache.revalidate_freq=60

Adjust the pm.max_children value in the pool config based on available RAM. A rule of thumb: max_children = floor((RAM - 1GB) / 128MB).

Nginx Gzip & Brotli

Gzip is already enabled in the server block. To add Brotli, install the module:

sudo apt install -y nginx-extras
sudo nano /etc/nginx/conf.d/brotli.conf

Insert:

brotli on;
brotli_static on;
brotli_comp_level 6;
brotli_types text/plain text/css application/javascript application/json application/xml application/json;

Cache Headers for Assets

Set long cache lifetimes for immutable assets (e.g., app.[hash].js):

location ~ \.(js|css|png|jpg|jpeg|gif|ico|svg)$ {
 expires max;
 add_header Cache-Control "public, must-revalidate, proxy-revalidate";
}

Common Mistakes & Troubleshooting

Even seasoned admins slip. Here are the most frequent issues and how to fix them.

Incorrect File Permissions

Laravel needs write access to storage/ and bootstrap/cache/. The recommended permissions are:

sudo chown -R laravel:www-data /var/www/laravel/storage
sudo chown -R laravel:www-data /var/www/laravel/bootstrap/cache
sudo chmod -R 775 /var/www/laravel/storage /var/www/laravel/bootstrap/cache

FastCGI Timeout Issues

If PHP requests hang, increase fastcgi_read_timeout in the server block:

fastcgi_read_timeout 300;

Mixed Content Errors

When HTTP assets load on an HTTPS page, browsers block them. Ensure every asset URL is protocol‑relative or explicitly uses https://. In Laravel, set 'url' => env('APP_URL', 'https://example.com').

Who Is This Best For? (Structured Table)

Target PersonaRecommended OptionKey Reason & Real‑World Benefit
Small ProjectsSingle‑VPS Nginx + PHP‑FPMLow cost, minimal maintenance, fast setup
Mid‑Scale AppsDockerized Nginx + PHP‑FPM + TraefikDynamic multi‑domain routing, easy scaling
Enterprise DeploymentsNGINX Plus + PHP‑FPM + Load BalancerHigh availability, advanced traffic shaping, enterprise support

People Also Ask: Frequently Asked Questions

Can I Use Docker with Nginx?

Yes. Docker Compose lets you spin up separate containers for Nginx, PHP‑FPM, and your database. A typical docker-compose.yml uses the laravelphp/php-fpm image and mounts the public directory.

How to Handle Multiple Domains?

Create separate server blocks for each domain, or use Traefik with a single reverse proxy that auto‑discovers services via Docker labels.

What About Laravel Forge?

Forge automates server provisioning and Nginx configuration. It’s ideal if you prefer a managed service over manual setup.

Wrap‑Up & Next Steps

How to Configure Laravel with Nginx is a repeatable process that scales from hobby sites to production workloads. By following the steps above, you’ll have a resilient, secure, and fast Laravel deployment.

Next actions: run php artisan migrate --env=production, set up Laravel Horizon for queue monitoring, and consider a CDN like Cloudflare to push static assets worldwide.

Keep your system updated: sudo apt upgrade for OS patches, composer update for dependencies, and certbot renew --dry-run to verify SSL renewal. Monitor logs with journalctl -u nginx -f and journalctl -u php8.3-fpm -f to catch issues before they impact users.

How to Configure Laravel with Nginx example setup

Frequently Asked Questions

Create a server block with root pointing to public/, enable try_files to route to index.php, set fastcgi_pass to PHP‑FPM socket, and secure .env files.

No comments yet. Be the first to share your technical feedback!

Leave Technical Feedback / Discussion

B

Brieflyn Editorial Team

Senior cybersecurity researchers, DevOps engineers, and technical editors at Brieflyn.

EXPERTISE: CYBERSECURITY, CLOUD INFRASTRUCTURE, & SOFTWARE SYSTEMS

Related Guides & Documentation