Skip to content

Latest commit

History

7 Commits

Folders and files

NameName
Last commit message
Last commit date

Repository files navigation

Multi-Engine Golang Applications with Nginx Proxy

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.

Prerequisites

  • Ubuntu Server
  • Domain Names (e.g., api1.yourdomain.com, api2.yourdomain.com) linked to your server's IP

1. Install Golang

sudo apt update
sudo apt install golang-go

2. Develop Two Golang Applications

For app1/main.go:

package main
import (
"fmt""net/http"
)
funcmain() {
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"
)
funcmain() {
http.HandleFunc("/", func(w http.ResponseWriter, r*http.Request) {
fmt.Fprintf(w, "Hello from App 2")
})
http.ListenAndServe(":8081", nil)
}

3. Systemd Service Configuration

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

4. Nginx Installation

sudo apt install nginx

5. Nginx Configuration

Modify /etc/nginx/sites-available/default:

server{listen80;server_name api1.yourdomain.com;location / {proxy_passhttp://localhost:8080;}}server{listen80;server_name api2.yourdomain.com;location / {proxy_passhttp://localhost:8081;}}

Validate and refresh Nginx:

sudo nginx -t
sudo systemctl reload nginx

6. Setting up DNS Records on Cloudflare

Add a records, as shown in the table below

TypeNameContent
Aapi1ip_address
Aapi2ip_address

7. Enabling HTTPS for your applications

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

8. Monitor the logs

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

About

Setting up dual Golang applications on a single Ubuntu server

Resources

Stars

0 stars

Watchers

1 watching

Forks

Releases

Packages

Contributors