Introduction: Why Deploying a Go Application on VPS is Simple and Reliable
Go (Golang) is one of the most efficient languages for creating server applications. Its main advantage is compilation into a single static binary that does not require an interpreter or virtual machine to be installed on the server. This makes deploying a Go application on a VPS (Virtual Private Server) incredibly simple and fast compared to Python, Node.js, or Java.
In this article, we will deploy a Go application on a VPS step by step: from initial server setup to running it via systemd and publishing it through Nginx with HTTPS. You will learn how to minimize risks and ensure stable 24/7 operation of your service.
1. Preparing the VPS and Installing Necessary Software
Before starting the deployment, make sure you have SSH access to the VPS (e.g., Ubuntu 22.04 LTS). We will use a basic set of tools.
1.1. Initial Server Setup
Connect to the server and update the packages:
ssh root@your-server-ipapt update && apt upgrade -yCreate a separate user for the application (do not work as root):
adduser deployerusermod -aG sudo deployersu - deployer1.2. Installing Go (if you need to compile on the server)
Although the binary is usually built locally, sometimes it is convenient to compile directly on the VPS. Install the latest version of Go:
wget https://go.dev/dl/go1.22.2.linux-amd64.tar.gzsudo tar -C /usr/local -xzf go1.22.2.linux-amd64.tar.gzecho 'export PATH=$PATH:/usr/local/go/bin' >> ~/.bashrcsource ~/.bashrcgo version1.3. Installing Git and Nginx
Git will be needed for cloning the repository, and Nginx for reverse proxy and SSL:
sudo apt install git nginx -y2. Building the Go Application and Preparing for Deployment
2.1. Example of a Simple Go Application
Let's create a minimal HTTP server in Go. You can use your own project, but for demonstration purposes this code (main.go) will work:
package main
import ( "fmt" "net/http" "os")
func main() { port := os.Getenv("PORT") if port == "" { port = "8080" }
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { fmt.Fprintf(w, "Hello from Go! Server time: %s", time.Now().Format(time.RFC1123)) })
fmt.Printf("Server starting on port %s", port) http.ListenAndServe(":"+port, nil)}2.2. Compiling the Binary for Linux
On your local machine (or on the server), build the binary for the target VPS architecture (usually amd64):
GOOS=linux GOARCH=amd64 go build -o myapp main.goCheck that the binary is static and does not require libraries:
ldd myapp # Should output "not a dynamic executable"2.3. Uploading to the Server
Copy the binary to the VPS via SCP:
scp myapp deployer@your-server-ip:/home/deployer/On the server, move it to a suitable directory and grant execute permissions:
sudo mkdir -p /opt/myappsudo mv /home/deployer/myapp /opt/myapp/sudo chmod +x /opt/myapp/myapp3. Configuring systemd for Autostart and Monitoring
systemd is the standard service manager in Linux. It will allow your application to start at system boot and automatically restart on failures.
3.1. Creating a Unit File
Create the file /etc/systemd/system/myapp.service:
sudo nano /etc/systemd/system/myapp.serviceInsert the following configuration:
[Unit]Description=My Go ApplicationAfter=network.target
[Service]Type=simpleUser=deployerGroup=deployerWorkingDirectory=/opt/myappExecStart=/opt/myapp/myappRestart=alwaysRestartSec=5Environment=PORT=80