概述
Let’s Encrypt作为一个公共且免费SSL的项目逐渐被广大用户传播和使用,是由Mozilla、Cisco、Akamai、IdenTrust、EFF等组织人员发起,主要的目的也是为了推进网站从HTTP向HTTPS过度的进程,目前已经有越来越多的商家加入和赞助支持。
感觉全民https时代到了,这段时间看到很多站都开启了https, 刚好昨天看到一条新闻介绍各种免费 SSL 证书,就跟风拿博客练下手吧。
对比了多个免费证书厂商的的简介后,决定使用目前使用范围最为广泛的免费 SSL 证书-Let’s Encrypt。虽然它是免费的,但它还有些缺憾的:免费期只有3个月且暂时还不支持提供通配符 SSL 证书,不过目前官方称已经测试通配符证书了,再过一个多月估计就开放了。
使用步骤
- 获取证书
服务器环境:Nginx + Ubuntu
安装certbot-auto, 有两种方法:
a. git克隆:
git clone https://github.com/letsencrypt/letsencrypt
b. 命令行下载:
wget https://dl.eff.org/certbot-auto
完成后运行
./certbot-auto certonly
第一次运行可能还会自动安装一些依赖,等待自行安装完后将出现:
How would you like to authenticate with the ACME CA?
-------------------------------------------------------------------------------
1: Nginx Web Server plugin - Alpha (nginx)
2: Spin up a temporary webserver (standalone)
3: Place files in webroot directory (webroot)
-------------------------------------------------------------------------------
Select the appropriate number [1-3] then [enter] (press 'c' to cancel): 1 <---选择验证方式,也就是验证有没有域名所有权
Plugins selected: Authenticator nginx, Installer None
Enter email address (used for urgent renewal and security notices) (Enter 'c' to
cancel): xxxxxx@gmail.com <---接收通知的邮箱,比如证书快到期提醒等
-------------------------------------------------------------------------------
Please read the Terms of Service at
https://letsencrypt.org/documents/LE-SA-v1.2-November-15-2017.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 <---接受服务协议
-------------------------------------------------------------------------------
Would you be willing to share your email address with the Electronic FrontierFoundation, 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 <---这里选N吧,邮箱不接收一些乱七八糟的信息
Please enter in your domain name(s) (comma and/or space separated) (Enter 'c'
to cancel): freshines.com www.freshines.com <---证书对应的域名,多个域名写一起,因为还不支持域名通配,所以二级域名也需要列出来
Obtaining a new certificate
Performing the following challenges:
tls-sni-01 challenge for freshines.com
tls-sni-01 challenge for www.freshines.com
Waiting for verification...
Cleaning up challenges
IMPORTANT NOTES:
- Congratulations! Your certificate and chain have been saved at:
/etc/letsencrypt/live/freshines.com/fullchain.pem
Your key file has been saved at:
/etc/letsencrypt/live/freshines.com/privkey.pem
Your cert will expire on 2018-04-08. To obtain a new or tweaked
version of this certificate in the future, simply run
letsencrypt-auto again. To non-interactively renew *all* of your
certificates, run "letsencrypt-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
上面斜体的两处就是证书文件,后面配置将用到这两个路径。
- nginx配置
编辑nginx配置文件
vi /etc/nginx/nginx.conf
server {
# 监听端口
listen 443;
# 域名
server_name www.freshines.com freshines.com;
ssl on;
ssl_certificate /etc/letsencrypt/live/freshines.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/freshines.com/privkey.pem;
...
如果需要将http强制跳转到https, 在上述文件再添加:
server {
# 监听端口
listen 80;
# 域名
server_name www.freshines.com freshines.com;
#rewrite ^(.*)$ https://$host$1 last;
return 301 https://$server_name$request_uri;
}
ssl证书续期
因为每次只有三个月的免费期,因此到期前需要续期,以免影响访问。我们可以写个定时任务,让系统自动运行进行续期。
运行命令:
crontab -e
末尾添加以下内容,在每月1日自动运行
0 0 1 * * /home/freshines/work/online/letsencrypt/certbot-auto renew --pre-hook "service nginx stop" --post-hook "service nginx start"