---
title: "使用letsencrypt升级网站到https"
description: "因为[Hipo在线版](https://hipo.cash/admin)的~~计划(不日放出)~~，所以将hipo.cash升级到https，证书是免费开放的 [letsencrypt](https://letsencrypt.org)，配合官方的工具[certbot](https://certbot.eff.org/#ubuntutrusty-nginx)。"
url: "https://hicc.pro/p/blog/letsencrypt-cerbot-https"
author: "tyler wang"
published: "2016-10-09T13:57:50.000Z"
updated: "2018-01-10T12:51:58.000Z"
category: "blog"
tags:
  - "Hipo"
  - "https"
  - "letsencrypt"
  - "certbot"
lang: "zh-CN"
site: "Hipo日志"
---

# 使用letsencrypt升级网站到https

因为[Hipo在线版](https://hipo.cash/admin)的~~计划(不日放出)~~，所以将hipo.cash升级到https，证书是免费开放的 [letsencrypt](https://letsencrypt.org)，配合官方的工具[certbot](https://certbot.eff.org/#ubuntutrusty-nginx)。

----  2018.1.10  ----

新版本的cerbot更加的方便，下面的命令还是来自于cerbot网站

```
// 如果是ubuntu nginx的方案
$ sudo apt-get update
$ sudo apt-get install software-properties-common
$ sudo add-apt-repository ppa:certbot/certbot
$ sudo apt-get update
$ sudo apt-get install python-certbot-nginx 

// 然后直接
$ sudo certbot --nginx
// 它会枚举出你 server的网站，你选择需要升级https的域名后，它甚至直接帮你配制nginx，重启，真正的一键。
```

当然如果你之前就配了https的话，如果用完cerbot，还是不能访问，可以参考下面：
```
listen 443 ssl; # managed by Certbot
ssl_certificate /etc/letsencrypt/live/hicc.me/fullchain.pem; # managed by Certbot
ssl_certificate_key /etc/letsencrypt/live/hicc.me/privkey.pem; # managed by Certbot
include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot

# Redirect non-https traffic to https
if ($scheme != "https") {
    return 301 https://$host$request_uri;
} # managed by Certbot
```

----

letsencrypt通过访问你服务器上生成的一个随机文件来验证你的域名，所以再使用cerbot脚本之前，首先需要确保要能够通过你的域名直接访问到`.well-known`目录，如果你讲随机文件放倒`/var/www/server/challenges`可以这样配置nginx。

```
server {
    server_name www.yoursite.com yoursite.com;

    location ^~ /.well-known/ {
        alias /var/www/server/challenges;
        try_files $uri =404;
    }
}
```

下面的教程基本来自[cerbot官网](https://certbot.eff.org/#ubuntutrusty-nginx)(使用ubuntu 14 + nginx)，

下载cerbot

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

./certbot-auto
```

然后运行
```
./path/to/certbot-auto certonly
```
然后就是输入联系邮箱，选择标准插件还是"webroot"(官网原话:which can work with the webroot directory of any webserver software，hipo选用的是这个)插件，之后输入需要升级的域名(hipo.cash www.hipo.cash)，之后就是输入验证域名随机文件生成的目录(也就是上述所说的`/var/www/server/challenges`)。

Done！！ 一般情况就会提示执行成功。

之后配置nginx监听443，重定向http到https
```
server {
    listen 80 ;
    listen [::]:80;
    server_name hipo.cash www.hipo.cash;
    return  301 https://hipo.cash$request_uri;
}

server {
    listen 443 ssl ;
    listen [::]:443 ssl;

    server_name hipo.cash www.hipo.cash; # Replace with your domain
    ssl on;
    ssl_certificate /etc/letsencrypt/live/hipo.cash/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/hipo.cash/privkey.pem;
    ssl_trusted_certificate /etc/letsencrypt/live/hipo.cash/chain.pem;
    location / {
        proxy_pass http://127.0.0.1:18080;
    }
}
```

重启nginx，正常情况下网站便成功升级到了https😄

letsencrypt的脚本需要90后renew下，certbot也支持自动更新。

```
./path/to/certbot-auto renew --quiet --no-self-upgrade
```

可以通过下面验证下

```
./path/to/certbot-auto renew --dry-run
```

更新之后需要重启下nginx

```
service nginx restart
```

![https://hipo.cash/ 手机版](https://image.hicc.pro/image/1/f3/c3af92470cb11b07267da2bcf1948.JPG)

----
因为hicc.me中图片自用用的是七牛http链接，替换稍有点麻烦，hicc.me只能稍后升级到https了～～

---

原文 / Source: <https://hicc.pro/p/blog/letsencrypt-cerbot-https>
