如何实现全站的HTTPS访问(一)——SSL证书申请

1729
1

一步一步教你如何通过Certbot安装Let’s Encrypt 证书,来实现全站的HTTPS访问。

准备条件:

1、Linux服务器/VPS一台,Debian/Ubuntu系统环境。

2、要申请SSL证书的域名。

申请步骤如下:

一,安装snap

1、Debian/Ubuntu系统执行如下命令:

sudo su root
sudo apt update
sudo apt install snapd
sudo snap install core
sudo snap refresh core
sudo snap install hello-world

当你看到”hello-world 6.4 from Canonical✓ installed“类似字样,就表明你已经安装成功啦

2、Centos系统由于已经停止维护(CentOS 停止更新维护,天下没有免费且不散的宴席!),本文讲不再介绍如何安装过程,如有需要,请查看英文版安装文档:https://snapcraft.io/docs/installing-snapd

二、安装Cerbot

sudo snap install --classic certbot
sudo ln -s /snap/bin/certbot /usr/bin/certbot
sudo snap set certbot trust-plugin-with-root=ok

三、申请SSL证书

终于到了申请SSL证书的环节了,直接上命令:

certbot certonly \
  --manual \
  --register-unsafely-without-email \
  --preferred-challenges dns \
  -d example.com \
  -d *.example.com

其中”–preferred-challenges dns“验证方式的dns可以改为http,如果包含泛域名,则只能选择dns验证。

dns验证需要到域名解析商处,按要求添加或修改_acme-challenge的TXT记录。例如:


Please deploy a DNS TXT record under the name:

_acme-challenge.example.com.

with the following value:

3VfBqrTvAdj26Nrn-Uh76GUvQhnzIhQlQzaSvCsO73k

Before continuing, verify the TXT record has been deployed. Depending on the DNS
provider, this may take some time, from a few seconds to multiple minutes. You can
check if it has finished deploying with aid of online tools, such as the Google
Admin Toolbox: https://toolbox.googleapps.com/apps/dig/#TXT/_acme-challenge.example.com.
Look for one or more bolded line(s) below the line ‘;ANSWER’. It should show the
value(s) you’ve just added.


主机名为:_acme-challenge,记录类型为TXT,记录值为:3VfBqrTvAdj26Nrn-Uh76GUvQhnzIhQlQzaSvCsO73k,这个记录值需修改为命令显示的记录值。

验证方式为http验证时,需要按要求上传一个文件。


Create a file containing just this data:

0fHdliRtPP8QteIR_PFYJQMzwM0FH4YhlUOsaP2DK1o.Ev2Ke_1KIMk-gmM2vVpXyKWW73bmTauc8OYSTCjMorQ

And make it available on your web server at this URL:

http://example.com/.well-known/acme-challenge/0fHdliRtPP8QteIR_PFYJQMzwM0FH4YhlUOsaP2DK1o


即:网站根目录/.well-known/acme-challenge 下,创建一个文件名为0fHdliRtPP8QteIR_PFYJQMzwM0FH4YhlUOsaP2DK1o,内容为0fHdliRtPP8QteIR_PFYJQMzwM0FH4YhlUOsaP2DK1o.Ev2Ke_1KIMk-gmM2vVpXyKWW73bmTauc8OYSTCjMorQ的文件。

接下来命令会问你是否同意服务条款,我们选Y,然后回车


Please read the Terms of Service at
https://letsencrypt.org/documents/LE-SA-v1.3-September-21-2022.pdf. You must
agree in order to register with the ACME server. Do you agree?


(Y)es/(N)o: Y
Account registered.

验证成功后,将会显示SSL证书签发结果,和SSL证书的存储位置。

Successfully received certificate.
Certificate is saved at: /etc/letsencrypt/live/example.com/fullchain.pem
Key is saved at: /etc/letsencrypt/live/example.com/privkey.pem
This certificate expires on 2023-05-31.
These files will be updated when the certificate renews.
Certbot has set up a scheduled task to automatically renew this certificate in the background.


四、安装SSL证书

使用SSL证书安装到web服务器上就可以实现HTTPS访问啦。如果还有什么不懂的地方,欢迎评论区留言,或直接联系我们。

五、自动更新SSL证书

要续订(renew)Certbot的证书,您可以使用以下命令:

sudo certbot renew

运行此命令将自动检查所有已配置的证书,并续订30天内即将到期的证书。如果证书未到期,则不会执行任何操作。如果您在运行certbot renew时遇到任何问题,可以尝试添加--dry-run选项来进行模拟测试。这样,Certbot将模拟执行续订操作,但不会更改任何证书或配置文件。这有助于排除潜在的问题并进行调试。

要在Certbot中指定特定的域名进行续订,您可以使用以下命令:

sudo certbot renew --cert-name example.com

example.com替换为您要续订的实际域名。此命令将只续订名为example.com的证书,而不会影响其他证书。

请注意以下几点:

  1. 在运行certbot renew之前,请确保您已安装了最新版本的Certbot。
  2. 您可以同时指定多个域名,用逗号分隔。例如,--cert-name example.com,domain2.com
  3. 如果您不确定证书的名称,可以运行sudo certbot certificates命令查看您当前已经安装的证书列表及其名称。
  4. 确保您的域名在配置文件中正确地映射到相应的证书。您可以检查Certbot的配置目录(通常位于/etc/letsencrypt)中的相关文件,确保域名和证书名称匹配。

请根据您的具体情况和配置进行相应调整,并确保在执行续订操作之前进行适当的计划和测试,以避免中断服务。

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注

One thought on “如何实现全站的HTTPS访问(一)——SSL证书申请