免費(fèi)(Let's Encrypt )SSL證書(shū)ubuntu 16.04配置

20.04安裝Certbot

參考鏈接:https://www.digitalocean.com/community/tutorials/how-to-secure-nginx-with-let-s-encrypt-on-ubuntu-20-04

sudo apt install certbot python3-certbot-nginx

16.04安裝 Certbot

Certbot 其實(shí)就是維護(hù) Let's Encrypt 的 Package,在 Ubuntu 16.04 上,我們可以這樣安裝:

首先安裝 Nginx:

sudo apt-get install nginx

以上過(guò)程,等待安裝完畢就好。

然后添加 package repository

sudo add-apt-repository ppa:certbot/certbot

這個(gè)過(guò)程中,等待驗(yàn)證完畢,按下 ENTER 就好。然后更新 apt 源數(shù)據(jù):

sudo apt-get update

最后,安裝 Certbot 的 Nginx package:

sudo apt-get install python-certbot-nginx

配置Nginx

安裝完 Nginx 和 Certbot 之后,需要簡(jiǎn)單配置 Nginx 以便于 Let's Encrypt 能起作用:

sudo vi /etc/nginx/sites-available/default

使用 vi 編輯器打開(kāi) /etc/nginx/sites-available/default,可以直接刪除里面的所有內(nèi)容,然后再添加下面的配置:

server {    
  listen 80;    
  listen [::]:80;    
  server_name your-domain.com www.your-domain.com;
}

注意這里的 your-domain.com 換成你自己的域名。

保存退出之后,執(zhí)行以下命令來(lái)檢測(cè) Nginx 的配置文件是否有錯(cuò):

sudo nginx -t

如果出現(xiàn)類似 syntax ok 這樣的語(yǔ)句,就說(shuō)明 Nginx 的配置文件沒(méi)有問(wèn)題。之后就是重新加載 Nginx 的配置文件了:

sudo service nginx reload

簽發(fā) SSL 證書(shū)

前面的兩大步配置完成,就可以使用 Let's Encrypt 簽發(fā) SSL 證書(shū)了:

sudo certbot --nginx -d your-domian.com -d www.your-domain.com

注意這里的 your-domain.com 換成你自己的域名。
如果你第一次運(yùn)行 certbot 命令的話,你需要在彈出的窗口輸入你的郵箱地址還有需要接受 Let's Encrypt 的協(xié)議,這樣之后,你大概會(huì)看到下面的文字:

Please choose whether or not to redirect HTTP traffic to HTTPS, removing HTTP access.


1: No redirect - Make no further changes to the webserver configuration.
2: Redirect - Make all requests redirect to secure HTTPS access.
Choose this fornew sites, or if you're confident your site works on HTTPS.
You can undo this
change by editing your web server's configuration.


輸入郵箱后選agree,yes

Select the appropriate number [1-2] then [enter] (press 'c' to cancel):

在上面這里選擇 1 或者 2,我推薦大家直接選擇 2,因?yàn)檫@個(gè)會(huì)直接將你的 nginx 文件配置好并且是會(huì)將 http 跳轉(zhuǎn)到 https 的。

選擇完畢之后,等待 SSL 生成完畢,就會(huì)有類似這樣的輸出:

IMPORTANT NOTES:

  • Congratulations! Your certificate and chain have been saved at /etc/letsencrypt/live/your-domain.com/fullchain.pem. Your cert will expire on 2017-12-29. To obtain a new or tweaked version of this certificate in the future, simply run certbot again with the "certonly" option. To non-interactively renew all of your certificates, run "certbot renew"
  • Your account credentials have been saved in your Certbot configuration directory at etc/letsencrypt. You should make a secure backup of this folder now. This configuration directory will also contain certificates and private keys obtained by Certbot so making regular backups of this folder is ideal.
  • If you like Certbot, please consider supporting our work by:

Donating to ISRG / Let's Encrypt: https://letsencrypt.org/donate
Donating to EFF: https://eff.org/donate-le

然后在上面的文字中,這個(gè) /etc/letsencrypt/live/your-domain.com/fullchain.pem 路徑很重要,就是你的 SSL 證書(shū)路徑。

其實(shí)到這里,訪問(wèn) your-domain.com 應(yīng)該就可以看到 https 的效果了。

自動(dòng)更新證書(shū)

因?yàn)?Let's Encrypt 簽發(fā)的 SSL 證書(shū)有效期只有 90 天,所有在過(guò)期之前,我們需要自動(dòng)更新 SSL 證書(shū),而如果你使用最新的 certbot 的話,Let's Encrypt 會(huì)幫你添加自動(dòng)更新的腳本到 /etc/cron.d 里,你只需要去檢測(cè)一下這個(gè)命令是否生效就OK!

sudo certbot renew --dry-run

如果這個(gè)命令你沒(méi)看到什么 error 的話,那就是沒(méi)什么問(wèn)題了。

總結(jié)

本文主要是介紹了在 Ubuntu 16.04 的云服務(wù)器上,使用 Nginx 作為服務(wù)器軟件情況下,如何配置 Let's Encrypt 的 SSL 證書(shū),使得網(wǎng)站得以支持 https 的過(guò)程。內(nèi)容很淺,但是我覺(jué)得還是挺有用的。

本文源自:
https://mp.weixin.qq.com/s/x3eTsRwgtOuJ68BcU2HPyw

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容