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 Go application.

Building for production

Your application compiles to a single binary, with your migrations, seeders, and views built in. Build your frontend assets first, then the binary:

1npm run build
2go build -o app .
1npm run build
2go build -o app .

go build writes the binary to app in your application's directory. If your server runs a different operating system or processor than your machine, such as Linux on an x86 server while you work on a Mac, set GOOS and GOARCH when you build:

1GOOS=linux GOARCH=amd64 go build -o app .
1GOOS=linux GOARCH=amd64 go build -o app .

If you use Inertia's 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 Go code and your views, 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/
  • 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/ 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 in your application's directory or in your host's environment variables. When both set the same variable, Laravel uses the host's value. 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, Cloud generates a separate key for each environment. On your own servers, generate one with ./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, a server error's page shows the error's message, and a panic's stack, to anyone who visits it.

Running the server

Run the binary with no arguments to serve your application:

1./app
1./app

The server listens on 127.0.0.1:8000 by default, which only your own machine can reach. 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 old server with SIGTERM (or when you press Ctrl+C), Laravel stops accepting new connections and gives the requests in progress time to finish. The server waits up to 20 seconds. To change that limit, set SERVER_SHUTDOWN_TIMEOUT to a number of seconds a little below the time your process manager waits before it kills the process, so the server exits on its own first:

1SERVER_SHUTDOWN_TIMEOUT=30
1SERVER_SHUTDOWN_TIMEOUT=30

If a request is still running when the limit runs out, Laravel cancels the request's context and answers with a 503.

While the server stops, it closes your event streams right away so browsers reconnect to the new server. Your ticks stop starting new runs, and any tick marked RunOnShutdown runs one last time after the requests and background work in progress finish.

Behind a load balancer

Most production servers sit behind a load balancer that handles HTTPS. Tell Laravel to trust the load balancer in bootstrap/app.go, so r.IP() returns the visitor's address and the URLs your application generates start with https://:

1// bootstrap/app.go
2Middleware: func(m *laravel.Middleware, cfg *config.Config) {
3 m.Proxies = []string{"10.0.0.0/8"}
4},
1// bootstrap/app.go
2Middleware: func(m *laravel.Middleware, cfg *config.Config) {
3 m.Proxies = []string{"10.0.0.0/8"}
4},

Not sure what to put in that list? The trusting proxies section of the middleware guide explains which addresses to list, and 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./app migrate --force
1./app migrate --force

If you deploy to several servers, run migrate from one server or deploy step. Laravel holds a database lock while migrating, so each migration still runs once if two servers run it at the same time. On Laravel Cloud, put ./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./app queue:work
1./app queue:work

A worker runs the code it was built with, so restart your workers on every deploy, as you restart the server. Run ./app queue:restart after you replace the binary. Every worker finishes the jobs it's running and exits, and your process manager starts each worker again with the new binary. To learn more, check out the queues guide's section on running workers in production.

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

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

If your schedule has sub-minute tasks, run ./app schedule:interrupt after you replace the binary, so a schedule:run that's already repeating them stops instead of running your previous build until the end of the minute.

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 Go 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:

You don't need to configure Laravel for Cloud. When your application starts there, it listens on the address and port that Cloud gives it, so you don't set SERVER_HOST or SERVER_PORT, and it trusts Cloud's proxies. Cloud also generates a separate APP_KEY for each environment. When you create your Cloud environment, add your frontend and Go builds to its build commands and your migrations to its deploy commands:

1# Build commands
2npm ci && npm run build
3go build -o app .
4 
5# Deploy commands
6./app migrate --force
1# Build commands
2npm ci && npm run build
3go build -o app .
4 
5# Deploy commands
6./app migrate --force

Cloud runs your binary to serve your application. For queued jobs, 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 OnOneRunner, and set CACHE_STORE to a store every instance shares, such as database.

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