
How to Deploy Go Web Application on Ubuntu • Linux & Open Source
Go, also known as Golang, is an open-source programming language developed by Google. Go is designed for efficiency, ease of use, and scalability. It is very popular for building web applications, microservices, and cloud-based applications. In this guide, we will discuss how to deploy a simple Go web application on Ubuntu using Nginx as a reverse proxy and Systemd to manage the application service.
Install Go
Update and install Go:
sudoaptupdate wgethttps://go.dev/dl/go1.23.3.linux-amd64.tar.gz sudotar–C/usr/local–xzfgo1.23.3.linux–amd64.tar.gz echo‘export PATH=$PATH:/usr/local/go/bin’>>~/.profile source~/.profile |
Verifying Go installation by displaying the version number:
The response to the command:
goversiongo1.23.3linux/amd64 |
Creating a Go App
Create a directory for the application project with the name goweb:
mkdir~/goweb&&cd~/goweb |
Creating the main.go file:
The contents of the file main.go:
import (
“fmt”
“net/http”
)
func handler(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, “Hello, World!”)
}
func main() {
http.HandleFunc(“/”, handler)
http.ListenAndServe(“:8080”, nil)
}
packagemain import( “fmt” “net/http” ) funchandler(whttp.ResponseWriter,r *http.Request){ fmt.Fprintf(w,“Hello, World!”) } funcmain(){ http.HandleFunc(“/”,handler) http.ListenAndServe(“:8080”,nil) } |
Compile the application:
Creating a Service
Create a systemd service file:
sudonano/etc/systemd/system/goweb.service |
File contents:
[Service]
ExecStart=/home/youruser/goweb/app
Restart=always
User=youruser
Group=youruser
Environment=PORT=8080
[Install]
WantedBy=multi-user.target
[Unit] Description=GoWebApp After=network.target [Service] ExecStart=/home/youruser/goweb/app Restart=always User=youruser Group=youruser Environment=PORT=8080 [Install] WantedBy=multi–user.target |
Enable the service:
sudosystemctldaemon–reload sudosystemctlenablegoweb sudosystemctlstartgoweb sudosystemctlstatusgoweb |
Nginx Configuration
Install Nginx:
sudoaptinstallnginx–y |
Create the Nginx configuration file:
sudonano/etc/nginx/conf.d/goweb.conf |
File contents:
location / {
proxy_pass
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection ‘upgrade’;
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
}
server{ listen80; server_nameyourdomain.com; location/{ proxy_passhttp://localhost:8080; proxy_http_version1.1; proxy_set_headerUpgrade$http_upgrade; proxy_set_headerConnection‘upgrade’; proxy_set_headerHost$host; proxy_cache_bypass$http_upgrade; } } |
Restart Nginx:
sudosystemctlrestartnginx |
Test the Application
Open your browser and visit the address:
The result is the text “Hello, World!”.
caleg24
Berita Olahraga
News
Berita Terkini
Berita Terbaru
Berita Teknologi
Seputar Teknologi
Drama Korea
Resep Masakan
Pendidikan
Berita Terbaru
Berita Terbaru
Berita Terbaru