# 10 分钟将你的网站升级到 HTTPS

随着 HTTPS 的普及，现在不用 HTTPS 都不好意思把网站发给别人了，所以索性也把个人博客升级到 HTTPS 。

HTTPS 带来的好处不用多说了，直入正题。

**环境配置**

先说一下服务器环境配置，根据运行环境不同，可能会有少许配置差异。

OS: Ubuntu 14.04.2

Web Server: Nginx 1.4.6

CGI: PHP-FPM

现在提供免费证书的公益组织非常多了，[Let's encrypt](https://letsencrypt.org/) 就是其中一家。为了减少配置工作，推荐使用他们的 Shell 工具 [certbot](https://certbot.eff.org/) 在服务端自动配置。官网上列有不同 OS 的下载与安装方法。

官方推荐方式： certbot --nginx

我们这里用的是指定参数安装 **certbot certonly -w /var/www/blog -d** [**doruby.com**](https://doruby.com)

-w 是你站点存放的位置，目的是在对应的目录下，创建 *well-known/acme-challenge/* 这样的目录，并放入认证文件，它会让官网根据这个文件是否可访问，来判断这个域名是不是你所拥有的。所以要保证这个目录是可以被外部所访问到的。 所以在执行这个命令前，先确认你的 nginx 配置了它可被访问：

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

-d 是对应的域名，多个域名可以在后面继续用 -d 添加

如果命令顺利执行完成，会收到 Congratulations 的提示，并提示证书所在位置，比如 /etc/letsencrypt/live/doruby.com/fullchain.pem 记录下来。

在 Nginx 配置文件中，添加 SSL 相关配置

`server { listen 443; # 原先应该是 80`

`ssl on ssl_certificate /etc/letsencrypt/live/doruby.com/fullchain.pem; ssl_certificate_key /etc/letsencrypt/live/doruby.com/privkey.pem;`

如果相让原先访问 Http 的入口自动跳转到 Https ，可以再增加一些配置

```
server { 
    listen 80; 
    server_name doruby.com; 
    return 301;
    https://$host$request_uri;
}
```

OK. 重启 Nginx ,顺利的话你现在应该可以看到你的网站跳到 Https 了，并且前面会有绿色小锁。

这里有几点需要注意

* 如果创建证书遇到问题，第一时间检查提示的日志文件
* 如果配置完成后，网站打不开，establish 不成功，先检查一下服务器有没有打开 443 端口的访问权限


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://www.doruby.com/productivity/security/10-fen-zhong-jiang-ni-de-wang-zhan-sheng-ji-dao-https.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
