What you need before starting
- A Linux server with SSH and a domain.
- Your project with
go.modand amainthat reads the port and configuration from environment variables.
The steps, in short
- Compile for Linux from any operating system:
CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -o myapp ./cmd/myapp
- Upload the binary with
scpor from your pipeline and keep it in a folder such as/opt/myapp/. - Create a service user without administrator permissions to run it.
- Define a systemd service with
Restart=alwaysand anEnvironmentFilefor your environment variables. - Put a proxy in front: Caddy obtains and renews the HTTPS certificate by itself; Nginx also works if you already know it.
- Automate: build and upload on every push with GitHub Actions and restart the service.
Common mistakes
- Compiling with CGO enabled and the binary failing on the server because of missing libraries.
- Using relative paths for config files or templates: the service starts from a different folder.
- Not setting timeouts on the HTTP server.
- Running the binary as root.
How we help
We recommend the simplest route for your case and leave it running with automatic restarts, HTTPS, logs and continuous deployment.