如何在我的Heroku Node Express应用程序中使用LetsEncrypt SSL证书?

我有一个在Heroku上运行的Node Express应用程序,我想用来自LetsEncrypt的免费SSL证书进行encryption。 但是,我所看到的方法需要打开端口443和80,以使ACME进程正常工作。

Heroku只给你一个端口,不让你select哪个端口。 那么我怎样才能使用LetsEncrypt?

我昨天花了一大堆时间搞清楚了。 第一次很长一段时间,StackOverflow没有答案,我正在尝试做的事情!

更新:

Heroku现在支持本地LetsEncrypt! 所以这个解决方法不再需要。

说明:

https://devcenter.heroku.com/articles/automated-certificate-management

对于新的应用程序,您不需要执行任何操作,默认情况下它是打开的。 对于2017年3月21日之前创build的应用程序,您可以使用以下Heroku cli命令将其打开: heroku certs:auto:enable

谢谢@西class牙列车


背景

理想情况下,LetsEncrypt允许自动证书更新过程。 在Heroku上这很难做,所以这个答案描述了如何使用手动过程。 使用Heroku环境variables,你可以很容易地手动更新你的证书 – 不需要修改代码。

信用这个答案主要是两个很好的博客文章: https : //medium.com/@franxyzxyz/setting-up-free-https-with-heroku-ssl-and-lets-encrypt-80cf6eac108e#.67pjxutaw

https://medium.com/should-designers-code/how-to-set-up-ssl-with-lets-encrypt-on-heroku-for-free-266c185630db#.ldr9wrg2j

有一个GitHub项目,显然支持Heroku的自动化证书更新。 我试过的时候会更新这个答案:
https://github.com/dmathieu/sabayon

使用节点快速应用程序在Heroku上使用LetsEncrypt

准备好Express服务器:

将这个中间件添加到您的快速应用程序。 请务必在任何将httpredirect到https的中间件之前添加它,因为此端点必须是http。

 // Read the Certbot response from an environment variable; we'll set this later: const letsEncryptReponse = process.env.CERTBOT_RESPONSE; // Return the Let's Encrypt certbot response: app.get('/.well-known/acme-challenge/:content', function(req, res) { res.send(letsEncryptReponse); }); 

使用certbot创build证书文件:

  1. 启动certbot: sudo certbot certonly --manual
    出现提示时input网站url(www.example.com)
    certbot将以格式显示质询响应string
    xxxxxxxxxxxxxxxxxxx.yyyyyyyyyyyyyyyyyy
    在此状态下等待CERTBOT。 不要按input或退出。
  2. 转到Heroku仪表板并查看应用程序设置:
    https://dashboard.heroku.com/apps/your-heroku-app-name/settings
    在“configurationvariables”下,单击“显示configurationvariables”
    编辑CERTBOT_RESPONSE var的值以匹配来自步骤a的质询响应。
  3. 等待heroku应用程序重新启动。
  4. 通过访问http://www.example.com/.well-known/acme-challenge/whatever来testing设置
    注意HTTP,而不是HTTPS
    它应该显示质询响应string。 如果发生这种情况,请继续下一步。 如果不是,那么在继续操作之前尽一切努力使该URL返回CRstring,否则您将需要重复整个过程。
  5. 返回到Certbot并按Enter继续。
    如果一切按计划进行,certbot会告诉你一切正常,并显示创build的证书的位置。 您将在下一步中使用此位置。 请注意,由于操作系统权限,您可能无法检查文件夹的内容。 如果有疑问,请sudo ls /etc/letsencrypt/live/www.example.com查看文件是否存在。

更新Heroku实例以使用新的证书:

运行heroku certs:add如果您的站点没有heroku certs:add 。 如果更新,运行heroku certs:update
sudo heroku certs:update --app your-heroku-app-name /etc/letsencrypt/live/www.example.com/fullchain.pem /etc/letsencrypt/live/www.example.com/privkey.pem

您也可以validation您的域名所有权让我们用DNS而不是HTTPencryption。

使用certbot ,将DNS指定为您的首选挑战:

 sudo certbot certonly --manual --preferred-challenges dns 

在几次提示之后,certbot会告诉你一个DNS TXTlogging来validation你的域名:

 Please deploy a DNS TXT record under the name _acme-challenge.www.codesy.io with the following value: CxYdvM...5WvXR0 Once this is deployed, Press ENTER to continue 

您的域名注册商可能拥有自己的部署TXTlogging的文档。 做到这一点,回到certbot ,然后按ENTER键 – Let's Encrypt将检查TXTlogging,签署证书, certbot会将其保存为您上传到heroku。

看到我详细的博客文章了解更多 。