Deploying a Go Application on VPS: Step-by-Step Guide from A to Z

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Introduction: Why Go and VPS Are the Perfect Pair for Your Project

\n\n

Go (Golang) is a compiled language created for performance and simplicity. Its main advantage during deployment is static compilation. You don't need to install an interpreter or a virtual machine on the server. You simply upload one binary file and run it. This makes deploying a Go application on a VPS incredibly fast and reliable.

\n\n

In this guide, we will go through the entire path: from writing a simple HTTP server in Go to running it on a real VPS server running Ubuntu 22.04. We will set up systemd for automatic startup, Nginx as a reverse proxy, and Let's Encrypt for HTTPS. All code examples are ready to use.

\n\n

1. Preparing the VPS and Writing the Go Application

\n\n

1.1. Initial Server Setup

\n\n

After purchasing a VPS (e.g., from DigitalOcean, Timeweb, or Hetzner), connect via SSH:

\n\n
ssh root@your_server_IP
\n\n

Perform basic security setup:

\n\n
apt update && apt upgrade -y\nadduser deployer\nusermod -aG sudo deployer\n# Disable root login in /etc/ssh/sshd_config\n# PermitRootLogin no\nsystemctl restart sshd
\n\n

Make sure ports 80 and 443 are open in the firewall (UFW):

\n\n
ufw allow 22\nufw allow 80\nufw allow 443\nufw enable
\n\n

1.2. Simple Go Application for Deployment

\n\n

Create a file main.go on your local machine:

\n\n
package main\n\nimport (\n\t"fmt"\n\t"log"\n\t"net/http"\n\t"os"\n)\n\nfunc main() {\n\tport := os.Getenv("PORT")\n\tif port == "" {\n\t\tport = "8080"\n\t}\n\n\thttp.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {\n\t\tfmt.Fprintf(w, "Hello from Go! Server is running on port %s", port)\n\t})\n\n\thttp.HandleFunc("/health", func(w http.ResponseWriter, r *http.Request) {\n\t\tw.WriteHeader(http.StatusOK)\n\t\tw.Write([]byte("OK"))\n\t})\n\n\tlog.Printf("Starting server on :%s", port)\n\tlog.Fatal(http.ListenAndServe(":"+port, nil))\n}
\n\n

Test it locally:

\n\n
go run main.go
\n\n

Open your browser at http://localhost:8080. If you see the greeting, everything is working.

\n\n

2. Building and Uploading the Binary to the Server

\n\n

2.1. Cross-Compilation for Linux

\n\n

On your local machine (even if you have Windows or macOS), run:

\n\n
GOOS=linux GOARCH=amd64 go build -o myapp main.go
\n\n

The -o myapp flag sets the output file name. The environment variables GOOS and GOARCH specify the target architecture (64-bit Linux).

\n\n

After building, you will have a binary file myapp of about 10–15 MB.

\n\n

2.2. Uploading to the VPS

\n\n

Use scp to copy the file:

\n\n
scp myapp deployer@your_server_IP:/home/deployer/myapp
\n\n

Or, if you use rsync:

\n\n
rsync -avz myapp deployer@your_server_IP:/home/deployer/myapp
\n\n

On the server, move the binary to a system directory and make it executable:

\n\n
sudo mv /home/deployer/myapp /usr/local/bin/myapp\nsudo chmod +x /usr/local/bin/myapp
\n\n

3. Setting Up systemd and Automatic Startup

\n\n

3.1. Creating a Service File

\n\n

Systemd will manage our application: start it on system boot, restart it on failure, and collect logs.

\n\n

Create a file /etc/systemd/system/myapp.service:

\n\n
sudo nano /etc/systemd/system/myapp.service
\n\n

Insert the following configuration:

\n\n
[Unit]\nDescription=Go Application Server\nAfter=network.target\n\n[Service]\nType=simple\nUser=deployer\nGroup=deployer\nWorkingDirectory=/usr/local/bin\nExecStart=/usr/local/bin/myapp\nRestart=always\nRestartSec=5\nEnvironment=PORT=8080\nEnvironment=GIN_MO                

Blogs

Book Recommendations