生成证书

首先我们需要安装 nginx

我们使用 Letsencrypt的 letsencrypt-auto脚本 现在centos7已经不支持了

1
2
3
4
5
6
7
8
9
10
11
# english
Skipping bootstrap because certbot-auto is deprecated on this system.
Your system is not supported by certbot-auto anymore.
Certbot cannot be installed.
Please visit https://certbot.eff.org/ to check for other alternatives.

# chinese
跳过引导程序,因为在此系统上不推荐使用 certbot-auto。
certbot-auto 不再支持您的系统。
无法安装 Certbot。
请访问 https://certbot.eff.org/ 查看其他替代方案。

certbot-auto不再支持所有的操作系统!根据作者的说法,certbot团队认为维护certbot-auto在几乎所有流行的UNIX系统以及各种环境上的正常运行是一项繁重的工作,加之certbot-auto是基于python 2编写的,而python 2即将寿终正寝,将certbot-auto迁移至python 3需要大量工作,这非常困难,因此团队决定放弃certbot-auto的维护。
既然如此,现在我们还能继续使用certbot吗?certbot团队使用了基于snap的新的分发方法。

我的环境 CentOS 7

安装letsencrypt but 在安装letsencrypt之前,需要先安装epelsnaps

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# 安装epel
yum install -y epel-release

# 安装 snaps
yum install -y snapd

#启用snapd.socket。
systemctl enable --now snapd.socket

#创建/var/lib/snapd/snap和/snap之间的链接
ln -s /var/lib/snapd/snap /snap

#将snap更新至最新版本
snap install core
snap refresh core

卸载已安装的certbot

1
yum remove certbot

通过snap安装certbot

1
snap install --classic certbot

创建/snap/bin/certbot的软链接,方便certbot命令的使用

1
ln -s /snap/bin/certbot /usr/bin/certbot

letsencrypt的使用 获取证书。

确保nginx处于运行状态,需要获取证书的站点在80端口,并且可以正常访问。

生成证书

1
certbot certonly --nginx --email 你的邮箱 -d 你的域名 -d 你的域名

配置Nginx文件

在上面的生成证书执行完毕之后会打印证书文件路径 复制这两条路径在nginx文件中配置

1
2
/etc/letsencrypt/live/你的域名/fullchain.pem
/etc/letsencrypt/live/你的域名/privkey.pem

我的nginx配置文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#user  nobody;
worker_processes 1;

#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;

#pid logs/nginx.pid;

events {
worker_connections 1024;
}

http {
include mime.types;
default_type application/octet-stream;

#log_format main '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"';

#access_log logs/access.log main;

sendfile on;
#tcp_nopush on;

#keepalive_timeout 0;
keepalive_timeout 65;

#gzip on;

server {
listen 443 ssl;
server_name 你的域名 www.你的域名;
#charset koi8-r;
#access_log logs/host.access.log main;
ssl_certificate /etc/letsencrypt/live/www.ryder.pw/fullchain.pem; # pem文件的路径
ssl_certificate_key /etc/letsencrypt/live/www.ryder.pw/privkey.pem; # key文件的路径

# ssl验证相关配置
#缓存有效期
ssl_session_timeout 5m;
#加密算法
ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
#安全链接可选的加密协议
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
#使用服务器端的首选算法
ssl_prefer_server_ciphers on;

location / {
root html;
index index.html index.htm;
}

#error_page 404 /404.html;

# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}

server {
listen 80;
server_name ryder.pw www.ryder.pw;
location / {
#强制https请求
rewrite ^(.*)$ https://$host$1 permanent;
}
}
}