首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >使用Let's Encrypt实现nginx网站https加密

使用Let's Encrypt实现nginx网站https加密

作者头像
fanzhh
发布2019-08-20 11:29:05
1.3K0
发布2019-08-20 11:29:05
举报

介绍

Let's Encrypt是一个于2015年三季度推出的数字证书认证机构,旨在以自动化流程消除手动创建和安装证书的复杂流程,并推广使万维网服务器的加密连接无所不在,为安全网站提供免费的SSL/TLS证书。证书有90天的有效期,适合个人使用或者临时使用,不用再忍受自签发证书不受浏览器信赖的提示。Certbot是一款易于使用的客户端,实现Web服务器自动提取和部署SSL/TLS证书。

前提

  • Ubuntu 16.04操作系统, sudo非root用户权限
  • 已注册并能成功解析的域名,此处以example.com为例子
  • 已为域名解析服务器设置如下DNS记录
    • 一条example.com指向你服务器公网IP地址的A记录
    • 一条www.example.com指向你服务器公网IP地址的A记录
  • 已安装Nginx

步骤一:安装Certbot

首先,增加安装库。

$ sudo add-apt-repository ppa:certbot/certbot

回车接受,然后更新软件列表。

$ sudo apt-get update

最后,安装Certbot。

$ sudo apt-get install python-certbot-nginx

Certbot现在可以使用了,不过你要先配置好Nginx。

步骤二:配置Nginx

Certbot在Nginx配置文件中找到server代码块后可以自动配置SSL。如果你刚安装Nginx,可以按照下面设置,使用nano或任何你喜欢的编辑器打开配置文件:

$ sudo nano /etc/nginx/sites-available/default

找到server_name那一行,代替为如下内容:

...
server_name example.com www.example.com;
...

保存退出,检验有没有语法错误:

$ sudo nginx -t

如果语法无误,加载Nginx的新配置:

$ sudo systemctl reload nginx

步骤三:获得SSL证书 通过不同命令选项,Certbot可以用不同的方法来获得SSL证书。Certbot的Nginx插件将自动配置Nginx并在有必要的时候自动加载它:

$ sudo certbot --nginx -d example.com -d www.example.com

这个命令的--nginx选项指明加载nginx插件,--d指明我们要认证的域名。

如果你第一次运行Certbot,会提示输入邮件地址,接受服务协议,之后,Certbot会与Let's Encrypt服务器连接,然后测试要认证的域名是否在你的控制下。

如果以上过程运行顺利,Certbot接下来会询问是否把所有HTTP的访问转向HTTPS。

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):

选择后回车。配置会生成并更新,Nginx自动加载新配置文件。Certbot会提示你配置成功以及证书的存储位置:

IMPORTANT NOTES:
 - Congratulations! Your certificate and chain have been saved at
   /etc/letsencrypt/live/example.com/fullchain.pem. Your cert will
   expire on 2017-10-23. To obtain a new or tweaked version of this
   certificate in the future, simply run certbot again with the
   "certonly" option. To non-interactively renew *all* of your
   certificates, run "certbot 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

如果现在访问你的网站,你会发现浏览器地址栏左边已经有了个绿色锁标识符,表示你的网站是安全的。

步骤四:自动更新证书

Let's Encrypt's证书有效期只有90天,看到许多网上的文章都在crontab中配置自动更新证书任务。这次(2018年6月)在Ubuntu 16.04安装Certbot(版本:0.22.2),发现安装后自动创建了一个定时任务,位置在:/etc/cron.d/certbot

# /etc/cron.d/certbot: crontab entries for the certbot package
#
# Upstream recommends attempting renewal twice a day
#
# Eventually, this will be an opportunity to validate certificates
# haven't been revoked, etc.  Renewal will only occur if expiration
# is within 30 days.
SHELL=/bin/sh
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin

0 */12 * * * root test -x /usr/bin/certbot -a \! -d /run/systemd/system && perl -e 'sleep int(rand(3600))' && certbot -q renew

部分内容参考自这篇文章。

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2018.06.11 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 介绍
  • 前提
  • 步骤一:安装Certbot
  • 步骤二:配置Nginx
  • 步骤四:自动更新证书
相关产品与服务
SSL 证书
腾讯云 SSL 证书(SSL Certificates)为您提供 SSL 证书的申请、管理、部署等服务,为您提供一站式 HTTPS 解决方案。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档