dongyin4202 2016-04-26 18:46
浏览 25

我是否需要使用nginx或Apache才能使用Lets Encrypt?

I have a really simple API based on golang that just listens at a path and responds accordingly with a database insert.

I want to serve it over TLS/https using Lets Encrypt, but all the tutorials seem to indicate use of Apache or nginx as a requirement.

I like keeping my server really light and haven't seen any need to introduce the overhead of these web servers (it's definitely not a full fledged website) and over http my go implementation works well.

Is it possible to install it without Apache or nginx?

  • 写回答

1条回答 默认 最新

  • dongxu8533486 2016-04-26 18:51
    关注

    No, you do not need to use Apache/Nginx, Go handles TLS just fine.

    Check http.ListenAndServeTLS

    Example:

    ➜ 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.
    "))
    }
    
    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))
    }
    

    Note that if you want your go server to listen on port 443 (default https port), you will either have to run it as root or use systemd to pass it the port.

    Example of running on port 443:

    1. Change the code to log.Fatal(http.ListenAndServeTLS(":443", "cert.pem", "key.pem", nil))

    2. go build

    3. sudo ./your-package-name
    4. or if you don't want to run as root, you will have to chown cert.pem/key.pem as your user then run setcap cap_net_bind_service=+ep your-package-name then you will be able to listen on port 443/80 as a user.

    More details about using setcap: https://wiki.apache.org/httpd/NonRootPortBinding

    评论

报告相同问题?

悬赏问题

  • ¥15 touchsocket udp组播
  • ¥20 MAC怎么安装Silverlight 插件?以及安装了怎么启用
  • ¥15 VS2012中查询语句无法填入解析,数值传不进去
  • ¥15 gis系统开发出现命名空间“ESRI.ArcGIS”中不存在类型或命名空间名“Analyst3D”报错
  • ¥15 怎么让ai定时给我发信息 c#或者python
  • ¥15 scrapy的Error
  • ¥15 RBF-VSG姚凤军论文复现问题
  • ¥30 开发一个APP商城在制作tabbar的时候运行不了代码没有检查出错误,但是显示不出tabbar,以下为运行结果,如何解决?
  • ¥15 多网卡服务器中winform如何绑定指定网卡
  • ¥15 关于#python#pandas#的问题,想要实现:多个TXT导入Excel,进行分列,不同txt之间都从第一行开始,请各位专家解答!