欢迎,来自IP地址为:18.217.6.114 的朋友


Let’s Encrypt作为一个公共且免费SSL的项目逐渐被广大用户传播和使用,是由Mozilla、Cisco、Akamai、IdenTrust、EFF等组织人员发起,主要目的是推进网站从HTTP向HTTPS过渡的进程,目前已经有越来越多的商家加入和赞助支持。

Let’s Encrypt免费SSL证书的出现,使我这个小型网站也有机会开启HTTPS功能来装一回B(@_@)。不过话说回来,苹果已经强制要求APP通过HTTPS来连接网络服务,网站HTTPS化是趋势,毕竟安全为先么。

申请和使用Let’s Encrypt证书都比较简单,官方网站推荐使用一个Certbot工具,可以完成环境的自动配置。当然也可以使用其它的ACME(Automatic Certificate Management Environment)客户端,不过还是使用自动脚本比较方便。我为本站Nginx服务器添加SSL功能有如下步骤:

1、下载和运行Certbot工具

使用wget工具下载Certbot软件,并执行该程序。此处加入-v参数是为了可以显示更多的信息,由于软件在安装过程中会下载很多依赖包和python软件,开启此功能后就知道程序具体进度情况。

$ wget https://dl.eff.org/certbot-auto
$ chmod a+x certbot-auto
$ ./certbot-auto -v

2、通过 certbot工具申请域名证书

程序安装完成后,可以通过certbot工具交互式申请域名证书。使用命令certauto certonly来申请证书及生成密钥文件。

$ ./certbot-auto certonly
。。。。。。。。。。。。。。。。                                                                 //省略了部分提示信息
How would you like to authenticate with the ACME CA?
-------------------------------------------------------------------------------
1: Place files in webroot directory (webroot)
2: Spin up a temporary webserver (standalone)
-------------------------------------------------------------------------------
Select the appropriate number [1-2] then [enter] (press 'c' to cancel): 1                //选择一种证书验证方式
Please enter in your domain name(s) (comma and/or space separated) (Enter 'c'
to cancel):example.daehub.com                                                                       //输入域名
Obtaining a new certificate
Performing the following challenges:
http-01 challenge for example.daehub.com
Select the webroot for example.daehub.com:
-------------------------------------------------------------------------------
1: Enter a new webroot
-------------------------------------------------------------------------------
Press 1 [enter] to confirm the selection (press 'c' to cancel): 1
Input the webroot for example.daehub.com: (Enter 'c' to cancel):
/webroot/                                                                                                        //输入网站根目录
Generating key (2048 bits): /etc/letsencrypt/keys/0000_key-certbot.pem
Creating CSR: /etc/letsencrypt/csr/0000_csr-certbot.pem
IMPORTANT NOTES: - Congratulations! 
Your certificate and chain have been saved at /etc/letsencrypt/live/example.daehub.com/fullchain.pem. 
Your cert will expire on 2017-04-18. 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

以上步骤便完成了Let’s Encrypt证书的申请,申请成功后会在/etc/letsencrypt/live/example.daehub.com/目录生成4个文件用于SSL。

3、配置Nginx的SSL生效

在Nginx的配置文件中修改example.daehub.com对应的server项内容,开启SSL功能及侦听443端口,并将80端口的请求均重定向到443端口。对于PHP网站,要在443端口的server增加相应内容。

server {
    listen 443;
    server_name example.daehub.com;    #charset koi8-r;
    #access_log /var/log/nginx/log/host.access.log main;
    ssl on;
    ssl_certificate /etc/letsencrypt/live/example.daehub.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/example.daehub.com/privkey.pem;
}
    server {
    listen 80;
    server_name www.daehub.com;
    return 301 https://example.daehub.com$request_uri;
}

4、修改其它原HTTP内容
由于网站原来是采用HTTP方式搭建的,切换至HTTPS后,会有一些内容是同原来http的网址绑定,如插入到文章中的图片媒体等。对于WP网站,需要将WordPress地址(URL)和站点地址(URL)修改为HTTPS对应的网址,文章中的媒体内容可以通过删除后重新插入方式更改其对应地址,由于我还使用了百度地图的js代码,也根据网站提示修改为支持HTTPS的文件。

至此,一个采用Let’s Encrypt证书的Nginx网站便顺利的切换成HTTPS的安全网站。看着浏览器绿色安全的提示,是不是有点小成就感呢。

最后提示一下,Let’s Encrypt证书有效 期只有90天,到期后需要使用certbot-auto renew命令重新申请。可以使用linux的任务计划实现自动更新证书。

One thought on “Nginx网站用Let’sEncrypt证书开HTTPS”

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注