安装Certbot

1、下载安装文件

 wget https://dl.eff.org/certbot-auto

chmod a+x ./certbot-auto

2、初始化

./certbot-auto

3、获取证书

因为目前大多数国内的DNS服务商不在API支持的列表里,所以以下使用手动方式进行DNS认证,只要将下方命令中的*.minirplus.com替换为自己的域名即可

1
./certbot-auto certonly --manual -d *.minirplus.com --agree-tos --no-bootstrap --manual-public-ip-logging-ok --preferred-challenges dns-01 --server https://acme-v02.api.letsencrypt.org/directory

注意!域名的 minirplus.com 解析记录必须以 A记录 方式指向当前运行命令的服务器IP,而不能使用CNAME记录。否则会报错,报错信息如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
IMPORTANT NOTES:
 - The following errors were reported by the server:
 
   Domain: minirplus.com
   Type:   connection
   Detail: DNS problem: SERVFAIL looking up TXT for
   _acme-challenge.minirplus.com
 
   To fix these errors, please make sure that your domain name was
   entered correctly and the DNS A/AAAA record(s) for that domain
   contain(s) the right IP address. Additionally, please check that
   your computer has a publicly routable IP address and that no
   firewalls are preventing the server from communicating with the
   client. If you're using the webroot plugin, you should also verify
   that you are serving files from the webroot path you provided.

运行该命令后,会要求 输入邮箱,用于接收证书过期通知

1
2
Enter email address (used for urgent renewal and security notices) (Enter 'c' to
cancel):

接着会出现一段广告,大意是收集客户邮箱给赞助商,Y 或 N 均可

1
2
3
4
5
6
7
-------------------------------------------------------------------------------
Would you be willing to share your email address with the Electronic Frontier
Foundation, a founding partner of the Let's Encrypt project and the non-profit
organization that develops Certbot? We'd like to send you email about EFF and
our work to encrypt the web, protect its users and defend digital rights.
-------------------------------------------------------------------------------
(Y)es/(N)o:

接着重要的部分来了,在DNS记录中添加一个 _acme-challenge 前缀的域名 TXT记录,记录的内容为中间显示的随机码xVloe7V1kMEd2ZlOLlUxv-HltYfTDaMhrrwKjFU47DU

1
2
3
4
5
6
7
8
9
-------------------------------------------------------------------------------
Please deploy a DNS TXT record under the name
_acme-challenge.minirplus.com with the following value:
 
xVloe7V1kMEd2ZlOLlUxv-HltYfTDaMhrrwKjFU47DU
 
Before continuing, verify the record is deployed.
-------------------------------------------------------------------------------
Press Enter to Continue

接着确保当前域名的根记录 minirplus.com 为 A记录 并且指向当前服务器IP(这条原本不成问题,因为国外的服务商的DNS根域名只能添加A记录,但是国内的DNSPOD则更加灵活,可以添加CNAME记录,所以会在认证的时候出现问题)

按回车,进行认证

等待片刻,出现如下信息,说明认证成功

申请操作成功后, 会在界面中输出证书的存放路径, 以及证书的到期时间 (90天)

1
2
3
4
5
6
7
8
9
10
11
12
13
IMPORTANT NOTES:
 - Congratulations! Your certificate and chain have been saved at:
   /etc/letsencrypt/live/minirplus.com/fullchain.pem
   Your key file has been saved at:
   /etc/letsencrypt/live/minirplus.com/privkey.pem
   Your cert will expire on 2018-06-19. To obtain a new or tweaked
   version of this certificate in the future, simply run certbot-auto
   again. To non-interactively renew *all* of your certificates, run
   "certbot-auto renew"
 - 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

4、证书的存放路径

这里以example.com为例 )

生成证书中会创建 /etc/letsencrypt 文件夹, 证书文件默认存放在 /etc/letsencrypt/live/example.com 文件夹中, 其中 example.com 取自第一个域名
在 example.com 文件夹中包含 4 个文件 ./cert.pem ./chain.pem ./fullchain.pem ./privkey.pem

  • cert.pem 域名证书
  • chain.pem 根证书及中间证书
  • fullchain.pem 由 cert.pem 和 chain.pem 合并而成
  • privkey.pem 证书私钥

创建一个 2048 位的 Diffie-Hellman 文件\
(nginx 默认使用 1024 位的 Diffie–Hellman 进行密钥交换, 安全性太低)

1
openssl dhparam -out /etc/letsencrypt/live/dhparams.pem 2048

Nginx https 相关配置

这里以example.com为例 )

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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#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;
    gzip_min_length 5k;
    gzip_comp_level 3;
    gzip_types application/javascript image/jpeg image/svg+xml;
    gzip_buffers 4 32k;
    gzip_vary on;
    #gzip  on;
client_max_body_size 20m;
	 upstream apiserver {
        server 127.0.0.1:8080;
    }
    server {
        listen       80;
        server_name example.com www.example.com;
		 rewrite ^/(.*)$  https://$server_name$request_uri? permanent;

        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
 
    }

 

    # HTTPS server
    #
    server {
        listen       443 ssl;
        server_name  example.com www.example.com;
        ssl_certificate  /etc/letsencrypt/live/example.com/fullchain.pem;
        ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;
		# 配置 Diffie-Hellman 交换算法文件地址
		ssl_dhparam /etc/letsencrypt/live/dhparams.pem;
        # 配置服务器可使用的加密算法
    ssl_ciphers 'ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4:!DH:!DHE';
    # 指定服务器密码算法在优先于客户端密码算法时,使用 SSLv3 和 TLS 协议
    ssl_prefer_server_ciphers  on;

    # ssl 版本 可用 SSLv2,SSLv3,TLSv1,TLSv1.1,TLSv1.2
    # ie6 只支持 SSLv2,SSLv3 但是存在安全问题, 故不支持
    ssl_protocols        TLSv1 TLSv1.1 TLSv1.2;

    # 配置 TLS 握手后生成的 session 缓存空间大小 1m 大约能存储 4000 个 session
    ssl_session_cache  shared:SSL:50m;
    # session 超时时间
    ssl_session_timeout 5m;

    # 负载均衡时使用 此处暂时关闭 详情见 https://imququ.com/post/optimize-tls-handshake.html
    # 1.5.9 及以上支持
    ssl_session_tickets off;

    # 浏览器可能会在建立 TLS 连接时在线验证证书有效性,从而阻塞 TLS 握手,拖慢整体速度。OCSP stapling是一种优化措施,服务端通过它可以在证书链中封装证书颁发机构的 OCSP(Online Certificate Status Protocol)响应,从而让浏览器跳过在线查询。服务端获取 OCSP一方面更快(因为服务端一般有更好的网络环境),另一方面可以更好地缓存 以上内容来自https://imququ.com/post/my-nginx-conf-for-wpo.html
    # 1.3.7 及以上支持
    ssl_stapling               on;
    ssl_stapling_verify        on;
    # 根证书 + 中间证书
    ssl_trusted_certificate    /etc/letsencrypt/live/example.com/fullchain.pem;

    # HSTS 可以告诉浏览器,在指定的 max-age 内,始终通过 HTTPS 访问该域名。即使用户自己输入 HTTP 的地址,或者点击了 HTTP 链接,浏览器也会在本地替换为 HTTPS 再发送请求 相关配置见 https://imququ.com/post/sth-about-switch-to-https.html
    add_header Strict-Transport-Security max-age=60;
				#这里使用的是 转发到http的地址 如果指向本地服务器文件 使用如下配置
        #  location / { 
        #root 项目本地文件夹根路径
        #}
        location / {
        
           proxy_pass http://apiserver;
	proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header REMOTE-HOST $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        }
    }

}

以上配置完成后, 重启 nginx 即可完成对 https 的切换

安装Cron设置证书更新

1、检查 Cron 服务状态

1
service crond status

现实如下信息就说明已经安装

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
crond.service - Command Scheduler
   Loaded: loaded (/usr/lib/systemd/system/crond.service; enabled; vendor preset: enabled)
   Active: active (running) since 三 2019-11-20 22:02:36 CST; 3min 3s ago
 Main PID: 16835 (crond)
   CGroup: /system.slice/crond.service
           └─16835 /usr/sbin/crond -n

