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-go
For 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.target
Activate and initiate the service:
sudo systemctl enable app1.service
sudo systemctl start app1.service
Repeat 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 app1
sudo apt install nginx
Modify /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 nginx
Add 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.com
Manual Renewal:
sudo certbot renew
List all certificates:
sudo certbot certificates
To view the logs for your specific systemctl (systemd) service:
journalctl -u app1.service
Follow the Logs: Add -f to tail the logs (see new logs in real-time):
journalctl -fu app1.service
Show Logs for the Current Boot: Use -b:
journalctl -u app1.service -b
Show 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