Create a HTTPS Server
suggest changeGenerate a certificate
In order to run a HTTPS server, a certificate is necessary. Generating a self-signed certificate with openssl is done by executing this command:
openssl req -x509 -newkey rsa:4096 -sha256 -nodes -keyout key.pem -out cert.pem -subj "/CN=example.com" -days 3650`
The parameters are:
reqUse the certificate request toolx509Creates a self-signed certificatenewkey rsa:4096Creates a new key and certificate by using the RSA algorithms with4096bit key lengthsha256Forces the SHA256 hashing algorithms which major browsers consider as secure (at the year 2017)nodesDisables the password protection for the private key. Without this parameter, your server had to ask you for the password each time its starts.keyoutNames the file where to write the keyoutNames the file where to write the certificatesubjDefines the domain name for which this certificate is validdaysFow how many days should this certificate valid?3650are approx. 10 years.
Note: A self-signed certificate could be used e.g. for internal projects, debugging, testing, etc. Any browser out there will mention, that this certificate is not safe. In order to avoid this, the certificate must signed by a certification authority. Mostly, this is not available for free. One exception is the “Let’s Encrypt” movement: https://letsencrypt.org
The necessary Go code
You can handle configure TLS for the server with the following code. cert.pem and key.pem are your SSL certificate and key, which where generated with the above command.
package main
import (
"log"
"net/http"
)
func main() {
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("Hello, world!"))
})
log.Fatal(http.ListenAndServeTLS(":443","cert.pem","key.pem", nil))
}
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents