Deploying a Go Application on VPS: Step-by-Step Guide

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 Deploying a Go Application on a VPS is Simple and Efficient

Go (Golang) is renowned for its performance and simplicity. One of the language's main advantages is compilation into a single static binary that doesn't require additional runtimes to be installed on the server. This makes deploying a Go application on a VPS incredibly fast and reliable. In this article, we'll break down the complete deployment cycle: from server preparation to setting up a reverse proxy and HTTPS. You'll get a ready-made checklist that can be used for any Go project.



1. VPS Preparation and Installing Necessary Software

Before uploading the code, you need to prepare the server. It is assumed that you have a VPS with Ubuntu 22.04 or 24.04. Connect via SSH and perform a system update.

sudo apt update && sudo apt upgrade -y

Installing Go on the Server

Although Go compiles into a binary, a compiler may be needed on the server for building. Let's install the latest version:

wget https://go.dev/dl/go1.22.5.linux-amd64.tar.gzsudo tar -C /usr/local -xzf go1.22.5.linux-amd64.tar.gzexport PATH=$PATH:/usr/local/go/binecho 'export PATH=$PATH:/usr/local/go/bin' >> ~/.profile

Verify the installation: go version.



Installing Git and Nginx

Git will be needed for cloning the repository, and Nginx for the reverse proxy and static files.

sudo apt install git nginx -y


2. Building and Running the Go Application

Let's look at the process using a simple HTTP server as an example. Create a file main.go:

package main

import (\t"fmt"\t"net/http")

func handler(w http.ResponseWriter, r *http.Request) {\tfmt.Fprintf(w, "Hello from Go on VPS!")}

func main() {\thttp.HandleFunc("/", handler)\tfmt.Println("Server started on :8080")\thttp.ListenAndServe(":8080", nil)}

Build the binary for Linux (even if you are working on Windows/Mac):

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

Now upload the binary to the server. Use SCP or Rsync:

scp myapp user@your-server-ip:~/

On the server, make the file executable and run it:

chmod +x ~/myapp./myapp &

Check if it works: curl http://localhost:8080. If you see "Hello from Go on VPS!" — everything is working.



3. Setting up systemd for Auto-start

Manual startup is not a best practice. Let's use systemd so the application starts when the system boots and restarts on failures.

Create a service file:

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

Paste the configuration:

[Unit]Description=Go Application ServiceAfter=network.target

[Service]Type=simpleUser=www-dataWorkingDirectory=/home/userExecStart=/home/user/myappRestart=alwaysRestartSec=5

[Install]WantedBy=multi-user.target

Replace /home/user with your path. Activate and start the service:

sudo systemctl daemon-reloadsudo systemctl enable myappsudo systemctl start myappsudo systemctl status myapp

Now your Go application will run as a daemon.



4. Setting up Nginx as a Reverse Proxy and HTTPS

To make the application accessible via a domain and through port 443, let's configure Nginx.

Reverse Proxy Configuration

Remove the default config and create your own:

sudo rm /etc/nginx/sites-enabled/defaultsudo nano /etc/nginx/sites-available/myapp

Add:

server {    listen 80;    server_name yourdomain.com www.yourdomain.com;

location / { proxy_pass http://127.0.0.1:8080; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_h

Blogs

Book Recommendations