Navigation

Laravel

Which HTTP Server Should You Use for Laravel? A Developer's Guide to Nginx, Apache, and Octane

#laravel #php
Laravel HTTP Server Guide: Nginx offers proven reliability and is Laravel's recommended choice. Apache provides solid legacy support. Laravel Octane with FrankenPHP delivers 4x faster performance (311 vs 77 RPS). Choose Nginx for stability, Octane for speed.

Hey there, fellow Laravel developer! 👋

If you've ever stared at your terminal wondering "Should I use Nginx, Apache, or this shiny new Laravel Octane thing?" - you're not alone. I've been there, and trust me, the choice can make or break your app's performance.

Let me walk you through the options and help you pick the right one for your project.

Table Of Contents

TL;DR - Just Tell Me What to Use!

Building a regular Laravel app? Go with Nginx - it's fast, reliable, and Laravel's docs love it.

Need insane performance? Laravel Octane with FrankenPHP will blow your mind (and your server stats).

Stuck with existing infrastructure? Apache is still solid and won't let you down.


Let's Talk Traditional Web Servers First

Nginx: The Speed Demon Everyone Loves

Nginx is like that reliable friend who always shows up on time and never lets you down. It's what Laravel officially recommends, and there's a good reason for that.

Why developers swear by Nginx:

  • It handles thousands of concurrent connections without breaking a sweat
  • Uses way less memory than you'd expect
  • Serves your CSS, JS, and images lightning-fast
  • SSL setup? Piece of cake
  • It's running half the internet (seriously!)

Here's a basic Nginx config that actually works:

server {
    listen 80;
    listen [::]:80;
    server_name your-awesome-app.com;
    root /var/www/your-app/public;
    
    # Security headers (because we care about security, right?)
    add_header X-Frame-Options "SAMEORIGIN";
    add_header X-Content-Type-Options "nosniff";
    
    index index.php;
    charset utf-8;
    
    # The magic Laravel routing
    location / {
        try_files $uri $uri/ /index.php?$query_string;
    }
    
    # Don't log every favicon request (your logs will thank you)
    location = /favicon.ico { access_log off; log_not_found off; }
    location = /robots.txt  { access_log off; log_not_found off; }
    
    error_page 404 /index.php;
    
    # PHP-FPM magic happens here
    location ~ ^/index\.php(/|$) {
        fastcgi_pass unix:/var/run/php/php8.2-fpm.sock;
        fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
        include fastcgi_params;
        fastcgi_hide_header X-Powered-By;
    }
    
    # Hide those pesky dot files
    location ~ /\.(?!well-known).* {
        deny all;
    }
}

Apache: The Old Guard That Still Kicks

Apache is like your dad's old Toyota - it might not be the flashiest, but it'll get you where you need to go. Don't let anyone tell you Apache is "dead" - it's still powering millions of sites.

Why Apache might be your choice:

  • Your hosting provider probably supports it (shared hosting, anyone?)
  • .htaccess files just work (no server restarts needed)
  • Tons of modules for whatever weird thing you need to do
  • Documentation everywhere (seriously, Google anything + Apache)

A straightforward Apache setup:

<VirtualHost *:80>
    ServerName your-awesome-app.com
    ServerAdmin you@your-email.com
    DocumentRoot /var/www/your-app/public
    
    <Directory /var/www/your-app/public>
        Options Indexes FollowSymLinks MultiViews
        AllowOverride All
        Require all granted
    </Directory>
    
    ErrorLog ${APACHE_LOG_DIR}/laravel_error.log
    CustomLog ${APACHE_LOG_DIR}/laravel_access.log combined
</VirtualHost>

Enter Laravel Octane: The Game Changer

Okay, now we're getting to the fun stuff! 🚀

Traditional PHP is like starting your car every time you want to move forward. Laravel Octane? That's like keeping your engine running and just hitting the gas.

How This Magic Works

Think about it - normally, every single request to your Laravel app means:

  1. Start PHP ✋
  2. Load Laravel 📦
  3. Process request ⚡
  4. Send response 📤
  5. Kill everything 💀
  6. Repeat for the next request 🔄

That's... inefficient, right?

Octane says "Nah, let's boot Laravel ONCE and keep it in memory." Mind = blown 🤯

Your Octane Server Options

1. FrankenPHP: The New Kid That's Stealing the Show

FrankenPHP is built with Go (yeah, that Google language) and it's seriously impressive. Imagine having a server that just... works. HTTPS? Automatic. HTTP/3? Yep. Compression? Built-in.

