3

6.HTTPS协议配置

 2 years ago
source link: http://muyunyun.cn/blog/mx5pvgl1/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Https 协议配置

免费的 Https 证书,最多只有一年的期限,而且每个二级子域名要单独申请,很浪费时间。使用本文提供的方法,可以只配置一次,实现证书永久自动续期。

acme.sh 安装实践

笔者一开始将 acme.sh 安装在树莓派中,绕了弯。应将 acme.sh 安装在云服务器端

  • 步骤一: 安装 acme.sh。

https://github.com/acmesh-official/acme.sh/wiki/sudo

# unstall for current user
acme.sh --uninstall
# change to root(需要执行)
sudo su
# install again for root user
curl https://get.acme.sh | sh -s [email protected]
source ~/.bashrc
  • 步骤二: 获取 https 证书。

说明: 本文以 www.muyunyun.cn 作为案例演示,基于树莓派搭建家庭服务器 中的 frp.muyunyun.cn 如果要配置为 HTTPS,操作同理。

acme.sh --set-default-ca --server letsencrypt
acme.sh --issue -d www.muyunyun.cn --nginx

3e990527ed80cb020704ab30aa502856.jpg-400

在这一步中 acme.sh 读取了 nginx 配置(路径: /etc/nginx/conf.d/www.muyunyun.cn.conf),并自动生成了证书。

调试方法: acme.sh --issue -d www.muyunyun.cn --nginx --debug 2

  • 步骤三: 将证书拷贝到 /etc/nginx/ssl 文件夹。

创建文件夹

mkdir -p /etc/nginx/ssl/www.muyunyun.cn

拷贝证书:

acme.sh --install-cert -d www.muyunyun.cn \
--key-file /etc/nginx/ssl/www.muyunyun.cn/www.muyunyun.cn.key \
--fullchain-file /etc/nginx/ssl/www.muyunyun.cn/fullchain.cer \
--reloadcmd "service nginx force-reload"

请一定使用以上语法 acme.sh --install-cer 进行拷贝,这样证书才能保证在新的位置也能自动更新。

922adc77ccacd8bb3c52dd9ceb8e080e.jpg

  • 步骤四: 将 /etc/nginx/ssl/www.muyunyun.cn/ 中的证书手动配置到 nginx, 并重启 nginx 使之生效。

/etc/nginx/conf.d/www.muyunyun.cn.conf 中的内容替换为:

server {
listen 80;
listen [::]:80;
server_name www.muyunyun.cn;
root /usr/share/nginx/html/www.muyunyun.cn;
error_page 404 /404.html;
location = /404.html {
error_page 500 502 503 504 /50x.html;
location = /50x.html {
server {
listen 443 ssl http2;
listen [::]:443 ssl http2;
server_name www.muyunyun.cn;
root /usr/share/nginx/html/www.muyunyun.cn;
ssl_certificate "/etc/nginx/ssl/www.muyunyun.cn/fullchain.cer";
ssl_certificate_key "/etc/nginx/ssl/www.muyunyun.cn/www.muyunyun.cn.key";
ssl_session_cache shared:SSL:1m;
ssl_session_timeout 10m;
ssl_ciphers HIGH:!aNULL:!MD5;
ssl_prefer_server_ciphers on;
# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;
error_page 404 /404.html;
location = /40x.html {
error_page 500 502 503 504 /50x.html;
location = /50x.html {

重启 nginx:

systemctl restart nginx

打开 https://www.muyunyun.cn,证书已生效。

a397cb63269be96f1102a117bbe3fb49.jpg-400

由于我们全程使用 acme.sh 进行安装,acme.sh 会自动为你创建一个定时任务, 每天 0:00 点自动检测所有的证书, 如果快过期了, 需要更新, 则会自动更新证书。

运行 ps aux | grep acme 可以看到 acme 一直在后台运行。

root@VM-16-17-ubuntu:/etc/nginx/ssl/www.muyunyun.cn# ps aux | grep acme
root 1561193 0.0 0.0 7348 748 pts/0 S+ 03:57 0:00 grep --color=auto acme

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK