HTTPS for Golang API


I am new to Golang and I did set up a "hello world!" test message for a Golang API on our VPS. It works just fine at I would like however to move to HTTPS.

Could someone tell me step by step the right procedure to go from HTTP to HTTPS for a golang API? Thank you!

In case there is an issue with the golang code:

package main

import (

func main() {
        http.HandleFunc("/hello", func(w http.ResponseWriter, r *http.Request) {
                fmt.Fprintf(w, "Hello, World")

        fmt.Println("Server Started On Port 8080")
        log.Fatal(http.ListenAndServe(":8080", nil))


Thanks to John Hanley for support that lead to this answer.
First of all I did set the port 8443 for https by editing /etc/apache2/ports.conf:

Listen 80

<IfModule ssl_module>
        Listen 443
        Listen 8443

Then I added a VirtualHost in the config of the domain so that port 8443 acts as a proxy:

<VirtualHost *:8443>

        ProxyRequests Off
        <Proxy *>
                Order deny,allow
                Allow from all
        ProxyPass / http://localhost:8080/
        ProxyPassReverse / http://localhost:8080/

        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined

       Include /etc/letsencrypt/options-ssl-apache.conf
       SSLCertificateFile /etc/letsencrypt/live/
       SSLCertificateKeyFile /etc/letsencrypt/live/

and you need to load the modules proxy and proxy_http using e2enmod proxy and e2enmod proxy_http.
After reloading apache, the API can be called at

Answered By – Filippo

Answer Checked By – Terry (GoLangFix Volunteer)

Leave a Reply

Your email address will not be published.