Why FrankenPHP is becoming everyone's favorite:

  • Setup is ridiculously easy
  • Automatic HTTPS (Let's Encrypt just happens)
  • HTTP/3 support (because we live in the future)
  • You can literally package your Laravel app as a single binary file
  • It just works™️

Real talk: In benchmarks, FrankenPHP served 311 requests per second while traditional Nginx + PHP-FPM managed only 77. That's 4x faster! 🔥

2. Swoole: The Performance Beast

Swoole is a PHP extension that turns your humble PHP into an async powerhouse. It's like giving your Laravel app a Red Bull and watching it fly.

Swoole's superpowers:

  • Native PHP extension (no external dependencies)
  • Async everything (database calls, HTTP requests, you name it)
  • WebSocket support for real-time features
  • Coroutines (fancy word for "do multiple things at once")

In tests, Swoole hit 414 requests per second. Yeah, you read that right.

3. RoadRunner: The Go-Powered Middle Ground

RoadRunner is written in Go and offers a nice balance between performance and reliability. It's like the sensible choice that still makes you look cool.

RoadRunner's highlights:

  • Rock-solid stability
  • Great performance (around 350 RPS in tests)
  • Built-in process management
  • Extensible plugin system

Getting Started with Octane (It's Easier Than You Think!)

Ready to supercharge your Laravel app? Let's do this:

# Install the magic
composer require laravel/octane

# Choose your adventure
php artisan octane:install

# You'll see something like:
# Which application server would you like to use?
# [0] roadrunner
# [1] swoole  
# [2] frankenphp

My recommendation? Start with FrankenPHP (option 2). It's the easiest to get running.

Fire It Up!

# With FrankenPHP (my personal favorite)
php artisan octane:frankenphp

# Or if you chose Swoole
php artisan octane:start --server=swoole

# For production, use multiple workers
php artisan octane:start --workers=4 --server=frankenphp

Production Setup (Don't Skip This!)

In production, you'll want to put a traditional web server in front of Octane. Why? Because Nginx is still better at serving your CSS files and handling SSL certificates.

Nginx + Octane = Perfect Marriage:

server {
    listen 80;
    server_name your-app.com;
    
    # Proxy everything to Octane
    location / {
        proxy_pass http://127.0.0.1:8000;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection 'upgrade';
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_cache_bypass $http_upgrade;
    }
    
    # But serve static files directly (faster!)
    location ~* \.(js|css|png|jpg|jpeg|gif|ico|svg)$ {
        root /var/www/your-app/public;
        expires 1y;
        add_header Cache-Control "public, immutable";
    }
}

The Numbers Don't Lie: Performance Showdown

Let me share some real-world benchmark results that'll make you rethink everything:

What You're Running Requests Per Second How Much Faster?
Nginx + PHP-FPM 77 RPS Baseline
Apache + mod_php ~65 RPS Actually slower 😅
Octane + FrankenPHP 311 RPS 4x faster! 🚀
Octane + Swoole 414 RPS 5.4x faster! 🔥
Octane + RoadRunner ~350 RPS 4.5x faster!

These aren't marketing numbers - these are real benchmarks from developers testing production-like scenarios.


So... Which One Should YOU Choose?

Let me make this super simple for you:

Go with Nginx if:

  • You're building a "normal" Laravel app (most of us)
  • You want something that just works without surprises
  • Your team isn't ready for the Octane learning curve
  • You're using shared hosting (no choice here)
  • You value stability over bleeding-edge performance

Pick Apache when:

  • You're already running Apache (why fix what ain't broke?)
  • You need those .htaccess superpowers
  • Your hosting provider only supports Apache
  • You're working with legacy systems

Jump to Laravel Octane if:

  • Performance is make-or-break for your app
  • You're building APIs that need to fly
  • You have lots of concurrent users
  • You want to impress your team (and yourself)
  • You can handle a bit more complexity

Which Octane Server?

Start with FrankenPHP - seriously, it's the easiest path to ridiculous performance.

Go Swoole if you need WebSockets or want maximum speed.

Choose RoadRunner if you want something stable and Go-powered.


The Gotchas (Because There Are Always Gotchas)

Octane Isn't Magic - It Has Rules

Since your Laravel app stays in memory with Octane, you need to be careful about:

  • Memory leaks: Objects stick around between requests
  • Global variables: They'll bite you (avoid them!)
  • File uploads: Large files can cause issues
  • Database connections: Use connection pooling

But don't worry - Laravel's docs cover all this stuff.

Development vs Production

For development:

  • Stick with php artisan serve or Laravel Sail
  • Use Octane with --watch for auto-reloading when you change code
  • FrankenPHP if you need HTTPS locally

For production:

  • Always put Nginx or Apache in front of Octane
  • Use a process manager like Supervisor
  • Monitor everything (seriously, everything)

My Personal Take

After years of running Laravel apps, here's what I actually do:

  1. Start every project with Nginx - it's fast, reliable, and I sleep well at night
  2. Switch to Octane + FrankenPHP when I need more performance (which is more often these days)
  3. Only use Apache when I have no choice (shared hosting, legacy systems)

The performance gains from Octane are real, and FrankenPHP makes it almost as easy as traditional deployment. Plus, your users will notice the speed difference.


Wrapping Up

Look, there's no "wrong" choice here. Nginx will serve you well, Apache won't let you down, and Octane will make you feel like a performance wizard.

But if you're starting a new project and want to future-proof it? Give FrankenPHP a shot. The setup is simple, the performance gains are real, and you'll learn something new.

Your future self (and your server bills) will thank you.

What's next? Pick one, try it out, and remember - you can always change your mind later. That's the beauty of Laravel - it's flexible enough to work with whatever you throw at it.


Want to dive deeper?

Happy coding! 🎉

P.S. - If you end up trying Octane, drop a comment and let me know how it goes. I love hearing about real-world performance improvements!

Share this article

Add Comment

No comments yet. Be the first to comment!

More from Laravel