Centos 7 使用 certbot 安裝Let’s Encrypt SSL 證書

Centos 7 使用 certbot 安裝Let’s Encrypt SSL 證書

安裝環境

  • CentOS7 主機
  • Nginx

主要步驟

  • 安裝客戶端
#如果你已經安裝了EPEL,這個步驟可以省略
$ sudo yum install epel-release
#安裝certbot客戶端
$ sudo yum install certbot
  • 使用certbot嚮導安裝,有界面,根據指示一步步操作就行,但是YCHEUNG進行到設置webroot的時候就error了,只好放棄
$certbot certonly


如果失敗,可以自行使用命令行安裝(往下看)

  • 使用命令行安裝

即,用 certbot certonly命令,加上參數 --webroot表示使用webroot的方式安裝,或者你也可以使用--standalone方式安裝,standalone安裝需要使用80端口,也就是說一般你的站點需要暫停運行一段時間,不太友好。webroot則是它會生成一些文件到你的網站目錄下,然後訪問看驗證是否存在,所以使用這種方式的話,你需要在 nginx 裡面做設置,使它生成的驗證文件能夠被訪問。

  • NGINX設置

在這個站點的nginx配置文件中的 server block尾部加入:

server{
    ...    
    location ^~ /.well-known/acme-challenge/ {
        default_type "text/plain";
    }

    location = /.well-known/acme-challenge/ {
        return 404;
    }
}

然後重啟或者重新加載nginx: service nginx reload

之後再運行certbot命令:

$ certbot certonly --webroot -w /var/www/example -d example.com -d www.example.com -w /var/www/thing -d thing.is -d m.thing.is

-w後面指定你網站所在的根目錄, -d後面指定一個網站域名,如果多個域名指向同一個根目錄,可以再加一個 -d

  • 運行結果

成功!!
certbot

證書拿到了以後...

  • 更改nginx配置,把來自HTTP的訪問都跳轉去HTTPS
  • 證書有效期大約3個月,所以還要寫一個定時任務去更新正書

更新證書

可以寫定時任務更新, certbot renew --quiet ,也可以直接執行以下命令:

certbot renew 

刪除證書

輸入命令後,會給出證書列表供選擇刪除。

certbot delete

參考

Y Cheung

Y Cheung

Blogger, Programer & Traveler.
Shanghai