Certbot


https://certbot.eff.org/

 

使用 Let's Encrypt(Certbot) 配置 HTTPS

今天又折騰了一下 Let's Encrypt,發現距離上次不久就已經忘了怎么做了。不得不再扒一遍文檔。 這次記錄下來。

上次折騰時,Let's Encrypt 還叫 Let's Encrypt ,這次已經改名為 Certbot 了。 並且上次是使用 git 下載,這次可以直接 wget 了。

  1. 安裝

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

    這個小工具會自動下載並安裝相關依賴和 Python 包。稍等一下就完成了。

  2. 生成證書

    生成證書過程中需要鑒權。有多種方式,比如 webroot 、 standalone 、 apache、 nginx 、 manual 等。我使用過前兩種。 這兩種中,簡單一點的是 standalone。不過,這種方式需要把現有的 WebServer 停掉,因為這種方式下 certbot 需要占用 80 端口。

    # ./certbot-auto certonly --text --agree-tos --email webmaster@example.com --standalone -d example.com -d www.example.com -d service.example.com

    -d 參數指定域名,可多個。一般第一個是主域名。

    webroot 方式稍微繁瑣一些,但好處是不需要關停現有的 WebServer 。此方法需要在域名對應的根目錄下新建 .well-known 目錄並寫入若干文件供驗證服務訪問。 因此需要配置 WebServer 允許外部訪問 http://example.com/.well-known 路徑。配置方法請參考相應 WebServer 的文檔。Nginx 的默認配置應該不用修改,Apache 就不知道了。 另外,不同的域名的根路徑可能不同,下面的例子中可以看到為不同的域名指定不同的根路徑。

    # ./certbot-auto certonly --text --agree-tos --email webmaster@excample.com --webroot -w /var/www/example -d example.com -d www.example.com -w /var/service/example -d service.ulefa.com

    無論使用那種方式,運行以上命令后都會在 /etc/letsencrypt 生成一堆東西,包括證書。

  3. 修改 WebServer 配置以提供 HTTPS 服務

    這里以 Nginx 作例子吧。

    1. 打開 Nginx 的配置文件(默認為: /etc/nginx/nginx.conf ),在需要提供 HTTPS 的 server 下新增以下三行,並把 listen 80; 刪掉:
    listen 443 ssl;
    ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem; ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;
    1. 新增以下 server 使所有 HTTP 請求都跳轉至 HTTPS :
    server {
    listen 80; server_name example.com www.example.com service.example.com; return 301 https://$host$request_uri; }
  4. 定期 renew

    Let's Encrypt 的證書有效期為 90 天,所以需要在到期前 renew 一下證書。 使用以下命令即可。

    # ./certbot-auto renew --text --agree-tos --email webmaster@excample.com --webroot -w /var/www/example -d example.com -d www.example.com -w /var/service/example -d service.ulefa.com

    或者直接運行以下命令,此時 certbot 會使用默認參數(此例為: /etc/letsencrypt/renewal/example.com.conf ):

    # ./certbot-auto renew

    又或者在 crontab 里加入定時任務,每隔 80 天的凌晨 4 點執行一次 renew:

    0 4 */80 * * /path/to/certbot-auto renew &>> /dev/null

參考: https://certbot.eff.org/docs/

啟用 HTTPS 之后,會增加一點對服務器計算資源的占用,但是這非常值得。營造安全干凈的網絡環境需要大家共同的努力。

 Except where otherwise noted, content on this site is licensed under aCreative Commons Attribution 4.0 International license .


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM