gpt4 book ai didi

apache - 我需要使用 nginx 或 Apache 才能使用 Lets Encrypt 吗?

转载 作者:IT王子 更新时间:2023-10-29 00:43:53 24 4
gpt4 key购买 nike

我有一个非常简单的基于 golang 的 API,它只监听路径并相应地响应数据库插入。

我想使用 Lets Encrypt 通过 TLS/https 提供服务,但所有教程似乎都表明需要使用 Apache 或 nginx。

我喜欢让我的服务器保持真正的轻量级并且没有看到任何需要引入这些网络服务器的开销(它绝对不是一个完整的网站)并且通过 http 我的 go 实现运行良好。

没有Apache或者nginx可以安装吗?

最佳答案

不,您不需要使用 Apache/Nginx,Go 可以很好地处理 TLS。

检查 http.ListenAndServeTLS

例子:

➜ sudo letsencrypt certonly --standalone --agree-tos --email you@email.com -d domain1.com [-d domain2.com, etc..]
➜ sudo cat /etc/letsencrypt/archive/domain1.com/fullchain1.pem > cert.pem
➜ sudo cat /etc/letsencrypt/archive/domain1.com/privkey1.pem > key.pem

➜ cat main.go
import (
"log"
"net/http"
)

func handler(w http.ResponseWriter, req *http.Request) {
w.Header().Set("Content-Type", "text/plain")
w.Write([]byte("This is an example server.\n"))
}

func main() {
http.HandleFunc("/", handler)
log.Printf("About to listen on 10443. Go to https://domain1.com:10443/")
log.Fatal(http.ListenAndServeTLS(":10443", "cert.pem", "key.pem", nil))
}

请注意,如果您希望您的 go 服务器监听端口 443(默认 https 端口),您必须以 root 身份运行它或使用 systemd 将端口传递给它。

443端口运行示例:

  1. 将代码更改为 log.Fatal(http.ListenAndServeTLS(":443", "cert.pem", "key.pem", nil))

    /li>
  2. 开始构建

  3. sudo ./your-package-name
  4. 或者,如果您不想以 root 身份运行,则必须以您的用户身份 chown cert.pem/key.pem 然后运行 ​​setcap cap_net_bind_service=+ep your-package-name 然后您将能够以用户身份收听端口 443/80。

有关使用 setcap 的更多详细信息:https://wiki.apache.org/httpd/NonRootPortBinding

关于apache - 我需要使用 nginx 或 Apache 才能使用 Lets Encrypt 吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36873304/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com