Ubuntu安装TLS(SSL)证书
HTTPS ,是以安全为目标的 HTTP 通道,在HTTP的基础上通过传输加密和身份认证
保证了传输过程的安全性 。HTTPS 在HTTP 的基础下加入SSL协议,HTTPS 的安全基础是 SSL,加密的详细内容需要 SSL,所以若想让您的网站支持HTTPS协议,就需要安装SSL证书,本片文章安装的是TLS证书,这里补充一点TLS相关的历史。TLS全称Transport Layer Security
,是用于在联网计算机之间建立经过身份验证和加密的链接的协议。其前身是SSL(Secure Sockets Layer),最初是由网景公司(Netscape)研发,于1999年被IETF(The Internet Engineering Task Force - 互联网工程任务组)标准化,定义为TLS 1.0 。目前最新版本为2018年发布的TLS 1.3,详情参见RFC 8446。
获取证书
这里我们去一个为 2.25 亿个网站提供 TLS 证书的非盈利性证书颁发机构”Let’s Encrypt”获取证书,从这个机构获取证书的方式有很多种,我们这里使用官方推荐的Certbot ACME 客户端来获取证书。
以下是Certbot ACME安装的详细步骤:
1. SSH 进入服务器
以具有 sudo 权限的用户身份通过 SSH 连接到运行您的 HTTP 网站的服务器。
2. 安装 snapd
apt install snapd
root@rock64:/usr/local/natapp# apt install snapd
使用如下命令检查是否安装成功
snap version
root@rock64:/usr/local/natapp# snap version
snap 2.55.5+18.04
snapd 2.55.5+18.04
series 16
ubuntu 18.04
kernel 4.4.190-1233-rockchip-ayufan-gd3f1be0ed310
3. 确保snapd 版本是最新的
sudo snap install core; sudo snap refresh core
root@rock64:/usr/local/natapp# snap install core
2022-08-03T18:17:40+08:00 INFO Waiting for automatic snapd restart...
core 16-2.56.2 from Canonical* installed
root@rock64:/usr/local/natapp# snap refresh core
snap "core" has no updates available
4. 删除 certbot-auto 和任何 Certbot 操作系统包
检查当前系统的包管理器中是否包含certbot包,如果有,则删除他们,常见的包管理器有apt、dnf、yum等,根据您的操作系统,执行您包管理器的删除certbot包的操作,当前系统使用的是Ubuntu,故执行以下命令
apt remove certbot
root@rock64:/usr/local/natapp# apt remove certbot
Reading package lists... Done
Building dependency tree
Reading state information... Done
Package 'certbot' is not installed, so not removed
0 upgraded, 0 newly installed, 0 to remove and 0 not upgraded.
如果您的操作系统是dnf或者yum等,执行的命令可能是:sudo dnf remove certbot或sudo yum remove certbot.
5. 安装 Certbot
在机器上的命令行上运行此命令以安装 Certbot。
snap install --classic certbot
root@rock64:/usr/local/natapp# snap install --classic certbot
certbot 1.29.0 from Certbot Project (certbot-eff**) installed
6. 准备 Certbot 命令
在机器上的命令行执行以下指令,保证certbot命令可以运行。
ln -s /snap/bin/certbot /usr/bin/certbot
root@rock64:/usr/local/natapp# ln -s /snap/bin/certbot /usr/bin/certbot
7. 选择您希望如何运行 Certbot
Certbot提供了两种方式来给您安装证书
-
单机模式
Certbot 将暂时在您的机器上启动一个网络服务器,部署一个web项目生成验证文件,生成之后,Certbot服务器会请求您的服务器的验证文件,校验通过后将给您颁发证书。
-
网络模式
Certbot 会利用既有的 web server,在其 web root目录下创建隐藏文件, Let’s Encrypt 服务端会通过域名来访问这些隐藏文件,以确认你的确拥有对应域名的控制权。
由于本人测试第二种模式屡次失败,故采用了第一种方式,网上查阅了很多资料,可能是我部署的web项目不在web root预期的目录,所以暂时放弃了第二种方式,当然第二种方式也有它的好处,就是不需要停止你的Web服务,不会占用你的80端口。
单机模式
执行单机模式需要注意以下几点:
- 您的服务器可以在公网被访问到,就是需要有自己的域名
- 您服务器上的80端口不能被占用
sudo certbot certonly --standalone
root@rock64:/home/rock64# sudo certbot certonly --standalone
Saving debug log to /var/log/letsencrypt/letsencrypt.log
Please enter the domain name(s) you would like on your certificate (comma and/or
space separated) (Enter 'c' to cancel): leeleo.cc
Requesting a certificate for leeleo.cc
Successfully received certificate.
Certificate is saved at: /etc/letsencrypt/live/leeleo.cc/fullchain.pem
Key is saved at: /etc/letsencrypt/live/leeleo.cc/privkey.pem
This certificate expires on 2022-11-01.
These files will be updated when the certificate renews.
Certbot has set up a scheduled task to automatically renew this certificate in the background.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
If you like Certbot, please consider supporting our work by:
* Donating to ISRG / Let's Encrypt: https://letsencrypt.org/donate
* Donating to EFF: https://eff.org/donate-le
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
执行到上一步,我们的证书就已经安装成功了
8. 测试自动续订
您系统上的 Certbot 软件包带有一个 cron 作业或 systemd 计时器,可在证书到期之前自动更新您的证书。除非您更改配置,否则您无需再次运行 Certbot。您可以通过运行以下命令来测试证书的自动续订:
sudo certbot renew --dry-run
root@rock64:/home/rock64# certbot renew --dry-run
Saving debug log to /var/log/letsencrypt/letsencrypt.log
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Processing /etc/letsencrypt/renewal/leeleo.cc.conf
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Account registered.
Simulating renewal of an existing certificate for leeleo.cc
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Congratulations, all simulated renewals succeeded:
/etc/letsencrypt/live/leeleo.cc/fullchain.pem (success)
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
更新 certbot 的命令安装在以下位置之一:
/etc/crontab//etc/cron.*/*systemctl list-timers
9. 配置Nginx
# 强制重定向
server {
listen 80; # 监听80端口
server_name leeleo.cc; # 自己的域名
#把http的域名请求转成https
return 301 https://$host$request_uri;
}
server {
listen 443 ssl;
server_name leeleo.cc; # 自己的域名
# 配置 ssl
ssl_certificate /etc/letsencrypt/live/leeleo.cc/fullchain.pem; # pem文件的路径
ssl_certificate_key /etc/letsencrypt/live/leeleo.cc/privkey.pem; # key文件的路径
ssl_session_timeout 5m;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE;
ssl_prefer_server_ciphers on;
client_max_body_size 1024m;
location / {
proxy_set_header HOST $host;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://192.168.31.22:8090/; # 这里改为自己服务器ip
}
}
10. 确认下Certbot是否成功
要确认您的网站设置正确,请https://leeleo.cc/在浏览器中访问并在 URL 栏中查找锁定图标。