博客网站开通免费https支持(ssl证书)

根据 https://www.freehao123.com/top-8-free-ssl-cert/ 的推荐,最终选择 Let’s Encrypt,毕竟免费SSL这东西,还是要根正苗红,有了浏览器本身的支持,用户才能顺利进入。

安装Let’s Encrypt

安装git,已经安装的可以跳过这一步:

1
[server ~]# yum install git

下载certbot:

1
2
[server ~]# git clone https://github.com/certbot/certbot
[server ~]# cd certbot/

停止nginx:

1
2
[server certbot]# service nginx stop
Stopping nginx: [ OK ]

我的网站用的是nginx,安装certbot证书非常简单,一个命令下去即可,certbot同事支持apache插件,或者standalone等安装方式( https://certbot.eff.org/docs/using.html#getting-certificates-and-choosing-plugins ),如果是其他方式,建议参考 https://www.tecmint.com/setup-https-with-lets-encrypt-ssl-certificate-for-nginx-on-centos/ 或者官网文档。

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
[server certbot]# ./certbot-auto --nginx
yum is /usr/bin/yum
...
Is this ok [y/N]: y #先会自动使用yum下载一些安装包,输入y
...
Plugins selected: Authenticator nginx, Installer nginx
Enter email address (used for urgent renewal and security notices) (Enter 'c' to
cancel): youremail@163.com #输入你的邮箱地址
...
Please read the Terms of Service at
https://letsencrypt.org/documents/LE-SA-v1.1.1-August-1-2016.pdf. You must agree
in order to register with the ACME server at
https://acme-v01.api.letsencrypt.org/directory
-------------------------------------------------------------------------------
(A)gree/(C)ancel: A #输入A,同意
-------------------------------------------------------------------------------
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: N #输入Y或者N
Which names would you like to activate HTTPS for?
-------------------------------------------------------------------------------
1: mnstory.net
2: www.mnstory.net
-------------------------------------------------------------------------------
Select the appropriate numbers separated by commas and/or spaces, or leave input
blank to select all options shown (Enter 'c' to cancel): 1 2 #上面自动解析了nginx配置,列出了域名,输入序号,可以多选
Obtaining a new certificate
Performing the following challenges:
tls-sni-01 challenge for www.mnstory.net
tls-sni-01 challenge for mnstory.net
Waiting for verification...
Cleaning up challenges
Deployed Certificate to VirtualHost /etc/nginx/nginx.conf for set(['www.mnstory.net', 'mnstory.net'])
Please choose whether or not to redirect HTTP traffic to HTTPS, removing HTTP access.
-------------------------------------------------------------------------------
1: No redirect - Make no further changes to the webserver configuration.
2: Redirect - Make all requests redirect to secure HTTPS access. Choose this for
new sites, or if you're confident your site works on HTTPS. You can undo this
change by editing your web server's configuration.
-------------------------------------------------------------------------------
Select the appropriate number [1-2] then [enter] (press 'c' to cancel): 1 #是否同意将HTTP redirect 到 HTTPS,建议选择2
-------------------------------------------------------------------------------
Congratulations! You have successfully enabled https://www.mnstory.net, and https://mnstory.net
You should test your configuration at:
https://www.ssllabs.com/ssltest/analyze.html?d=www.mnstory.net
https://www.ssllabs.com/ssltest/analyze.html?d=mnstory.net
-------------------------------------------------------------------------------
IMPORTANT NOTES:
- Congratulations! Your certificate and chain have been saved at:
/etc/letsencrypt/live/www.mnstory.net/fullchain.pem
Your key file has been saved at:
/etc/letsencrypt/live/www.mnstory.net/privkey.pem # 这里显示了证书存放地址
Your cert will expire on 2018-01-12. To obtain a new or tweaked # 这里显示了过期时间和过期后的解决办法,应该就是调用 certbot-auto renew,我还没有试过。
version of this certificate in the future, simply run certbot-auto
again with the "certonly" option. To non-interactively renew *all*
of your certificates, run "certbot-auto renew"
- Your account credentials have been saved in your Certbot
configuration directory at /etc/letsencrypt. You should make a
secure backup of this folder now. This configuration directory will
also contain certificates and private keys obtained by Certbot so
making regular backups of this folder is ideal.
- 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

现在,你可以在浏览器访问你的https版本和http版本网站,看效果了。你也可以访问我的站点 https://mnstory.net 看效果。

分开http和https

这一步一般人都不需要,我是特殊需求,上面Please choose whether or not to redirect HTTP traffic to HTTPS, removing HTTP access 这一步,一般都是选择80端口自动跳转到443,我选择的是不跳转,自动生成的配置为带# managed by Certbot注释部分:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
server {
listen 80;
server_name www.mnstory.net mnstory.net;
root /your-www-root;
if ($host !~* mnstory\.net$) {
return 444;
}
include server.conf;
listen 443 ssl; # managed by Certbot
ssl_certificate /etc/letsencrypt/live/www.mnstory.net/fullchain.pem; # managed by Certbot
ssl_certificate_key /etc/letsencrypt/live/www.mnstory.net/privkey.pem; # managed by Certbot
include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
}

我准备将443和80分开,因为老的blog是80端口,新的blog准备替换成hexo+markdown方式写作,一时间不能将老文章全部翻译为markdown,所以存放到两个路径,让80和443访问的数据不一样,修改配置文件:

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
[server certbot]# vi /etc/nginx/nginx.conf
server {
listen 80;
server_name www.mnstory.net mnstory.net;
root /your-www-root;
if ($host !~* mnstory\.net$) {
return 444;
}
include server.conf;
}
server {
server_name www.mnstory.net mnstory.net;
root /your-www-root2;
if ($host !~* mnstory\.net$) {
return 444;
}
include server.conf;
listen 443 ssl; # managed by Certbot
ssl_certificate /etc/letsencrypt/live/www.mnstory.net/fullchain.pem; # managed by Certbot
ssl_certificate_key /etc/letsencrypt/live/www.mnstory.net/privkey.pem; # managed by Certbot
include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
}

如果是想重定向80到443,可以修改80的server配置为:

1
2
3
4
5
server {
listen 80;
server_name www.mnstory.net mnstory.net;
rewrite ^(.*)$ https://${server_name}$1 permanent;
}

重启nginx后生效。