Deploy Golang Application on AlmaLinux 9

By Raman Kumar

Updated on Sep 04, 2024

In this tutorial, we'll explain how to deploy Golang application on AlmaLinux 9 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 AlmaLinux server. We’ll cover creating a simple Go application, compiling it, and setting it up as a service on AlmaLinux.

Prerequisites

  • AlmaLinux 9 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 AlmaLinux

Step 1: Set Up the AlmaLinux Server

Update the system packages:

Before we begin, ensure that your system is up-to-date:

sudo dnf update -y

Install Git:

We'll use Git to manage our Go application code. Install Git with the following command:

sudo dnf install git -y

Install Go:

Install Go on the AlmaLinux server by downloading the binary distribution.

sudo dnf install golang -y

Verify the installation:

go version

Output:

go version go1.21.11 (Red Hat 1.21.11-1.el9_4) linux/amd64

This should display the installed version of Go.

Step 2: Create a Simple Go Application

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

Create a project directory:

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

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

vi main.go

Add following code:

package main

import (
    "fmt"
    "net/http"
)

func handler(w http.ResponseWriter, r *http.Request) {
    fmt.Fprintf(w, "Hello, AlmaLinux!")
}

func main() {
    http.HandleFunc("/", handler)
    fmt.Println("Starting server on :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, AlmaLinux" to any HTTP request.

Step 3: Build the Go Application

Next, build the Go application for your AlmaLinux 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 AlmaLinux 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 AlmaLinux 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 AlmaLinux 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

Caution: We have used root user for this service file. We had issue with normal user with sudo rights. Please use normal user and check if you get any issue.

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 AlmaLinux

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 dnf install nginx -y

Start and enable Nginx:

sudo systemctl start nginx
sudo systemctl enable nginx

Allow Nginx through the firewall:

sudo firewall-cmd --add-port=80/tcp --permanent
sudo firewall-cmd --add-port=443/tcp --permanent
sudo firewall-cmd --reload

Configure Nginx as a Reverse Proxy for Your Go Application

Create a new Nginx server block:

sudo vi /etc/nginx/conf.d/myapp.conf

Add the following configuration:

server {
    listen 80;
    server_name your-domain.com;

    location / {
        proxy_pass http://127.0.0.1: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.com with your domain name or server’s public IP address.

 

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 dnf 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: Configure SELinux

If you have enabled SELinux, follow this step.

Allow Nginx to Connect to the Network:

Use the following command to allow Nginx to establish outbound connections:

sudo setsebool -P httpd_can_network_connect 1

This command changes the SELinux policy to allow the httpd service (which includes Nginx) to connect to network resources.

Step 9: 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 AlmaLinux 9 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.