How to Setup a Go Web Service as a System-Level Service on Ubuntu (Step-by-Step Guide)

Learn how to set up a Go web service as a system-level service on Ubuntu using systemd. This step-by-step guide helps you automate startup, improve reliability, and ensure smooth server deployment for production.

If you’ve built a Go (GoLang) web service and want it to run automatically on server startup, the best approach is to configure it as a system-level service using systemd. This ensures your Go application runs reliably, restarts automatically on failure, and integrates seamlessly with the Ubuntu server environment.

In this guide, we’ll walk you through every step from compiling your Go program to creating a systemd service file, so you can deploy your Go app like a professional.

Why Run Go as a System Service on Ubuntu?

Setting up a Go web application as a system-level service provides several production benefits:

  • Automatic startup on system boot
  • Automatic restart if your service crashes
  • Improved security by running under a non-root user
  • Easy monitoring via systemctl status and journalctl logs
  • Simplified management (start, stop, enable, disable)

Step 1: Build Your Go Web Application

First, make sure your Go web app is ready and compiled for Linux.

cd /path/to/your/app
go build -o myapp main.go

You’ll get a binary file named myapp. Move it to a system directory such as /usr/local/bin/ for easier access:

sudo mv myapp /usr/local/bin/

Step 2: Create a System User for Your App

For better security, run your service as a dedicated non-root user.

sudo useradd -r -s /bin/false myappuser

Step 3: Create a Systemd Service File

Now, create a new service configuration file under /etc/systemd/system/.

sudo nano /etc/systemd/system/myapp.service

Paste the following:

[Unit]
Description=My Go Web Service
After=network.target

[Service]
User=myappuser
ExecStart=/usr/local/bin/myapp
Restart=on-failure
Environment=PORT=8080
WorkingDirectory=/usr/local/bin

[Install]
WantedBy=multi-user.target

Save and exit (Ctrl + O, Ctrl + X).

Step 4: Reload Systemd and Start the Service

Run the following commands to enable your Go web service:

sudo systemctl daemon-reload
sudo systemctl start myapp
sudo systemctl enable myapp

Step 5: Verify Your Service

Check the status:

sudo systemctl status myapp

If everything is correct, you’ll see the service active (running).

To view logs:

sudo journalctl -u myapp -f

Step 6: Access Your Go Web Service

If your app listens on port 8080, open your browser and visit:

http://<your-server-ip>:8080

You should see your Go web service in action!

Tip: Manage Multiple Go Services

If you have multiple Go applications, create separate users and service files for each one. This isolation improves security and makes debugging easier.

Example:

  • /etc/systemd/system/authapi.service
  • /etc/systemd/system/paymentapi.service

Step 7: Secure Your Go Web Service (Optional)

For production environments:

  • Use Nginx as a reverse proxy for SSL termination
  • Configure firewall rules (UFW) to allow only required ports
  • Monitor uptime using Prometheus or Grafana

Example Nginx reverse proxy configuration:

server {
    listen 80;
    server_name example.com;

    location / {
        proxy_pass http://127.0.0.1:8080;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
    }
}

Then enable HTTPS with Certbot.

Conclusion

You’ve successfully configured a Go web service to run as a system-level service on Ubuntu. With this setup, your app runs automatically on boot, restarts on crash, and can be easily managed with systemctl commands.

Whether you’re deploying APIs, dashboards, or microservices, this method ensures reliability, security, and production-grade stability for your Go applications.

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply

Your email address will not be published. Required fields are marked *