On this page
- Why use the official tarball instead of the distro package?
- What should be ready before you begin?
- How do you install Go from the official archive?
- How do you write and build a small service?
- How do you run the binary under systemd?
- How do you add HTTPS with Nginx in front?
- How does autocert give the binary its own certificate?
- Which firewall rules does the service need?
- How do you upgrade Go and redeploy safely?
- Questions
Why use the official tarball instead of the distro package?
The tarball gives you the current Go release on every distribution, while the packaged golang on Ubuntu, Debian and the RHEL family usually trails by one or more minor versions. Go keeps strong backward compatibility, but modules increasingly declare a recent go line in go.mod, and an old toolchain then has to download a newer one before it can build. Installing the official archive avoids that detour and behaves the same on Ubuntu 24.04, Ubuntu 22.04, Debian 12 and 13, Rocky Linux 9 and AlmaLinux 9.
Go programs compile to self-contained binaries, so the toolchain only has to live where you build. Many people compile on the server itself because it is simple; others build on a laptop or in CI and copy only the binary. Both work, and the service part of this page is identical either way.
What should be ready before you begin?
A normal user with sudo rights and a working SSH login are enough; the first SSH session tutorial covers that. Go itself needs about 250 MB of disk. Compiling a small service fits in 1 GB of RAM, though large projects with many dependencies build faster with 2 GB or a swap file. Install curl and tar if your image lacks them.
# Ubuntu and Debian
sudo apt update
sudo apt install -y curl tar git
# Rocky Linux and AlmaLinux 9
sudo dnf install -y curl tar gitCheck the CPU architecture so you fetch the right archive. Every BitcoinVPS plan reports x86_64, which Go calls amd64.
uname -mHow do you install Go from the official archive?
Ask go.dev for the current version string, download the matching archive, remove any previous tree and extract the new one into /usr/local. Extracting over an old install without removing it first can leave stale files behind, so the rm step matters.
GOVER=$(curl -fsSL "https://go.dev/VERSION?m=text" | head -n1)
echo "$GOVER"
curl -fsSLO "https://go.dev/dl/${GOVER}.linux-amd64.tar.gz"
sha256sum "${GOVER}.linux-amd64.tar.gz"
sudo rm -rf /usr/local/go
sudo tar -C /usr/local -xzf "${GOVER}.linux-amd64.tar.gz"Compare the printed checksum with the SHA256 value listed next to that file on the go.dev downloads page before you continue. Then put the toolchain on your PATH for login shells and reload the profile:
echo 'export PATH=$PATH:/usr/local/go/bin:$HOME/go/bin' | sudo tee /etc/profile.d/go.sh
source /etc/profile.d/go.sh
go version
go env GOROOT GOPATHYou should see the version you downloaded, /usr/local/go as GOROOT and /home/deploy/go as GOPATH. Tools installed with go install land in $HOME/go/bin, which the line above also adds.
How do you write and build a small service?
Create a module, add one file and build it. The example below listens on 127.0.0.1 so nothing is exposed until a proxy or firewall rule says so, and it reads the address from an environment variable for flexibility.
mkdir -p ~/hello && cd ~/hello
go mod init example.net/hello
nano main.gopackage main
import (
"fmt"
"log"
"net/http"
"os"
)
func main() {
addr := os.Getenv("ADDR")
if addr == "" {
addr = "127.0.0.1:8080"
}
mux := http.NewServeMux()
mux.HandleFunc("GET /", func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintln(w, "hello from go")
})
mux.HandleFunc("GET /healthz", func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
})
log.Printf("listening on %s", addr)
log.Fatal(http.ListenAndServe(addr, mux))
}gofmt -w .
CGO_ENABLED=0 go build -trimpath -ldflags="-s -w" -o hello .
./hello &
curl http://127.0.0.1:8080/
kill %1The method-and-path patterns such as GET /healthz need Go 1.22 or newer, which any current tarball satisfies. CGO_ENABLED=0 produces a fully static binary that runs on any of the supported images without extra libraries.
How do you run the binary under systemd?
Install the binary with install, write a unit file and enable it. Using install rather than mv matters on Rocky and Alma: a moved file keeps its home-directory SELinux label and systemd will refuse to execute it, while a freshly created copy gets the normal bin_t label.
sudo install -m 0755 ~/hello/hello /usr/local/bin/hello
sudo nano /etc/systemd/system/hello.service[Unit]
Description=hello Go service
After=network-online.target
Wants=network-online.target
[Service]
ExecStart=/usr/local/bin/hello
Environment=ADDR=127.0.0.1:8080
DynamicUser=yes
Restart=on-failure
RestartSec=2
NoNewPrivileges=yes
ProtectSystem=strict
ProtectHome=yes
[Install]
WantedBy=multi-user.targetsudo systemctl daemon-reload
sudo systemctl enable --now hello
systemctl status hello --no-pager
sudo ss -ltnp | grep 8080
journalctl -u hello -n 20 --no-pagerDynamicUser=yes runs the process as a throwaway user with no login and no home, which is a good default for a stateless service. More options for unit files are explained in managing systemd services.
How do you add HTTPS with Nginx in front?
A reverse proxy is the usual choice when the server hosts more than one site or you want access logs, compression and rate limits in one place. Point an A and AAAA record for app.example.net at 198.51.100.24 and 2001:db8:4f2a::24 first, as shown in the DNS records tutorial, then install Nginx and Certbot.
# Ubuntu and Debian
sudo apt install -y nginx certbot python3-certbot-nginx
# Rocky Linux and AlmaLinux 9
sudo dnf install -y epel-release
sudo dnf install -y nginx certbot python3-certbot-nginx
sudo systemctl enable --now nginx
sudo setsebool -P httpd_can_network_connect 1The SELinux boolean lets Nginx open connections to a local port on the RHEL family. Create a server block at /etc/nginx/conf.d/hello.conf:
server {
listen 80;
listen [::]:80;
server_name app.example.net;
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_header X-Forwarded-Proto $scheme;
}
}sudo nginx -t && sudo systemctl reload nginx
sudo certbot --nginx -d app.example.netCertbot edits the block to listen on 443 and installs a renewal timer. The Nginx reverse proxy guide goes deeper into headers, timeouts and WebSockets.
How does autocert give the binary its own certificate?
The golang.org/x/crypto/acme/autocert package requests and renews Let's Encrypt certificates from inside your program, so a single-purpose server needs no proxy at all. Add the dependency and replace the final listen call:
go get golang.org/x/crypto/acme/autocertm := &autocert.Manager{
Prompt: autocert.AcceptTOS,
HostPolicy: autocert.HostWhitelist("app.example.net"),
Cache: autocert.DirCache("/var/lib/hello/certs"),
}
srv := &http.Server{
Addr: ":443",
Handler: mux,
TLSConfig: m.TLSConfig(),
}
go http.ListenAndServe(":80", m.HTTPHandler(nil))
log.Fatal(srv.ListenAndServeTLS("", ""))Ports below 1024 need a capability, and the certificate cache needs a writable directory. Add these lines to the [Service] section, then rebuild, reinstall and restart:
AmbientCapabilities=CAP_NET_BIND_SERVICE
StateDirectory=helloStateDirectory creates /var/lib/hello owned by the dynamic user, so certificates survive restarts and you stay well inside the issuance rate limits.
Which firewall rules does the service need?
Open only what clients connect to: 80 and 443 for either HTTPS method, plus SSH. Keep 8080 closed, because it is bound to localhost anyway. Filtering on a BitcoinVPS instance happens inside the operating system; see UFW basics or firewalld.
# Ubuntu and Debian
sudo ufw allow OpenSSH
sudo ufw allow 80,443/tcp
sudo ufw enable
# Rocky Linux and AlmaLinux 9
sudo firewall-cmd --permanent --add-service=http --add-service=https
sudo firewall-cmd --reloadTest from your own computer with curl -I https://app.example.net/; a 200 status and a valid certificate mean the chain works end to end.
How do you upgrade Go and redeploy safely?
Upgrading the toolchain is the same three steps as installing: fetch the new archive, remove /usr/local/go, extract. Running binaries are not affected until you rebuild them, because the runtime is compiled in. A short deploy routine keeps the old binary around for a quick rollback:
cd ~/hello
git pull
go build -trimpath -ldflags="-s -w" -o hello.new .
sudo cp /usr/local/bin/hello /usr/local/bin/hello.prev
sudo install -m 0755 hello.new /usr/local/bin/hello
sudo systemctl restart hello
curl -fsS http://127.0.0.1:8080/healthz && echo healthyRun go list -m -u all now and then to see dependency updates and govulncheck ./... (installed with go install golang.org/x/vuln/cmd/govulncheck@latest) to spot known vulnerabilities in the modules you actually call.
Plans that fit this guide
Base city prices. Every plan includes a dedicated IPv4 address, IPv6, full root and all 18 images.
Questions about Install Go and run a Go service
Can I install Go with apt or dnf instead?
Yes, the golang or golang-go packages work and receive security fixes from the distribution, but they are usually older than the current release. Check with apt policy golang-go or dnf info golang and compare against the go line your modules require.
Why does systemd report status 203/EXEC on Rocky Linux?
The binary probably carries a home-directory SELinux label because it was moved, not copied. Run sudo restorecon -v /usr/local/bin/hello or reinstall it with install, then restart the unit.
Do I need Go installed on the server to run a Go program?
No. A compiled binary contains the runtime. You can build elsewhere with GOOS=linux GOARCH=amd64 go build and copy just the file with scp or rsync.
Should I choose Nginx or autocert for TLS?
Choose autocert for a single service that owns ports 80 and 443. Choose Nginx when several sites share the server, or when you want central logs, caching and rate limits.
How much memory does a Go web service use?
A small HTTP service often idles at 10 to 30 MB of resident memory. Building large projects needs far more than running them, so add swap on 1 GB plans if compiles fail.