Deploy Golang Application on Ubuntu 24.04

By Raman Kumar

Updated on Sep 03, 2024

In this tutorial, we'll explain how to deploy Golang application on Ubuntu 24.04 server with Nginx and install SSL certificate. 

Golang, also known as Go, is a statically typed, compiled programming language designed by Google. It was created by Robert Griesemer, Rob Pike, and Ken Thompson and was first released in 2009. Go was developed to address the shortcomings of existing languages in terms of performance, scalability, and ease of use, particularly in the context of modern, multi-core processors and large-scale distributed systems.

One of Go's standout features is its simplicity, which makes it easy to learn and use. The language has a clean syntax, with a focus on readability and minimalism, enabling developers to write clear and maintainable code. Go is also known for its strong concurrency support, which is facilitated by goroutines—lightweight, managed threads that allow for efficient multitasking within applications. This makes Go particularly well-suited for building networked and distributed systems, such as web servers, microservices, and cloud-native applications.

This tutorial will guide you through deploying a Go (Golang) application on an Ubuntu server. We’ll cover creating a simple Go application, compiling it, and setting it up as a service on Ubuntu.

Prerequisites

  • Ubuntu 24.04 installed dedicated server or KVM VPS with root or sudo privileges.
  • Basic knowledge of the terminal and SSH access to your server.
  • A domain name with pointing A record to the server.

Deploy Golang Application on Ubuntu

Step 1: Install Go on the Ubuntu Server

 If Go is not already installed on your Ubuntu server, follow these steps to install it:

Update the package list:

sudo apt update

Install Go using the package manager:

sudo apt install golang -y

Verify the installation:

go version

Output:

go version go1.22.2 linux/amd64

This should display the installed version of Go.

Step 2: Create a Simple Go Application

On your local machine or on Ubuntu server, create a simple Go application that will be deployed on the Ubuntu server.

Create a project directory:

mkdir ~/go-deploy
cd ~/go-deploy

Create a Go file (e.g., main.go):

nano main.go

Add following code:

package main

import (
    "fmt"
    "net/http"
)

func handler(w http.ResponseWriter, r *http.Request) {
    fmt.Fprintf(w, "Hello, World! Welcome to your Go application!")
}

func main() {
    http.HandleFunc("/", handler)
    fmt.Println("Server is running on port 8080...")
    http.ListenAndServe(":8080", nil)
}

Save and exit the file.

This is a basic web server that listens on port 8080 and responds with "Hello, World!" to any HTTP request.

Step 3: Build the Go Application

Next, build the Go application for your Ubuntu server.

Compile the Go application. Execute following set of commands:

go mod init myapp
go mod tidy
GOOS=linux GOARCH=amd64 go build -o myapp

These commands creates an executable named myapp that is compatible with Linux (amd64 architecture).

Verify the executable:

ls -l myapp

You should see an executable file named myapp in your project directory.

Step 4: Transfer the Application to the Ubuntu Server

If you have built Go application in your local machine follow this step or you can skip it.

Now that the application is built, transfer the executable to your Ubuntu server.

Use scp to copy the executable:

scp myapp user@your_server_ip:/home/user/

Replace user and your_server_ip with your server’s username and IP address.

SSH into your Ubuntu server:

ssh user@your_server_ip

Step 5: Set Up the Go Application as a Systemd Service

First, move the myapp file to /usr/local/bin

sudo mv myapp /usr/local/bin/

To ensure that your application runs automatically and restarts after a reboot, set it up as a systemd service.

Create a systemd service file:

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

Add the following configuration:

[Unit]
Description=My Go Application
After=network.target

[Service]
ExecStart=/usr/local/bin/myapp
Restart=on-failure
User=nobody
Group=nogroup

[Install]
WantedBy=multi-user.target

Save and close the file.

Reload the systemd daemon to apply the changes:

sudo systemctl daemon-reload

Start the Go application service:

sudo systemctl start myapp

Enable the service to start on boot:

sudo systemctl enable myapp

Check the status of the service:

sudo systemctl status myapp

You should see that the service is running.

Step 6: Install and Configure Nginx on Ubuntu

To secure your Go application with SSL, you can set up Nginx as a reverse proxy and install an SSL certificate. This tutorial will guide you through configuring Nginx and obtaining a free SSL certificate using Let’s Encrypt.

Install Nginx:

sudo apt install nginx -y

Start and enable Nginx:

sudo systemctl start nginx
sudo systemctl enable nginx

Allow Nginx through the firewall:

sudo ufw allow 'Nginx Full'

Configure Nginx as a Reverse Proxy for Your Go Application

Create a new Nginx server block:

sudo nano /etc/nginx/sites-available/myapp

Add the following configuration:

server {
    listen 80;
  server_name your_domain;

    location / {
        proxy_pass http://localhost:8080;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
    }
}

Replace your_domain with your domain name or server’s public IP address.

Enable the server block by creating a symbolic link:

sudo ln -s /etc/nginx/sites-available/myapp /etc/nginx/sites-enabled/

Test the Nginx configuration:

sudo nginx -t

If the test is successful, you should see a message that the configuration is OK.

Reload Nginx to apply the changes:

sudo systemctl reload nginx

Step 7: Install Certbot and Obtain an SSL Certificate

Install Certbot and the Nginx plugin:

sudo apt install certbot python3-certbot-nginx -y

Obtain an SSL certificate:

sudo certbot --nginx -d your_domain

Replace your_domain with your actual domain name or server IP address.

During this process, Certbot will automatically configure your Nginx server block to use the SSL certificate. It will also set up automatic renewal for the certificate.

Step 8: Access your application via HTTPS

Navigate to https://your_domain in your web browser. You should see the same "Hello, World!" message from your Go application, now secured with SSL.

Conclusion

We’ve seen how to deploy Golang application on Ubuntu 24.04 server with Nginx and install SSL certificate and set it up as a service using systemd. This setup ensures that your application runs continuously and restarts automatically if the server reboots. We've configured Nginx as a reverse proxy for your Go application and secured it with an SSL certificate using Let’s Encrypt. Your Go application is now accessible over HTTPS, providing encrypted communication for users. This setup not only enhances security but also ensures your application is production-ready.

You can now build more complex applications and deploy them using the same process.