跳去主要內容
返上一頁

Debian / Ubuntu 用 nginx-acme 自動搞掂 SSL Certificate

Debian / Ubuntu 用 nginx-acme 自動搞掂 SSL Certificate

最近又開咗部 Debian VPS,順手整埋 Web Server。

以前玩 Nginx,如果想開 HTTPS,通常流程都係:

  1. 安裝 Certbot
  2. 申請 SSL Certificate
  3. 修改 Nginx Config
  4. 設定自動 Renewal

今次試下 nginx-acme 呢個方案。

簡單講:

由 Nginx 自己處理 ACME Certificate。


nginx-acme 係咩?

nginx-acme 係 Nginx 官方推出嘅 ACME Module,用來自動申請同管理 SSL Certificate。

支援:


安裝前準備

以下假設:

更新系統:

sudo apt update
sudo apt upgrade -y

安裝工具:

sudo apt install curl vim wget gnupg dpkg apt-transport-https lsb-release ca-certificates

安裝 Nginx

加入 Repository:

curl -sSL https://n.wtf/public.key | sudo bash -c 'gpg --dearmor > /usr/share/keyrings/n.wtf.gpg'

更新:

sudo apt update

安裝:

sudo apt install nginx-extras -y

配置 nginx-acme

建立 Certificate Storage:

sudo mkdir -p /var/cache/nginx/letsencrypt
sudo chown 33:33 /var/cache/nginx -R

修改:

sudo nano /etc/nginx/sites-enabled/default

加入:

resolver 8.8.8.8:53 ipv6=off valid=5s;

acme_issuer letsencrypt {
    uri https://acme-v02.api.letsencrypt.org/directory;
    contact [email protected];
    state_path /var/cache/nginx/letsencrypt;
    accept_terms_of_service;
    ssl_trusted_certificate /etc/ssl/certs/ca-certificates.crt;
    ssl_verify on;
}

acme_shared_zone zone=ngx_acme_shared:1M;

配置 HTTPS Site

server {
    listen 443 ssl;

    server_name example.com;

    root /var/www/html;

    ssl_protocols TLSv1.3;

    acme_certificate letsencrypt;

    ssl_certificate $acme_certificate;
    ssl_certificate_key $acme_certificate_key;
}

重點:

acme_certificate letsencrypt;

呢行會令 Nginx 自動處理 Certificate。


測試

sudo nginx -t
sudo nginx -s reload

之後:

https://example.com

應該已經可以正常使用 HTTPS。


Certificate Renewal

nginx-acme 會自動處理 Renewal,唔需要另外寫 Cron。

查看:

journalctl -u nginx

總結

玩 VPS 咁耐,最煩通常唔係裝 Nginx。

真正麻煩係:

以前開一部新 VPS,又要重新搞一次 Certbot。

而家配置好 nginx-acme,之後交畀 Nginx 自己處理。

少啲手動操作,多啲自動化。

慢慢將重複工作自動化,先係玩 Infrastructure 嘅樂趣。


Share 哩篇文:

上一篇文
Debian / Ubuntu 安裝 Docker 同 Docker Compose
下一篇文
Debian / Ubuntu 手動加 Swap,幫部細 VPS 頂住 Memory 壓力