Nginx安装免费的SSL证书和自动更新

in Like with 1 comment

现在越来越多的网站开始启用SSL,下面介绍怎么安装免费的SSL证书,并通过计划任务实现证书的自动更新。

获取certbot并运行

wget https://dl.eff.org/certbot-auto
chmod +x certbot-auto
./cerrbot-auto

How would you like to authenticate and install certificates?
-------------------------------------------------------------------------------
1: Apache Web Server plugin - Beta (apache)
2: Nginx Web Server plugin - Alpha (nginx)
-------------------------------------------------------------------------------
Select the appropriate number [1-2] then [enter] (press 'c' to cancel): 2 <-选Nginx
Plugins selected: Authenticator nginx, Installer nginx
Enter email address (used for urgent renewal and security notices) (Enter 'c' to
cancel): ww@xx.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 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 

Which names would you like to activate HTTPS for?
-------------------------------------------------------------------------------
1: xx.xx.com <-这里会列出所有域名
2:yy.yy.com
-------------------------------------------------------------------------------
Select the appropriate numbers separated by commas and/or spaces, or leave input
blank to select all options shown (Enter 'c' to cancel): 1 <-选择要启用SSL的域名,回车代表选中所有域名

Obtaining a new certificate
Performing the following challenges:
http-01 challenge for xx.xx.com
Waiting for verification...
Cleaning up challenges
Deploying Certificate to VirtualHost /etc/nginx/conf.d/nginx.conf

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): 2 <-选2代表将所有HTTP访问转向到HTTPS
Redirecting all traffic on port 80 to ssl in /etc/nginx/conf.d/nginx.conf

-------------------------------------------------------------------------------
Congratulations! You have successfully enabled https://xx.xx.com  <-安装成功
重启Nginx,让SSL证书生效
systemctl restart nginx
使用计划任务来实现证书自动更新

将下面的脚本另存为auto_ssl_update.sh

/etc/letsencrypt/certbot-auto renew
systemctl restart nginx

赋予运行权限

chmod +x auto_ssl_update.sh

添加计划任务

crontab -e

加入

* 23 * * * /etc/letsencrypt/auto_ssl_update.sh

这样就会自动更新证书了。

Tips:
推荐一个非常好的检测SSL证书的网站。

Comments are closed.