centos7 下安裝配置 Let’s Encrypt

Let’s Encrypt

若是要啓用 HTTPS,咱們須要一個 CA 證書,Let's Encrypt 是一個免費的證書頒發機構,由 ISRG(Internet Security Research Group)運做。html

使用 Certbot 獲取證書

Certbot 是 Let's Encrypt 官方推薦的證書獲取工具,它能夠幫助很方便的獲取和更新 Let's Encrypt 證書,Certbot 支持全部 Unix 內核的操做系統。python

安裝 Certbot 客戶端

yum install -y epel-release
yum install -y certbot

注: 安裝和執行 certbot 過程當中,可能會報一些錯誤,這些錯誤主要是使用到的一些 python 庫版本不匹配問題,根據錯誤提示更新安裝相應版本的庫便可。nginx

獲取證書

由於使用 Certbot 獲取證書時,Let's Encrypt 服務器會訪問 http://sub.domain.com/.well-known 來驗證你的域名服務器,所以須要修改 nginx 配置文件,配置 .well-known 指向本地一個目錄:web

server {
    
    ......

    location /.well-known {
        alias /usr/local/nginx/html/.well-known;
    }

    ......
}

而後就可使用 certbot 命令來獲取證書了,獲取證書時須要輸入你的Email並接受用戶條款。須要注意:-w 指定的 web 目錄須要和前邊 nginx 配置的 .well-known 的本地目錄一致(/usr/local/nginx/html):apache

certbot certonly --webroot -w /usr/local/nginx/html/ -d sub.domain.com -m xxxxxx@xxx.com --agree-tos
  • -w 指定 webroot 目錄
  • -d domain 想要獲取的證書域名,支持多個域名

可是有些時候咱們的一些服務並無根目錄,例如一些微服務,這時候使用 --webroot 就走不通了。certbot 還有另一種模式 --standalone,這種模式不須要指定網站根目錄,他會自動啓用服務器的443端口,來驗證域名的歸屬。咱們有其餘服務(例如nginx)佔用了443端口,就必須先中止這些服務,在證書生成完畢後,再啓用。瀏覽器

certbot certonly --standalone -d sub.domain.com -m xxxxxx@xxx.com --agree-tos

若是成功獲取證書,你的密鑰和證書存放在 /etc/letsencrypt/live/sub.domain.com/ 目錄:tomcat

ll /etc/letsencrypt/live/sub.domain.com/

cert.pem -> ../../archive/sub.domain.com/cert1.pem
chain.pem -> ../../archive/sub.domain.com/chain1.pem
fullchain.pem -> ../../archive/sub.domain.com/fullchain1.pem
privkey.pem -> ../../archive/sub.domain.com/privkey1.pem
文件名 說明
cert.pem 服務端證書
chain.pem 瀏覽器須要的全部證書但不包括服務端證書,好比根證書和中間證書
fullchain.pem 包括了cert.pem和chain.pem的內容
privkey.pem 證書的私鑰

刪除證書

有時須要刪除已生成的證書,從新生成。可以使用以下命令進行刪除:服務器

certbot delete --cert-name sub.domain.com

生成 dhparam

nginx 配置 https 時,須要 dhparam,使用以下命令進行生成:session

openssl dhparam -out /etc/nginx/sites-enabled/dh4096.pem 4096

配置 HTTPS

配置 nginx

server {
    listen 80;
    server_name sub.domain.com;
    rewrite ^ https://$server_name$request_uri? permanent;
}

server {
    listen 443 ssl;

    server_name sub.domain.com;

    include /etc/nginx/sites-enabled/sub.domain.com.ssl;

    location / { try_files $uri @proxy_to_app; }
    location @proxy_to_app {
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $http_host;
        proxy_set_header X-Forwarded-Proto https;
        proxy_redirect off;
        proxy_pass http://127.0.0.1:8080;
    }
}

sub.domain.com.ssl 文件配置內容:app

ssl on;
ssl_certificate /etc/letsencrypt/live/sub.domain.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/sub.domain.com/privkey.pem;
ssl_prefer_server_ciphers on;
ssl_dhparam /etc/nginx/sites-enabled/dhparam.pem;
ssl_ciphers HIGH:!ADH:!MD5:!aNULL:!eNULL:!MEDIUM:!LOW:!EXP:!kEDH;
ssl_protocols TLSv1.2 TLSv1.1 TLSv1;
ssl_session_timeout 1d;
ssl_session_cache shared:SSL:50m;
ssl_stapling on;
ssl_stapling_verify on;
add_header Strict-Transport-Security max-age=15768000;

配置 tomcat

配置 tomcat 支持 https,有兩處須要修改:

  • 在 Connector 節點增長 proxyPort="443"

    <Connector port="8080" protocol="HTTP/1.1"
          connectionTimeout="20000"
          redirectPort="8443" proxyPort="443" />
  • 添加以下 Value 節點

    <Valve className="org.apache.catalina.valves.RemoteIpValve"
          remoteIpHeader="x-forwarded-for"
          remoteIpProxiesHeader="x-forwarded-by"
          protocolHeader="x-forwarded-proto" />

測試

啓動 nginx 或從新載入配置,使用瀏覽器打開 https://sub.domain.com/

service nginx reload

可使用在線工具 https://www.ssllabs.com/ssltest 來檢測證書狀況。

自動更新證書

Let's Encrypt 證書只有 90 天有效期,咱們須要在證書到期以前更新證書,certbot 提供了相應的命令 certbot renew。

/usr/bin/certbot renew --dry-run

能夠將此更新命令添加到計劃任務中,certbot renew 命令只會更新還有 30 天才會到期的證書,因此咱們能夠每隔 2 個月在凌晨3:30執行一次更新操做便可,建立一個新文件 certbot-auto-renew-cron,寫入 cron 計劃內容:

30 3 * */2 * /usr/bin/certbot renew --post-hook "service nginx restart" --quiet >> /var/log/cerbot.log

--pre-hook 這個參數表示執行更新操做以前要作的事情

--post-hook 這個參數表示執行更新操做完成後要作的事情

啓動 crontab 定時任務

crontab certbot-auto-renew-cron
相關文章
相關標籤/搜索