This README provides a streamlined guide for setting up dual Golang applications on an Ubuntu server. It focuses on running multiple engines from a singular server, each accessible via unique endpoints through Nginx reverse proxy.
- Ubuntu Server
- Domain Names (e.g., api1.yourdomain.com, api2.yourdomain.com) linked to your server's IP
sudo apt update
sudo apt install golang-goFor app1/main.go:
package main
import (
"fmt"
"net/http"
)
func main() {
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Hello from App 1")
})
http.ListenAndServe(":8080", nil)
}For app2/main.go:
package main
import (
"fmt"
"net/http"
)
func main() {
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Hello from App 2")
})
http.ListenAndServe(":8081", nil)
}For app1, create /etc/systemd/system/app1.service:
[Unit]
Description=Golang App 1
[Service]
ExecStart=/usr/bin/go run /root/app1/main.go
Restart=always
User=root
Group=root
WorkingDirectory=/root/app1
[Install]
WantedBy=multi-user.targetActivate and initiate the service:
sudo systemctl enable app1.service
sudo systemctl start app1.serviceRepeat the process for app2.
After modifying a systemd service file, you need to reload the systemd daemon to apply the changes.:
sudo systemctl daemon-reload
sudo systemctl restart app1sudo apt install nginxModify /etc/nginx/sites-available/default:
server {
listen 80;
server_name api1.yourdomain.com;
location / {
proxy_pass http://localhost:8080;
}
}
server {
listen 80;
server_name api2.yourdomain.com;
location / {
proxy_pass http://localhost:8081;
}
}Validate and refresh Nginx:
sudo nginx -t
sudo systemctl reload nginxAdd a records, as shown in the table below
| Type | Name | Content |
|---|---|---|
| A | api1 | ip_address |
| A | api2 | ip_address |
Install Certbot:
sudo apt update && sudo apt install certbot python3-certbot-nginx.Obtain certificates:
sudo certbot --nginx -d api1.yourdomain.com
sudo certbot --nginx -d api2.yourdomain.comManual Renewal:
sudo certbot renewList all certificates:
sudo certbot certificatesTo view the logs for your specific systemctl (systemd) service:
journalctl -u app1.serviceFollow the Logs: Add -f to tail the logs (see new logs in real-time):
journalctl -fu app1.serviceShow Logs for the Current Boot: Use -b:
journalctl -u app1.service -bShow Logs Since a Specific Time: Use --since:
journalctl -u app1.service --since "2024-01-09 12:00:00"Show a Specific Number of Lines: Use -n followed by the number of lines:
journalctl -u app1.service -n 100