# Deployment - [Introduction](#introduction) - [Building for production](#building-for-production) - [What goes on the server](#what-goes-on-the-server) - [Environment configuration](#environment-configuration) - [Running the server](#running-the-server) - [Stopping gracefully](#stopping-gracefully) - [Behind a load balancer](#behind-a-load-balancer) - [Health checks](#health-checks) - [Limiting connections](#limiting-connections) - [Running migrations](#running-migrations) - [Queue workers and the scheduler](#queue-workers-and-the-scheduler) - [Deploying to Laravel Cloud](#deploying-to-laravel-cloud) ## 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](#deploying-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: ```shell cargo build --release npm 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](https://inertiajs.com/docs/v3/advanced/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](views.md) 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`](configuration.md#the-base-path) to your application's directory. > [!NOTE] > 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](filesystem.md#laravel-cloud-object-storage) or on an [S3-compatible disk](filesystem.md#s3-compatible-disks) 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: ```ini APP_ENV=production APP_DEBUG=false APP_KEY=base64:... APP_URL=https://example.com DATABASE_URL=postgres://app:secret@db.internal/example ``` Use a different key in production than on your machine. On [Laravel Cloud](#deploying-to-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](configuration.md) shows you how to add settings of your own and read them anywhere in your code. > [!WARNING] > 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: ```shell ./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: ```ini SERVER_HOST=0.0.0.0 SERVER_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](responses.md#event-streams) close immediately so browsers reconnect to the new instance. [Ticks](tasks.md#ticks) stop scheduling new runs, and any tick marked `run_on_shutdown` executes one final time after active requests and [background work](tasks.md#managed-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://`: ```rust // src/app.rs .with_middleware(|middleware, _| { middleware.trust_proxies(["10.0.0.0/8"]); }) ``` Not sure what to put in that list? The [trusting proxies](middleware.md#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](routing.md#the-health-route) 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](responses.md#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: ```ini SERVER_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](migrations.md) 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: ```shell ./example-app migrate --force ``` If you deploy to several servers, run `migrate` from only one of them. On [Laravel Cloud](#deploying-to-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](queues.md), run at least one queue worker under your process manager, next to the server: ```shell ./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](scheduling.md), add one cron entry that runs the scheduler every minute: ```shell * * * * * cd /path/to/example-app && ./example-app schedule:run >> /dev/null 2>&1 ``` If your schedule has [sub-minute tasks](scheduling.md#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: ```shell ./example-app schedule:interrupt ``` ## Deploying to Laravel Cloud [Laravel Cloud](https://laravel.com/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: - [Autoscaling](https://laravel.com/cloud/docs/compute#autoscaling) that follows your traffic, and [scale-to-zero](https://laravel.com/cloud/docs/compute#scale-to-zero) when your application is idle, so you only pay for what you use - [Preview environments](https://laravel.com/cloud/docs/preview-environments) for every pull request - Managed [MySQL](https://laravel.com/cloud/docs/resources/databases/laravel-mysql) and [Postgres](https://laravel.com/cloud/docs/resources/databases/postgres) databases, and [Valkey](https://laravel.com/cloud/docs/resources/caches/valkey) caches - [Managed queues](https://laravel.com/cloud/docs/queues) whose workers scale with the jobs waiting - A built-in [scheduler](https://laravel.com/cloud/docs/scheduled-tasks) that runs your scheduled tasks, waking your application on time even when it has scaled to zero - [Object storage](https://laravel.com/cloud/docs/resources/object-storage) for your application's files - Managed [WebSocket servers](https://laravel.com/cloud/docs/resources/websockets) - HTTPS, a global [edge network](https://laravel.com/cloud/docs/network), [logs](https://laravel.com/cloud/docs/logs), and [metrics](https://laravel.com/cloud/docs/metrics/overview), with nothing to configure 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: ```shell # Build commands cargo build --release npm ci && npm run build # Deploy commands example-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](queues.md#laravel-cloud). For [broadcasting](broadcasting.md), use Cloud's managed Reverb instead of running the local [`reverb:start`](websockets.md) server in production. Application logs, scheduled command output, and reported server errors stream to Cloud's [Logs](https://laravel.com/cloud/docs/logs) page automatically. Cloud runs your [scheduled tasks](scheduling.md) too, with no cron entry to manage. Enable the **Scheduler** toggle on your environment's App cluster, as Cloud's [scheduled tasks guide](https://laravel.com/cloud/docs/scheduled-tasks) 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`](scheduling.md#running-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](https://laravel.com/cloud).