Introduction: Why Go and VPS Are the Perfect Pair for Your Project
\n\nGo (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\nIn 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\n1. Preparing the VPS and Writing the Go Application
\n\n1.1. Initial Server Setup
\n\nAfter purchasing a VPS (e.g., from DigitalOcean, Timeweb, or Hetzner), connect via SSH:
\n\nssh root@your_server_IP\n\nPerform basic security setup:
\n\napt 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\nMake sure ports 80 and 443 are open in the firewall (UFW):
\n\nufw allow 22\nufw allow 80\nufw allow 443\nufw enable\n\n1.2. Simple Go Application for Deployment
\n\nCreate a file main.go on your local machine:
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\nTest it locally:
\n\ngo run main.go\n\nOpen your browser at http://localhost:8080. If you see the greeting, everything is working.
2. Building and Uploading the Binary to the Server
\n\n2.1. Cross-Compilation for Linux
\n\nOn your local machine (even if you have Windows or macOS), run:
\n\nGOOS=linux GOARCH=amd64 go build -o myapp main.go\n\nThe -o myapp flag sets the output file name. The environment variables GOOS and GOARCH specify the target architecture (64-bit Linux).
After building, you will have a binary file myapp of about 10–15 MB.
2.2. Uploading to the VPS
\n\nUse scp to copy the file:
scp myapp deployer@your_server_IP:/home/deployer/myapp\n\nOr, if you use rsync:
\n\nrsync -avz myapp deployer@your_server_IP:/home/deployer/myapp\n\nOn the server, move the binary to a system directory and make it executable:
\n\nsudo mv /home/deployer/myapp /usr/local/bin/myapp\nsudo chmod +x /usr/local/bin/myapp\n\n3. Setting Up systemd and Automatic Startup
\n\n3.1. Creating a Service File
\n\nSystemd will manage our application: start it on system boot, restart it on failure, and collect logs.
\n\nCreate a file /etc/systemd/system/myapp.service:
sudo nano /etc/systemd/system/myapp.service\n\nInsert 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