如何在 Heroku 上安装 LetsEncrypt SSL 证书

How do you install a LetsEncrypt SSL Certificate on Heroku

由于 Heroku 是只读的并且不允许 sudo,我需要做什么才能在他们的服务器上为我的应用程序安装 LetsEncrypt.org 证书?

如果我已经设置了 config.force_ssl = true 有关系吗?

Heroku 的 default recommendation 是使用服务器名称指示的 SSL (SNI),它是免费的。由于您已经获得证书和密钥,您可以通过以下方式添加它们:

heroku certs:add <cert>.pem <key>.key

如果您需要支持不支持 SNI 的旧版浏览器客户端,请使用 Herkou SSL Endpoint 插件,每月收费 20 美元:

通过 运行

添加该插件
heroku addons:create ssl:endpoint

然后添加您的 LetsEncrypt.org 证书:

heroku certs:add <cert>.pem <key>.key

最好的方法是将新的 ssl 域(以 https 开头)分配给您的域,这会自动覆盖非 http 域

编辑:此答案不再适用。

它是在 Heroku 实现对 LetsEncrypt 的本地支持之前编写的。剩下的留给 posterity,但这不再是必要的。现在使用


正在安装初始证书

您可以在手动模式下使用 certbot 生成质询响应,将您的站点修改为 return 该响应,然后最终完成 certbot 手动过程。

See this blog post by Daniel Morrison,或下方证书更新下的链接答案,了解更多详情。

证书更新

正如@Flimm 提到的,以及链接博客 post 中提到的,您必须每 3 个月更新一次,直到 Heroku 为 LetsEncrypt 提供更好的支持。您可以使用此答案中描述的环境变量使该过程更顺畅(无需上传代码更改)(Node/Express 但概念相同):

萨巴永

有一个 GitHub 项目可以通过设置 Heroku 环境变量为您自动执行所有这些操作。这是一个微型 Web 应用程序,您可以将其作为另一个 Heroku 应用程序安装,然后再配置您的主要应用程序。我还没有尝试过,但我打算使用它而不是下次更新我的证书:https://github.com/dmathieu/sabayon

我在此处的第一个答案中阅读了博客 post,但我不想用 ACME URL 和逻辑污染我的代码库。所以我做了类似的事情,但使用了 DNS 域验证 ...

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

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

经过几次提示后,certbot 会告诉您部署 DNS TXT 记录以验证您的域:

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

您的域注册商可能有自己的文档来部署 TXT 记录。这样做,然后返回 certbot 并按 ENTER - Let's Encrypt 将检查 TXT 记录,签署证书,certbot 将保存它以供您上传到 heroku。

See my own blog post for more detail.


这里有两个 bash 函数,您可以使用它们 自动执行 为您完成的过程

function makessl {
    sudo certbot certonly --manual --rsa-key-size 4096 --preferred-challenges dns -d 
    sudo heroku certs:add --type=sni /etc/letsencrypt/live//fullchain.pem /etc/letsencrypt/live//privkey.pem
}

function renewssl {
    sudo certbot certonly --manual --rsa-key-size 4096 --preferred-challenges dns -d 
    sudo heroku certs:update /etc/letsencrypt/live//fullchain.pem /etc/letsencrypt/live//privkey.pem
}

They take an arguement for the domain name and as long as you run them from within your heroku app folder you will not have to specify an --app NAME

Example: makessl www.domain.com

Example: renewssl www.domain.com


结合这是 @Eric 的答案,你就可以开始了:

heroku certs:auto:enable

我创建了一个 certbot 插件,它使用 Heroku CLI 来自动验证和安装 Let's Encrypt 证书:https://github.com/gboudreau/certbot-heroku

我只有一个使用 php-nginx Heroku buildpack 的示例,但阅读该示例并找到其他 buildpack 的等效项应该很容易。 欢迎提出请求以帮助他人!

仅供参考,如果您 运行 付费测功机,Heroku 现在提供带 Let's Encrypt 的自动证书管理。您可以通过以下方式启用它:

heroku certs:auto:enable

更多信息:

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