在 Nginx 中使用 letsencrypt 证书实现 HTTPS
最近在配置 matrix synapse 时,才注意到现在配置一个 https 网站已经非常简单,而且 nginx 也非常好用。
1. 生成 SSL Letsencript 证书
letsencrypt
提供免费的 SSL 证书,并且操作非常简单,命令行下几条命令即可完成。
1.1. 安装letsencrpt
首先安装命令行工具:
sudo apt install letsencrypt -y
1.2. 生成证书
再使用下面命令即可为zhiqiang.org
生成免费的 SSL 证书:
sudo certbot certonly --rsa-key-size 2048 --standalone --agree-tos --no-eff-email --email zhang@zhiqiang -d zhiqiang.org
这条命令会显示以下信息:
Cleaning up challenges
IMPORTANT NOTES:
- Congratulations! Your certificate and chain have been saved at:
/etc/letsencrypt/live/zhiqiang.org/fullchain.pem
Your key file has been saved at:
/etc/letsencrypt/live/zhiqiang.org/privkey.pem
Your cert will expire on 2020-04-21. To obtain a new or tweaked
version of this certificate in the future, simply run certbot
again. To non-interactively renew *all* of your certificates, run
"certbot 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
生成的证书文件位于文件夹/etc/letsencrypt/live/zhiqiang.org/fullchain.pem
下。
1.3. 常见错误
需要注意的是上面命令有可能出现下面的提示:
Problem binding to port 80: Could not bind to IPv4 or IPv6.
字面意思是所需要的 80 端口被占用。但我检查sudo netstat -ap | grep 80
并没有发现 80 端口被占用。使用sudo
systemctl stop nginx
停止nginx
后,问题解决,虽然原因仍不明确。
1.4. 设置自动更新证书
letsencript
提供的证书有效期只有 90
天,因此需要定期更新证书。可以在服务器添加一个crontab
定时任务来处理。由于certbot
需要sudo
权限,我们需先用sudo
-i
切换到root
用户,然后输入crontab -e
,添加下面的定时任务:
10 1 * */2 * certbot renew --pre-hook "systemctl stop nginx" --post-hook "systemctl start nginx"
添加完毕后exit
可以退出root
用户。
2. 使用 Nginx 配置 HTTPS 站点
有了上面的证书,就可以配置 HTTPS 站点了。nginx 的配置也比以前的 apache 简单多了。
2.1. 安装nginx
sudo apt install nginx
安装后的nginx
位于/etc/nginx
。
2.2. 添加站点
直接添加/etc/nginx/sites-enabled/zhiqiang.org
文件:
# 将80端口的http服务转发到443端口https服务。
server {
listen 80;
server_name zhiqiang.org;
return 301 https://$server_name$request_uri;
}
# 配置443端口的https服务
server {
listen 443 ssl;
listen [::]:443 ssl;
server_name zhiqiang.org;
ssl_certificate /etc/letsencrypt/live/zhiqiang.org/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/zhiqiang.org/privkey.pem;
root /home/zhangzq/blog/ftp;
index index.html index.htm;
location /_matrix {
proxy_pass http://127.0.0.1:8008;
proxy_set_header X-Forwarded-For $remote_addr;
}
}
# 可以配置很多个https服务,使用不同的端口,比如 https://zhiqiang.org:444。
server {
listen 444 ssl;
listen [::]:444 ssl;
server_name zhiqiang.org;
ssl_certificate /etc/letsencrypt/live/zhiqiang.org/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/zhiqiang.org/privkey.pem;
location / {
proxy_pass http://127.0.0.1:8080;
proxy_set_header X-Forwarded-For $remote_addr;
}
}
添加完毕之后执行测试命令:sudo nginx -t
,若显示下面结果表示一切正常,如有警告或错误,需根据提示修改,最常见的错误是少写分号:
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
2.3. 启动nginx
下面命令可启动nginx
:
sudo systemctl start nginx
下面命令可让系统在开机时自动启动:
sudo systemctl enable nginx
其它相关的还有重启:sudo systemctl restart nginx
,以及停止:sudo systemctl stop nginx
。
Tips: Until now, everytime you want to store your article, we will help you store it in Filecoin network. In the future, you can store it in Filecoin network using your own filecoin.
Support author:
Author's Filecoin address:
Or you can use Likecoin to support author: