DEV Community

Cover image for How to Install an SSL Certificate on an Ubuntu Server Running a Go Application
Judy Page
Judy Page

Posted on

How to Install an SSL Certificate on an Ubuntu Server Running a Go Application

If you are looking to secure your go application with an SSL certificate to protect your sensitive data and ensure encrypted communication to build user trust.

Without having an SSL certificate your application is vulnerable to cyber attacks, data interception and third-party unauthorized access.

In this article, we will take you through the step-by-step process of installing an SSL certificate on the Ubuntu server for a Go application.
Specifically, we will focus on using a certificate from Porkbun, a reliable domain and SSL provider, to enhance your application's security and encryption.

By following these steps you can guarantee a safer and more secure online experience for your users.

How to install an SSL certificate on an Ubuntu server running on a Go application

Step 1:
1: Log in to your Porkbun account and locate the SSL certificate section
2: Now you have to download the SSL certificate bundle which includes:
private.key.pem(private key)
Public.key.pem (public key)
domain.cert.pem(certificate)

Step 2: Upload the SSL files to the server
Transfer your SSL files to your Ubuntu server using SCP or SFTP

scp private.key.pem public.key.pem domain.cert.pem user@your-server:/home/user/

Enter fullscreen mode Exit fullscreen mode

Move them to a secure location:

sudo mkdir -p /etc/ssl/api.example.xyz/
sudo mv private.key.pem /etc/ssl/api.example.xyz/
sudo mv public.key.pem /etc/ssl/api.example.xyz/
sudo mv domain.cert.pem /etc/ssl/api.example.xyz/
Enter fullscreen mode Exit fullscreen mode

Set proper permissions:

sudo chmod 600 /etc/ssl/api.example.xyz/*
sudo chown root: root /etc/ssl/api.example.xyz/*

Enter fullscreen mode Exit fullscreen mode

Step 3. Configure the Go application for HTTPS

Since our Go application runs on its own without Apache or Nginx, we need to set up TLS directly in the Go code.

package main

import (
"log"
"net/http"
)
func main() {
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
 w.Write([]byte("Hello, Secure API!"))
})
err := http.ListenAndServeTLS(":443",
 "/etc/ssl/api.example.xyz/domain.cert.pem",
 "/etc/ssl/api.example.xyz/private.key.pem",
 nil)
if err != nil {
 log.Fatal(err)
}
}
Enter fullscreen mode Exit fullscreen mode

Step 4: Allow HTTPS in Firewall
If you’re using ufw (Uncomplicated Firewall), allow traffic on port 443 (HTTPS):

sudo ufw allow 443/tcp
sudo ufw reload
Enter fullscreen mode Exit fullscreen mode

Step 5: Restart the Go Application
Run the Go application again to apply the changes:

go run main.go

Enter fullscreen mode Exit fullscreen mode

Step 6: Testing your SSL Installation
You can test your SSL installation using curl:

curl -v https://api.example.xyz 

Enter fullscreen mode Exit fullscreen mode

Alternatively, you can use SSL installation checker tool to verify the SSL certificate is correctly installed.

Now your Go application is secured with SSL

Speedy emails, satisfied customers

Postmark Image

Are delayed transactional emails costing you user satisfaction? Postmark delivers your emails almost instantly, keeping your customers happy and connected.

Sign up

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay