We know how to run Laravel apps in PHP, but how do you run them if they’re written in Rust? In this post, we’ll go from the default PHP stack to a Rust stack, explained for Laravel developers.
Yesterday, Laravel announced Laravel Rust . It has the same framework ideas (routing, middleware, sessions, queues, …), but it’s written in Rust, on top of Axum and Tokio . If you’ve been running Laravel for years, your PHP stack is probably Nginx in front of PHP-FPM.
Short version: your web server stops talking FastCGI and becomes a plain reverse proxy. Your app becomes one long-running binary that you keep alive with systemd. The configs get shorter.
What changes on the server#
With PHP, Nginx hands each request to a PHP-FPM worker over a FastCGI socket. PHP-FPM keeps a pool of worker processes around. Each request boots Laravel, runs your code and throws everything away again.
With Laravel Rust, cargo build --release gives you a single binary, named after your project. Run it without arguments and it’s an HTTP server:
$ ./example-app
Server running at `http://127.0.0.1:8000`. Press Ctrl+C to stop.
That binary is the application server. There’s no FPM pool, no FastCGI, no php.ini. Your web server proxies HTTP to 127.0.0.1:8000, the same way you’d put Nginx in front of a Node or Go app.
The binary also serves the files in public/ itself, when no route matches the request. So the web server is optional. It’s there for TLS, for serving static files a bit faster, and because you probably already have one. The deployment docs
only talk about “a load balancer that handles HTTPS”. So the configs below are my own.
A few things still have to sit next to the binary on the server: public/, resources/views/, storage/ and the .env file. Your migrations are compiled into the binary.
Nginx: PHP-FPM vs. Rust#
The PHP-FPM version is Laravel’s own Nginx config , trimmed down to the lines that do something. Switch tabs to compare.
PHP-FPM
server {
listen 80;
listen [::]:80;
server_name example.com;
root /var/www/example.com/public;
index index.php;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location ~ ^/index\.php(/|$) {
fastcgi_pass unix:/run/php/php8.5-fpm.sock;
fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
include fastcgi_params;
fastcgi_hide_header X-Powered-By;
}
location ~ \.php$ {
return 404;
}
location ~ /\.(?!well-known).* {
deny all;
}
}
Rust
server {
listen 80;
listen [::]:80;
server_name example.com;
root /var/www/example.com/public;
location / {
try_files $uri @app;
}
location @app {
proxy_pass http://127.0.0.1:8000;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
}
The Rust version works the same way as the PHP one. If the file exists in public/ (your Vite build in public/build/, favicon.ico, …), Nginx serves it from disk. Everything else goes to the binary. You could drop the try_files and proxy everything, because the binary serves public/ too. But Nginx is better at static files.
You’d add TLS the same way you do today, with certbot or by hand.
Don’t forget proxy_set_header Host#
By default, Nginx’s proxy_pass sends the Host from the proxy_pass URL. So the app sees Host: 127.0.0.1:8000. In production, Laravel Rust only answers requests for the host in APP_URL (and its subdomains). Everything else gets a 400.
The trusted hosts docs explain how to allow extra hostnames.
Trust the proxy, in code#
With PHP-FPM you never had to think about this. Nginx passes the client’s IP to PHP as REMOTE_ADDR over FastCGI. With a reverse proxy, every connection the app sees comes from Nginx. I added a small route to the skeleton that prints what the app thinks the request looks like. Behind Nginx, without any extra config:
$ curl -s http://example.com/whoami
ip=127.0.0.1 host=example.com secure=false url=http://example.com/whoami
Every visitor is 127.0.0.1. That breaks rate limiting, logging and anything else that uses the visitor’s IP. And once you add TLS, secure stays false. Your generated URLs keep starting with http://.
The fix is a line in src/app.rs. That means it’s compiled into the binary:
.with_middleware(|middleware, _| {
middleware.trust_proxies(["127.0.0.1"]);
// ...
})
After a rebuild, the same request from another machine:
$ curl -s http://example.com/whoami
ip=172.20.0.3 host=example.com secure=false url=http://example.com/whoami
$ curl -s -H 'X-Forwarded-For: 6.6.6.6' http://example.com/whoami
ip=172.20.0.3 host=example.com secure=false url=http://example.com/whoami
That second request is a client trying to fake its IP. It doesn’t work. The framework reads X-Forwarded-For from right to left and picks the first address it doesn’t trust. Nginx’s $proxy_add_x_forwarded_for adds to the header a client sent. That’s safe, as long as you list your proxy’s address instead of "*". A single IP works. So does a range like 10.0.0.0/8. The trusting proxies
section has the details.
Caddy: PHP-FPM vs. Rust#
Caddy needs even less. The PHP version is the same idea as my older Caddyfile for Laravel , without the extras.
PHP-FPM
example.com {
root * /var/www/example.com/public
encode zstd gzip
php_fastcgi unix//run/php/php8.5-fpm.sock
file_server
}
Rust
example.com {
root * /var/www/example.com/public
encode zstd gzip
@app not file
reverse_proxy @app 127.0.0.1:8000
file_server
}
The @app not file matcher sends a request to the binary when there’s no file on disk for it. file_server handles the rest. That’s the Caddy version of try_files $uri @app.
If you don’t care about Caddy serving the static files, this works too:
example.com {
reverse_proxy 127.0.0.1:8000
}
Caddy avoids these Nginx problems on its own. reverse_proxy passes the original Host header and sets X-Forwarded-For and X-Forwarded-Proto. It also throws away any X-Forwarded-For a client sends, unless you’ve told Caddy to trust that client. Over HTTPS, with trust_proxies(["127.0.0.1"]) in the app:
$ curl -s -H 'X-Forwarded-For: 6.6.6.6' https://example.com/whoami
ip=172.20.0.3 host=example.com secure=true url=https://example.com/whoami
The app sees the real client IP, secure=true and https:// URLs. You still need the trust_proxies line, though. That part is the app’s job, whichever proxy you use.
Running the app: systemd instead of PHP-FPM#
This is where the Rust side is more work. On a PHP server, apt install php8.5-fpm gives you a running pool. You only add a systemd unit (or a Supervisor config) for queue:work and a cron entry for the scheduler.
With Rust, the same binary is your app server, your queue worker and your scheduler. Which one depends on the arguments you give it. You keep it running yourself:
PHP-FPM
# /etc/systemd/system/example-app-queue.service
# PHP-FPM itself comes with its own unit from the package.
[Unit]
Description=example.com queue worker (Laravel PHP)
After=network.target
[Service]
User=www-data
WorkingDirectory=/var/www/example.com
ExecStart=/usr/bin/php artisan queue:work
Restart=always
[Install]
WantedBy=multi-user.target
# crontab -u www-data -e
* * * * * cd /var/www/example.com && php artisan schedule:run >> /dev/null 2>&1
Rust
# /etc/systemd/system/example-app.service
[Unit]
Description=example.com (Laravel Rust)
After=network.target
[Service]
User=www-data
WorkingDirectory=/var/www/example.com
ExecStart=/var/www/example.com/example-app
Restart=always
# systemd's default is 1024, below SERVER_MAX_CONNECTIONS.
LimitNOFILE=65535
[Install]
WantedBy=multi-user.target
# /etc/systemd/system/example-app-queue.service
[Unit]
Description=example.com queue worker (Laravel Rust)
After=network.target
[Service]
User=www-data
WorkingDirectory=/var/www/example.com
ExecStart=/var/www/example.com/example-app queue:work
Restart=always
[Install]
WantedBy=multi-user.target
# crontab -u www-data -e
* * * * * cd /var/www/example.com && ./example-app schedule:run >> /dev/null 2>&1
The WorkingDirectory= matters. The binary looks for public/, storage/, resources/ and .env in the directory it starts from. If you can’t start it from there, set APP_BASE_PATH in the unit with Environment=. Don’t set it in .env. The app looks for .env in the directory it starts from, so it would never find that file.
Raise LimitNOFILE, as in the unit above. systemd gives a service 1,024 open files by default, and the binary doesn’t raise that itself. Every connection takes one, so you run out long before the 8,192 connections the app accepts. When that happens, requests fail with unable to open database file, not with a clear “too many open files” error.
By default the app listens on 127.0.0.1:8000. That’s what you want behind a proxy on the same server, so there’s nothing to set. Use SERVER_HOST and SERVER_PORT for anything else. The docs don’t mention listening on a Unix socket. So it’s TCP on 127.0.0.1.
Handling concurrency#
With PHP-FPM, one PHP process handles one request at a time. The pool setting pm.max_children is the number of requests your server can work on at once. When all children are busy, new requests wait in line, and FPM writes this to its log:
WARNING: [pool www] server reached pm.max_children setting (5), consider raising it
Laravel Rust works differently. It’s one process, running Tokio
. Tokio starts one worker thread per CPU core. Each thread juggles many requests. When a request waits on the database, Redis or an HTTP call, the thread works on another request in the meantime. Requests aren’t tied to a thread: any free thread picks up any waiting request. So a 4-core server has 4 worker threads, and they share one limit for the whole process. That’s SERVER_MAX_CONNECTIONS, 8,192 open connections by default. It’s not per thread. Behind Nginx, one connection carries one request at a time, so that’s about 8,192 requests at once.
That changes what you tune:
PHP-FPM
; /etc/php/8.5/fpm/pool.d/www.conf (the default values)
pm = dynamic
pm.max_children = 5
pm.start_servers = 2
pm.min_spare_servers = 1
pm.max_spare_servers = 3
The default of 5 is low for any real site. You size pm.max_children by memory: the RAM you can give PHP, divided by the memory one PHP process uses. Then you reload PHP-FPM.
Rust
# .env
# Worker threads. Leave it out to get one per CPU core.
TOKIO_WORKER_THREADS=4
# Open connections the server accepts, including idle keep-alive ones.
SERVER_MAX_CONNECTIONS=8192
# Database connections in the pool.
DB_MAX_CONNECTIONS=10
The connection values shown are the defaults. Most servers don’t need to change any of them.
There’s no max_children to size. The thread count follows the CPU cores the process is allowed to use. In a container with a CPU limit of 4, you get 4 threads. You only set TOKIO_WORKER_THREADS to use fewer threads, for example when the app shares the server with MySQL. That’s a Tokio setting, not a Laravel one, so you won’t find it in the Laravel Rust docs. It works from .env and from the systemd unit.
The setting that does matter is the database pool. Each request that runs a query borrows a connection from the pool. The default is 10 connections. When all 10 are in use, the next query waits, for up to 30 seconds. So for a page that queries the database, DB_MAX_CONNECTIONS is your real limit, much like pm.max_children is with PHP. Raise it if requests queue up, but keep the total of all your app instances below your database’s connection limit. The database docs
have the details.
CPU-heavy work is different. A handler that keeps the CPU busy, like resizing an image or building a big export, holds on to its worker thread until it’s done. Other requests on that thread have to wait. With PHP-FPM, a slow request only blocks its own process. In Laravel Rust, move that work to task::spawn_blocking
. That runs it on a separate pool of threads.
So do you need to run several binaries? Not for concurrency. One process already uses every core. You’d run more than one to restart without downtime (see below), or to spread over several servers.
Queue workers follow the same idea. One ./example-app queue:work --concurrency=8 process runs up to 8 jobs at once. That works well for jobs that wait on the network or the database. You can still run several worker processes, as you do today.
Deploying a new version#
With PHP, a deploy puts new files on disk. You run git pull and composer install, you migrate, and you reload PHP-FPM to clear OPcache. The next request picks up the new code.
With Rust, the code is in the binary. So you build it and restart. The simplest place to build it is where it runs: on the web server, the same way you run composer install there today. That needs Rust and Node on the server, and the build keeps the CPU busy while it runs.
Don’t build it on a newer system than the server. The binary needs the glibc version of the machine that built it. A binary I built on Debian 12 refuses to start on Ubuntu 20.04:
/app: /lib/x86_64-linux-gnu/libc.so.6: version `GLIBC_2.34' not found (required by /app)
If you build in CI instead, build on the same OS version as your server.
PHP-FPM
cd /var/www/example.com
git pull
composer install --no-dev --optimize-autoloader
npm ci && npm run build
php artisan migrate --force
php artisan optimize
sudo systemctl reload php8.5-fpm
sudo systemctl restart example-app-queue
Rust
cd /var/www/example.com
git pull
cargo build --release
npm ci && npm run build
cp target/release/example-app example-app.new
mv example-app.new example-app
./example-app migrate --force
sudo systemctl restart example-app example-app-queue
Two things here. First, you can’t cp over a binary that’s running:
$ cp /tmp/new-app example-app
cp: cannot create regular file 'example-app': Text file busy
Copy it next to the old one and mv it into place. The rename swaps the file in one step. The running process keeps using the old file until it restarts.
Second, the restart. On SIGTERM the app stops accepting new connections. It gives running requests up to 20 seconds to finish (SERVER_SHUTDOWN_TIMEOUT). But nothing is listening until the new process has started. During that time, Nginx answers with a 502.
For a small app, a fraction of a second of 502s per deploy is probably fine. If it isn’t, run two instances on two ports behind the proxy and restart them one at a time. Or use Laravel Cloud . It’s clearly where Laravel would like you to host it.
Smaller differences#
- Streaming works through Nginx without
proxy_buffering off. Laravel Rust sendsX-Accel-Buffering: noon its event streams, and Nginx respects that. - Nginx’s upload limit is lower than the app’s. The app takes multipart uploads up to 16 MB, but Nginx’s default
client_max_body_sizeis 1 MB. A 2 MB upload got a413from Nginx before the app ever saw it. PHP has the same problem, plus theupload_max_filesizeinphp.ini. Caddy has no limit by default. - The security headers moved into the app. Laravel’s PHP Nginx config adds
X-Frame-OptionsandX-Content-Type-Optionsin Nginx. Laravel Rust adds both itself, on every response, so I left them out of the Rust configs. Static files that Nginx or Caddy serves from disk won’t get them, though. - The binary doesn’t set
Cache-Controlonpublic/build/, the same as PHP’s setup. If you want long cache times on your hashed Vite assets, add that in Nginx or Caddy.
Is it worth it?#
On the web server side, I like it. The Rust configs are shorter than the PHP ones. They’re the same reverse proxy configs you’d write for any Go or Node app. The Host header and trust_proxies are the only lines you have to get right.
The extra work is in running the app. You get a unit file per process, a build step that takes minutes instead of seconds, and a restart on every deploy. That’s normal for any compiled app, but it’s new if you’ve only deployed PHP. If you tried Laravel Octane or FrankenPHP , it’ll feel familiar. That’s also a long-running app behind a proxy, with the same deploy and memory questions.
Laravel Rust is a beta. The docs warn that releases before 1.0 can still break APIs and config. I’ll update this post if the way you deploy it changes. If you’re running it behind something else (HAProxy, Traefik, …), I’d love to hear how it went!