---
title: "在 Nginx 上使用 Let's Encrypt 加密(HTTPS)你的网站[简明教程]"
date: 2016-02-21T01:39:30Z
modified: 2020-03-08T03:20:45Z
permalink: "https://www.appinn.com/use-letsencrypt-with-nginx/"
type: post
status: publish
excerpt: ""
categories:
  - 网络工具
tags:
  - 网络工具
  - https
  - "Let's Encrypt"
  - Nginx
  - ssl
featured_image: "https://www.appinn.com/wp-content/uploads/2016-02-20-5-46-05.pngo_.png"
author: scavin
timestamp: 2020-03-08T03:20:45Z
---

前几天有个消息，只在特定的圈子里传播了一下：[The PKI platform of StartSSL is now hosted by Qihoo 360](https://pierrekim.github.io/blog/2016-02-16-why-i-stopped-using-startssl-because-of-qihoo-360.html)，大意是老牌的免费证书提供商 StartSSL 将服务器放到了 Qihoo 360 的服务器上，于是担心证书已不再安全。从而转向免费证书新星 **Let’s Encrypt**。

![](https://static2.appinn.com/wp-content/gallery/201602/2016-02-20-5-46-05.png!o)

青小蛙曾经用过好几年的 StartSSL 服务，为此还付费使用过，一年 59.9 美金，你可以随意签发证书。不过近两年证书普遍降价，再加上免费的 **Let’s Encrypt** 和即将到来的 HTTP/2，互联网的全面 HTTPS 化将在不久的将来实现…届时不会再有劫持，不会再有干扰…总之看起来很美。

> **[Let’s Encrypt](https://letsencrypt.org/)** 是一个将于2015年末推出的数字证书认证机构，将通过旨在消除当前手动创建和安装证书的复杂过程的自动化流程，为安全网站提供免费的SSL/TLS证书。
> 
> Let’s Encrypt 是由互联网安全研究小组（ISRG，一个公益组织）提供的服务。主要赞助商包括电子前哨基金会，Mozilla基金会，Akamai以及思科。2015年4月9日，ISRG与Linux基金会宣布合作。

那么，现在使用 **Let’s Encrypt** 则是一个非常经济实惠又安全的选择，于是青小蛙参考 [How To Secure Nginx with Let’s Encrypt on Ubuntu 14.04](https://www.digitalocean.com/community/tutorials/how-to-secure-nginx-with-let-s-encrypt-on-ubuntu-14-04) 这篇文章，有了这篇教程。

### 必备条件：

1. 有一台 VPS，推荐 [HH 的 VPS](https://hellohostnet.com/vps.html) 产品；
2. 有一个域名(可以在 [HH 注册](http://hellohostnet.com/domain.html))；
3. 想加密自己的网站

实现结果：

![](https://static2.appinn.com/wp-content/gallery/201602/nginx-letsencrypt.png!o)

用户通过 TLS/SSL 加密访问你的网站，而后台实现自动续签 Let’s Encrypt 证书。

### 开始

教程基于 Ubuntu 14.04 系统，不够其他 Linux 版本都是类似的，用你熟悉的系统即可。

### 第 1 步：安装 Let’s Encrypt 客户端

装前准备，更新系统和安装 git & bc：

> _`apt-get update<br></br>``apt-get -y install git bc`_

克隆 Let’s Encrypt 到 /opt/letsencrypt:

> _git clone https://github.com/letsencrypt/letsencrypt /opt/letsencrypt_

### 第 2 步：获取证书

首先关闭 Nginx：

> _service nginx stop_

并且检查一下 80 端口没有被占用：

> _netstat -na | grep ‘:80.\*LISTEN’_

运行 Let’s Encrypt:

> _cd /opt/letsencrypt_
> _./letsencrypt-auto certonly -a webroot –webroot-path=/usr/share/nginx/html -d example.com -d www.example.com_

_注意：Let’s Encrypt 需要超级用户权限，如果你没有使用 sudo 命令，可能会让你输入密码。_

之后会出现图形界面输入邮箱、条款、域名等信息:

![](https://static2.appinn.com/wp-content/gallery/201602/le-domain.png!o)

支持多域名，只需要在第三张截图里用空格或者英文逗号分隔就好了。

**_目前 Let’s Encrypt 没有 EV 证书与 泛域名证书的计划。_**

> IMPORTANT NOTES:
> 
> - If you lose your account credentials, you can recover through e-mails sent to domain@appinn.com
> - Congratulations! Your certificate and chain have been saved at /etc/letsencrypt/live/example.com/fullchain.pem. Your cert will expire on 2016-05-15. To obtain a new version of the certificate in the future, simply run Let’s Encrypt again.
> - Your account credentials have been saved in your Let’s Encrypt 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 Let’s Encrypt so making regular backups of this folder is ideal.
> - If like Let’s Encrypt, please consider supporting our work by:
> 
> Donating to ISRG / Let’s Encrypt: https://letsencrypt.org/donate
> Donating to EFF: https://eff.org/donate-le

看到这一段，就正常证书已经签发成功，位于 /etc/letsencrypt，注意备份。

如果使用国内 VPS，此处可能会由于 DNS 问题出错，可以尝试更换 VPS 的 DNS 为第三方，比如 8.8.8.8。

每一个域名都会自动生成四个文件，位于 /etc/letsencrypt/archive/domain 目录下：

> cert.pem: 域名证书
> chain.pem: The Let’s Encrypt 证书
> fullchain.pem: 上面两者合体
> privkey.pem: 证书密钥

### 第 3 步：配置 Nginx

有了域名证书，就开始配置 Nginx 了，这部分比较略。

打开对应网站的配置文件，一般在 /etc/nginx/sites-available/default 或者 /usr/local/nginx/conf/ 中，试你自己的情况。

> `server {`
> 
> listen 443 ssl;
> server\_name appinn.com;
> …
> 
> ssl on;
> ssl\_certificate /etc/letsencrypt/live/appinn.com/fullchain.pem;
> ssl\_certificate\_key /etc/letsencrypt/live/appinn.com/privkey.pem;
> 
> ssl\_protocols TLSv1 TLSv1.1 TLSv1.2;
> ssl\_prefer\_server\_ciphers on;
> ssl\_ciphers ‘EECDH+AESGCM:EDH+AESGCM:AES256+EECDH:AES256+EDH’;
> 
> …
> 
> }

注意修改红色的部分为你的域名

如果你想开启全站 https，需要将 http 转向到 https，再添加一个 server 就好了：

> server {
> listen 80;
> server\_name appinn.com;
> return 301 https://$host$request\_uri;
> }

注意以上配置文件需要根据实际情况修改。

保存，重启 Nginx

> _service nginx restart_

此时，打开你的域名比如 https:// 就能看到绿色的地址栏了。

### 第 4 步：自动续签证书

Let’s Encrypt 证书只有 90 天的有效期，这和之前按年使用的商业证书有些区别，所以我们还需要设置自动续签，好让证书一直有效。

#### 安装 Webroot 插件

这是一个可以不用停止 Web 服务就能让 Let’s Encrypt 验证域名的插件，再次打开 Nginx 配置文件，在 ssl 下面添加：

> location ~ /.well-known {
> allow all;
> }

保存。

#### 使用命令行续签证书

> _/opt/letsencrypt/letsencrypt-auto renew_

创建定时任务：

> _crontab -e_

添加下面一行，让每周一早上 2 点 30 分运行一次，并记录到日志文件中。

> _30 2 \* \* 1 /opt/letsencrypt/letsencrypt-auto renew_ >> /var/log/le-renewal.log

### 结束

至此，在 Nginx 上使用 Let’s Encrypt 的配置与续签全部完成，今后就完全不需要打理这一部分了，HTTPS 将默默工作。目前**善用佳软** 已经在使用 Let’s Encrypt，可以围观一下：[https://xbeta.info](https://xbeta.info)

![](https://static2.appinn.com/wp-content/gallery/201602/2016-02-21-2-35-47.png!o)

小众软件还有一枚有效期内的泛域名证书，目前还没有使用 Let’s Encrypt，是的，你可以用 [https://www.appinn.com](https://www.appinn.com) 来访问小众软件，不过由于历史以及 CDN 遗留问题，还没有全站切换过去，等待 HTTP/2 吧。

不过发现频道([https://faxian.appinn.com](https://faxian.appinn.com))已经 HTTPS 了哦。有疑问欢迎留言反馈。

## Topics

**分类目录:** [网络工具](https://www.appinn.com/category/net/?output_format=md)

**标签:** [https](https://www.appinn.com/tag/https/?output_format=md), [Let's Encrypt](https://www.appinn.com/tag/lets-encrypt/?output_format=md), [Nginx](https://www.appinn.com/tag/nginx/?output_format=md), [ssl](https://www.appinn.com/tag/ssl/?output_format=md)