Skip to main content
The Go quickstart walks you through your first deployment to Laravel Cloud. Once your application is up and running, this guide helps you get it ready for production. We’ll cover how to compile your Go application, start it, and configure it so that it runs smoothly on Cloud.

Compile during the build

Cloud installs your Go modules automatically and pre-fills a build command based on your application’s entrypoint. If your main package lives at the root of your repository, the default build command looks like this:
If your entrypoint lives in a directory such as cmd/server, Cloud builds that package instead. When your repository contains more than one entrypoint, you may select the one you would like to deploy. Cloud also sets CGO_ENABLED=1 for you when it detects cgo dependencies or C source files. Of course, if your application has additional requirements, you’re free to edit the command. The default start command, ./app, runs the binary produced by your build. Because you compile in your build commands, there’s nothing left to compile at runtime. Your application starts quickly when a new instance boots or when your environment wakes up after scaling to zero. If your application includes other executables, compile them during the build as well. For example, an application with both a server and a migration command might use the following build command:
Remember to use CGO_ENABLED=1 for any binary that requires cgo. If you change your server binary’s name or output path, update your start command to match.

Run your application

Listen on the right port

Cloud routes traffic to your application on the port set by the PORT environment variable, which is 3000 by default. Have your application read this value and leave the host out of its listen address:
When you use a bare :port address with Go’s net/http server, it listens on all of the available IPv4 and IPv6 interfaces. Listening on IPv6 matters because Cloud’s startup probes reach your application over IPv6. If you use an IPv4-only listener such as net.Listen("tcp4", ...), or a loopback-only address, Cloud can’t reach your application. If your application deploys successfully but still isn’t reachable, take a look at the deployment troubleshooting guide.

Shut down gracefully

When an instance stops, whether during a deployment or while scaling down, Cloud sends a SIGTERM signal to your process. To avoid cutting off requests that are still in progress, handle this signal in your application and wait for those requests to finish before exiting. If you are using a net/http server, you may use the following function with your application’s handler:
Call this function from main, and your process keeps running until Shutdown returns. Keep your application’s shutdown timeout shorter than your environment’s graceful shutdown timeout so the rest of the instance has time to stop cleanly. Once that timeout passes, Cloud terminates any process that is still running.

Running behind the proxy

Cloud handles TLS for you before forwarding requests to your application, so your server only needs to speak plain HTTP. Cloud’s proxy also passes the original request scheme in the X-Forwarded-Proto header and the client’s address chain in the X-Forwarded-For header. Before relying on these headers to determine the request scheme or client IP address, configure your framework’s trusted proxy settings. Frameworks such as Gin and Echo include these settings. In general, avoid trusting forwarded headers from arbitrary sources, since clients can set them to any value they like.

Health checks

Cloud’s default health checks don’t request a route in your application, and Cloud doesn’t detect a health route for Go applications. Instead, add a lightweight route such as /up that responds once your application can serve requests:
Then, point your environment’s startup and readiness health checks at /up so a deployment goes live only once your application is ready.

Logging

Anything your application writes to standard output or standard error, including panic messages, appears in your environment’s logs. Go’s log package writes to standard error by default. If you would rather log each entry as a single JSON object, you may use log/slog:

Databases and caches

When you attach a database or cache from the infrastructure canvas, Cloud injects a DATABASE_URL (postgresql://... or mysql://...) or REDIS_URL (rediss://...) variable into your application’s environment. You may read these values with os.Getenv. For Postgres, you may pass the connection URL straight to pgx:
For Redis, go-redis can parse the URL for you, and it configures TLS automatically when it sees the rediss:// scheme:
The MySQL driver is a little different, since it expects its own DSN format rather than a URL. You may parse the connection URL and let mysql.Config build the DSN for you:
If your application needs any driver-specific options, you may add them to the configuration before opening the connection.

Run migrations

Database migrations belong in your environment’s deploy commands. Cloud runs these commands just before your new deployment goes live. If one of them fails, Cloud cancels the rollout. If your application includes the cmd/migrate executable from the build example, your deploy command is:
If you prefer a separate migration tool, install or compile it during your build, then use its command here instead.

Object storage

When you attach a bucket as your environment’s default disk, Cloud injects the AWS_BUCKET, AWS_ENDPOINT_URL, AWS_REGION, AWS_ACCESS_KEY_ID, and AWS_SECRET_ACCESS_KEY variables. For public buckets, Cloud also injects the bucket’s public base URL as AWS_URL. When creating your S3 client with the AWS SDK for Go, be sure to pass the endpoint explicitly:
You don’t need to pass the region or access keys yourself, since the SDK reads them from the environment automatically.

Background workers

To process jobs or run other long-lived work alongside your web server, add a custom background process to your App cluster or a worker cluster. Compile your worker during the build, or give your server binary a subcommand that starts it:
Cloud restarts a background process if it exits unexpectedly. If your worker consumes jobs from Redis, it may connect using the same REDIS_URL as your application.

Scheduled tasks

To run recurring commands, use scheduled tasks. Open your App cluster or a worker cluster on the infrastructure canvas, add a command and schedule under Scheduled commands, then save and deploy. For example, if your binary has a subcommand that prunes expired sessions:
Laravel Cloud wakes a sleeping App cluster to run the command, so you can keep scale to zero enabled.