Deploying a Go Application on VPS: A 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 is Ideal for VPS, and What We Will Do



Go (Golang) is a language created for scalable and high-performance server applications. Compilation into a single static binary file makes it an ideal candidate for deployment on a VPS (Virtual Private Server). You don't need to install an interpreter or dependencies on the server — just copy the file and run it.



In this article, we will go through the entire process: from writing a simple HTTP server in Go to running it in production on a VPS using systemd for auto-start, Nginx as a reverse proxy, and Let's Encrypt for HTTPS.



You will learn:

  • How to compile a Go application for Linux
  • How to upload the binary file to a VPS via SSH
  • How to configure systemd for automatic startup
  • How to install and configure Nginx
  • How to obtain a free SSL certificate


Step 1: Preparing the Go Application and Compilation



Creating a Simple Web Application



For an example, let's write a minimal HTTP server that responds with "Hello, VPS!". Create a file main.go:



package main

import ( "fmt" "net/http")

func main() { http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { fmt.Fprintf(w, "Hello, VPS!") })

fmt.Println("Server started on :8080") http.ListenAndServe(":8080", nil)}


Compilation for Linux



If you are developing on Windows or macOS, you need to compile the binary for the target VPS architecture (usually linux/amd64). Open a terminal in the project folder and run:



set GOOS=linuxset GOARCH=amd64go build -o myapp


For Linux/macOS use export:



export GOOS=linuxexport GOARCH=amd64go build -o myapp


As a result, a binary file myapp will appear. Check its size — it will be small (about 7-10 MB), because Go compiles everything statically.



Step 2: Uploading to VPS and Configuring systemd



Connecting to the Server and Uploading the File



Connect to your VPS via SSH:



ssh root@your-server-ip


Create a directory for the application:



mkdir -p /opt/myappcd /opt/myapp


On your local machine, upload the binary via SCP (run in a separate terminal):



scp myapp root@your-server-ip:/opt/myapp/


Return to the server and grant execute permissions:



chmod +x /opt/myapp/myapp


Creating a systemd Service



To make the application start automatically at system boot and restart on failures, we will create a systemd unit. Create the file:



nano /etc/systemd/system/myapp.service


Insert the following content:



[Unit]Description=My Go ApplicationAfter=network.target

[Service]Type=simpleUser=www-dataGroup=www-dataWorkingDirectory=/opt/myappExecStart=/opt/myapp/myappRestart=on-failureRestartSec=5

[Install]WantedBy=multi-user.target


Explanation of parameters:

  • User and Group — the user under which the application will run (root is not recommended).
  • Restart=on-failure — automatic restart on crash.
  • RestartSec=5 — pause before restarting.


Now activate and start the service:



systemctl daemon-reloadsystemctl enable myappsystemctl start myappsystemctl status myapp


If everything is done correctly, you will see the status active (running). Check that the application responds locally:



curl http://localhost:8080


Step 3: Configuring Nginx as a Reverse Proxy



Direct access to the application via port 8

Blogs

Book Recommendations