Skip to content

Deployment

Getting Started

Deployment

Introduction

When you're ready to put your application in front of real users, you'll build it for production, give it a production environment, and keep its server, queue workers, and scheduler running. This guide walks through each step. If you'd rather not manage servers at all, skip ahead to Laravel Cloud, the fastest way to ship and scale a Laravel Rust application.

Building for production

Your application compiles to a single binary, with your migrations built in. Build it in release mode, and build your frontend assets alongside it:

1cargo build --release
2npm run build
1cargo build --release
2npm run build

Cargo writes the binary to target/release/, named after your project, such as target/release/example-app. Release builds take longer than development builds, but they run much faster, so always deploy a release build.

If you use Inertia server-side rendering, build with npm run build:ssr instead, and set INERTIA_SSR=true in your production environment.

What goes on the server

The binary holds your Rust code, but your application still reads a few directories while it runs. Put these next to the binary on your server:

  • public/, which holds the assets that npm run build writes to public/build/
  • resources/views/, which holds your views and the root view every page renders in
  • storage/, which your application writes to, such as for uploads and log files
  • bootstrap/ssr/, if you use server-side rendering

Start the binary from your application's directory, the one that contains public/, resources/, and storage/. If your process manager starts it from somewhere else, set APP_BASE_PATH to your application's directory.

A local storage/ directory belongs to one server, and many hosts erase it on every deploy. In production, keep uploads in Laravel Cloud Object Storage or on an S3-compatible disk instead.

Environment configuration

Your production environment needs its own settings, either in a .env file on the server or in your host's environment variables. At a minimum, set these:

1APP_ENV=production
2APP_DEBUG=false
3APP_KEY=base64:...
4APP_URL=https://example.com
5DATABASE_URL=postgres://app:[email protected]/example
1APP_ENV=production
2APP_DEBUG=false
3APP_KEY=base64:...
4APP_URL=https://example.com
5DATABASE_URL=postgres://app:[email protected]/example

Use a different key in production than on your machine. On Laravel Cloud, each environment gets its own key automatically. On your own servers, generate one with ./example-app key:generate --show. As your application grows, the configuration guide shows you how to add settings of your own and read them anywhere in your code.

Always set APP_DEBUG to false in production. With debug mode on, server errors render a page with the error, its causes, the route, and the request, for anyone to see.

Running the server

Run the binary with no arguments to serve your application:

1./example-app
1./example-app

The server listens on 127.0.0.1:8000 by default. In a container, or anywhere the server must accept connections from other machines, listen on every interface instead:

1SERVER_HOST=0.0.0.0
2SERVER_PORT=8000
1SERVER_HOST=0.0.0.0
2SERVER_PORT=8000

Run the server under a process manager, such as systemd or your container platform, so it starts with the machine and restarts if it stops. To deploy a new version, replace the binary and restart the process.

Stopping gracefully

When your process manager stops the previous server with SIGTERM (or when you press Ctrl+C), Laravel stops accepting new connections and gives in-flight requests time to finish. SERVER_SHUTDOWN_TIMEOUT controls how long the server waits, defaulting to 20 seconds. Any request still running when the timeout expires receives a 503 response. Keep this value shorter than your process manager's kill grace period so the server exits cleanly before being terminated.

During shutdown, event streams close immediately so browsers reconnect to the new instance. Ticks stop scheduling new runs, and any tick marked run_on_shutdown executes one final time after active requests and background work finish.

Behind a load balancer

Most production servers sit behind a load balancer that handles HTTPS. Tell Laravel to trust it, so request.ip() returns the client's address and the URLs your application generates start with https://:

1// src/app.rs
2.with_middleware(|middleware, _| {
3 middleware.trust_proxies(["10.0.0.0/8"]);
4})
1// src/app.rs
2.with_middleware(|middleware, _| {
3 middleware.trust_proxies(["10.0.0.0/8"]);
4})

Not sure what to put in that list? The trusting proxies section of the middleware guide helps you find your load balancer's addresses, and explains when it's safe to trust every proxy instead.

Health checks

Load balancers and uptime monitors often need a way to tell whether your application is running. Your application answers at /up, which responds with a 200 once your application has started. Point your load balancer's health check at this URL. If /up clashes with one of your routes, or your load balancer expects another path, the routing guide shows you how to change it.

Limiting connections

By default, Laravel accepts up to 8,192 concurrent TCP connections (including idle keep-alive connections and open event streams) so a sudden traffic spike or flood of slow clients can't exhaust your server's memory. Once the server reaches that limit, new connections wait until an open one closes.

The default works well for most applications, though you may adjust it with the SERVER_MAX_CONNECTIONS environment variable:

1SERVER_MAX_CONNECTIONS=2048
1SERVER_MAX_CONNECTIONS=2048

Lower the limit on memory-constrained servers, such as a 512 MB container, or raise it if your application holds thousands of long-lived connections open at once. You may also set SERVER_MAX_CONNECTIONS=0 to disable the limit.

Running migrations

Run your migrations as part of each deploy, before the new version starts serving requests. In production, migrate asks you to confirm before it runs, so pass --force in a deploy script:

1./example-app migrate --force
1./example-app migrate --force

If you deploy to several servers, run migrate from only one of them. On Laravel Cloud, you don't need to arrange this yourself: put example-app migrate --force in your deploy commands, and Cloud runs them once for each deploy, however many instances you have.

Queue workers and the scheduler

If your application dispatches queued jobs, run at least one queue worker under your process manager, next to the server:

1./example-app queue:work
1./example-app queue:work

A worker runs the code it was compiled with, so restart your workers on every deploy, just as you restart the server.

If your application has scheduled tasks, add one cron entry that runs the scheduler every minute:

1* * * * * cd /path/to/example-app && ./example-app schedule:run >> /dev/null 2>&1
1* * * * * cd /path/to/example-app && ./example-app schedule:run >> /dev/null 2>&1

If your schedule has sub-minute tasks, run schedule:interrupt once the new build is in place, so a schedule:run that's already repeating them stops instead of running your previous build until the end of the minute:

1./example-app schedule:interrupt
1./example-app schedule:interrupt

Deploying to Laravel Cloud

Laravel Cloud is a fully managed platform from the team behind Laravel, and it's the best way to ship and scale your Laravel Rust application. Push to Git, and Cloud builds your application, runs your migrations, and rolls out the new version with no downtime.

Cloud takes care of the parts of production that are hardest to get right:

Laravel already knows how to run on Cloud. When your application starts there, it listens on the address and port Cloud gives it, with no SERVER_HOST or SERVER_PORT to set, and each environment gets its own APP_KEY. When you create your Cloud environment, add your frontend build to its build commands and your migrations to its deploy commands:

1# Build commands
2cargo build --release
3npm ci && npm run build
4 
5# Deploy commands
6example-app migrate --force
1# Build commands
2cargo build --release
3npm ci && npm run build
4 
5# Deploy commands
6example-app migrate --force

Cloud runs your binary to serve your application. For queued jobs, enable the sqs feature and set QUEUE_CONNECTION=cloud to use Cloud's managed queues. For broadcasting, use Cloud's managed Reverb instead of running the local reverb:start server in production.

Application logs, scheduled command output, and reported server errors stream to Cloud's Logs page automatically.

Cloud runs your scheduled tasks too, with no cron entry to manage. Enable the Scheduler toggle on your environment's App cluster, as Cloud's scheduled tasks guide shows, and Cloud runs schedule:run every minute. When your environment runs several instances, mark tasks that should run only once with on_one_runner, and set CACHE_STORE to a store every instance shares, such as database.

To deploy your first application, get started with Laravel Cloud.