Dan*_*e B 28 ssl https ssl-certificate go lets-encrypt
我有自己的域名,用Go编写的Web服务.我正在使用内置的Go Web服务器,前面没有Nginx或Apache.
我想开始通过HTTPS服务,我意识到Let's Encrypt即将成为这样做的方式.
任何人都可以共享整个设置过程来配置在Linux服务器上运行的Go应用程序吗?
Pyl*_*nux 58
这是使用我发现的Go和Let的加密证书的HTTPS服务器的最小自动设置:
package main
import (
"crypto/tls"
"log"
"net/http"
"golang.org/x/crypto/acme/autocert"
)
func main() {
certManager := autocert.Manager{
Prompt: autocert.AcceptTOS,
HostPolicy: autocert.HostWhitelist("example.com"), //Your domain here
Cache: autocert.DirCache("certs"), //Folder for storing certificates
}
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("Hello world"))
})
server := &http.Server{
Addr: ":https",
TLSConfig: &tls.Config{
GetCertificate: certManager.GetCertificate,
},
}
go http.ListenAndServe(":http", certManager.HTTPHandler(nil))
log.Fatal(server.ListenAndServeTLS("", "")) //Key and cert are coming from Let's Encrypt
}
Run Code Online (Sandbox Code Playgroud)
有关autocert包的更多信息:链接
编辑:由于letsencrypt安全问题需要提供http ,请在此处阅读更多内容.作为此修复的奖励我们现在有http - > https重定向.如果您已经收到证书,那么旧的示例将继续有效,但它将为新站点打破.
Dan*_*e B 27
我发现了一个非常简单的解决方案,使用独立模式.
安装CERTBOT客户端(由Let的加密推荐)
(go to the directory where you want to install the certbot client)
git clone https://github.com/certbot/certbot
cd certbot
./certbot-auto --help`
Run Code Online (Sandbox Code Playgroud)
问题证书(第一次)
注意,此操作通过端口80进行,因此,如果您的Go应用程序侦听端口80,则需要在运行此命令之前将其关闭(顺便说一下,这可以非常快速地运行)
./certbot-auto certonly --standalone-supported-challenges http-01 -d www.yourdomain.com
在您的GO CODE中添加SSL LISTENER
http.ListenAndServeTLS(":443", "/etc/letsencrypt/live/www.yourdomain.com/fullchain.pem", "/etc/letsencrypt/live/www.yourdomain.com/privkey.pem", nil)
更新证书(证书在90天后到期)
注意您可以手动运行(您将在证书过期前几天收到电子邮件),或者设置crontab
如果您的Go应用程序不再侦听端口80,则执行此命令时Go应用程序可以继续运行:
./certbot-auto renew --standalone
如果您的Go应用程序仍然侦听端口80,您可以指定停止并重新启动Go应用程序的命令:
./certbot-auto renew --standalone --pre-hook "command to stop Go app" --post-hook "command to start Go app"
有关Certbot命令的完整文档:https://certbot.eff.org/docs/using.html
归档时间: |
|
查看次数: |
12738 次 |
最近记录: |