11月 20 22:02:36 iZ8vbepvjvx725nlz8icsoZ systemd[1]: Started Command Scheduler.
11月 20 22:02:36 iZ8vbepvjvx725nlz8icsoZ crond[16835]: (CRON) INFO (RANDOM_D...
11月 20 22:02:36 iZ8vbepvjvx725nlz8icsoZ crond[16835]: ((null)) Unauthorized...
11月 20 22:02:36 iZ8vbepvjvx725nlz8icsoZ crond[16835]: ((null)) SELinux in p...
11月 20 22:02:36 iZ8vbepvjvx725nlz8icsoZ crond[16835]: ((null)) Unauthorized...
11月 20 22:02:36 iZ8vbepvjvx725nlz8icsoZ crond[16835]: ((null)) SELinux in p...
11月 20 22:02:36 iZ8vbepvjvx725nlz8icsoZ crond[16835]: ((null)) Unauthorized...
11月 20 22:02:36 iZ8vbepvjvx725nlz8icsoZ crond[16835]: ((null)) SELinux in p...
11月 20 22:02:37 iZ8vbepvjvx725nlz8icsoZ crond[16835]: (CRON) INFO (running ...
11月 20 22:02:37 iZ8vbepvjvx725nlz8icsoZ crond[16835]: (CRON) INFO (@reboot ...
Hint: Some lines were ellipsized, use -l to show in full.

2、安装 cron 服务

依次输入以下 2 条命令并回车执行

1
2
3
yum -y install vixie-cron

yum -y install crontabs

成功安装 Cron 之后,启动 cron 服务。

3、启动 Cron 服务

1
service crond start

执行后会出现:Starting crond: [ OK ] 的提示,表明启动成功。

继续执行开机启动服务命令,把 Cron 加入开机启动的服务列表中:

1
chkconfig --level 345 crond on

安装完检查一下 Cron 服务状态

1
service crond status

如果提示:crond (pid xxxxx) is running… 代表正常运行中。

4、创建 Cron 文件并添加编辑 Certbot 的自动续期命令

输入以下命令:

1
crontab -e

相当于在/var/spool/cron 下创建了一个root文件

在root中文件输入

1
0 3 */7 * * /bin/certbot renew --renew-hook "/etc/init.d/nginx reload"

以上含义是:每隔 7 天,夜里 3 点整自动执行检查续期命令一次。续期完成后,重启 nginx 服务。

5、重启 Cron 服务,使之生效

1
service crond restart
夜河资源网提供的所有内容仅供学习与交流。通过使用本站内容随之而来的风险以及法律责任与本站无关,所承担的法律责任由使用者承担。
一、如果您发现本站侵害了相关版权,请附上本站侵权链接和您的版权证明一并发送至邮箱:yehes#qq.com(#替换为@)我们将会在五天内处理并断开该文章下载地址。
二、本站所有资源来自互联网整理收集,全部内容采用撰写共用版权协议,要求署名、非商业用途和相同方式共享,如转载请也遵循撰写共用协议。
三、根据署名-非商业性使用-相同方式共享 (by-nc-sa) 许可协议规定,只要他人在以原作品为基础创作的新作品上适用同一类型的许可协议,并且在新作品发布的显著位置,注明原作者的姓名、来源及其采用的知识共享协议,与该作品在本网站的原发地址建立链接,他人就可基于非商业目的对原作品重新编排、修改、节选或者本人的作品为基础进行创作和发布。
四、基于原作品创作的所有新作品都要适用同一类型的许可协议,因此适用该项协议, 对任何以他人原作为基础创作的作品自然同样都不得商业性用途。
五、根据二〇〇二年一月一日《计算机软件保护条例》规定:为了学习和研究软件内含的设计思想和原理,通过安装、显示、传输或者存储软件等方式使用软件的,可不经软件著作权人许可,无需向其支付报酬!
六、鉴此,也望大家按此说明转载和分享资源!本站提供的所有信息、教程、软件版权归原公司所有,仅供日常使用,不得用于任何商业用途,下载试用后请24小时内删除,因下载本站资源造成的损失,全部由使用者本人承担!