如何在AWS的Amazon-Linux主機上透過certbot配置Let's Encrypt免費且自動續啟用的SSL安全憑證

新的一年一樣貫徹 I code, therefore I am.

每天進步1%,持續一年你能獲得37倍成長

繼續鑽研更深一層的Infra 和 SRE技能

記得在三年前因接案所以配置過certbot免費的SSL憑證筆記
是針對佈署在Nginx上的NodeJs網站應用

如何在Digital Ocean的ubuntu主機上透過certbot配置Let's Encrypt免費且自動續啟用的SSL安全憑證

這次示範在AWS的EC2 (OS: Amazon-Linux)上配置是針對Apache上的asp.net core網站應用


Step1.下載EPEL
為了安裝CertBot,我們必須在 Amazon Linux 2中啟用EPEL儲存庫。
1
sudo amazon-linux-extras install epel



安裝並啟用 EPEL 後,就能透過 yum 安裝 CertBot

Step2.安裝CertBot
1
sudo yum install certbot




Step3.安裝 CertBot 的 Apache 插件
1
sudo yum install python-certbot-apache







Step4. 列出使用certbot指令安裝的插件,做double check
確保有顯示Apache Web Server 插件
1
sudo certbot plugins




Saving debug log to /var/log/letsencrypt/letsencrypt.log

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
* apache
Description: Apache Web Server plugin
Interfaces: IAuthenticator, IInstaller, IPlugin
Entry point: apache = certbot_apache._internal.entrypoint:ENTRYPOINT

* standalone
Description: Spin up a temporary webserver
Interfaces: IAuthenticator, IPlugin
Entry point: standalone = certbot._internal.plugins.standalone:Authenticator

* webroot
Description: Place files in webroot directory
Interfaces: IAuthenticator, IPlugin
Entry point: webroot = certbot._internal.plugins.webroot:Authenticator
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -


Step5.在向 Lets Encrypt 請求 SSL 憑證之前,需要確保 Apache 針對VirtualHost的設置有完善。
有經驗的老手通常會知道 Apache 的配置檔案
對於 Debian/Ubuntu 系統,預設位置在/etc/apache2/
對於 CentOS/RHEL 系統,預設位置在/etc/httpd/


如果對Apache配置不熟悉的夥伴們可以參考 第25個冬天之前blog
Linux Ubuntu Apache上的管理配置筆記

此外在 Apache 伺服器中,你可以使用 apachectl -S命令來查看 Apache 正在使用哪個配置檔案。這個命令會顯示 Apache 的主配置檔案路徑,以及任何包含或引用的其他配置檔案。




Step6.請求證書
1
certbot certonly --apache

下一步就是透過 CertBot 請求 SSL 證書

在此我要針對domain name: www.marblesrace.top 配置
所以輸入編號:1
enter之後接續下去

從命令輸出可明確得知

我的 SSL 憑證位於
/etc/letsencrypt/live/www.marblesrace.top/fullchain.pem

金鑰檔位於
/etc/letsencrypt/live/www.marblesrace.top/privkey.pem

憑證將於2024年4月1日到期


Step7. 針對443 VirtualHost配置檔設置 SSL 憑證路徑



更新virtualhost兩種配置撰寫模板

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
<VirtualHost *:*>
	RequestHeader set "X-Forwarded-Proto" expr=%{REQUEST_SCHEME}
</VirtualHost>
<VirtualHost *:80>
	ProxyPreserveHost On
	ProxyPass / http://127.0.0.1:9487/
	ProxyPassReverse / http://127.0.0.1:9487/
	ServerName www.marblesrace.top
	ServerAlias *.marblesrace.top
    	ErrorLog /var/www/error.log
    	CustomLog /var/www/requests.log common	
	RewriteEngine On
    	RewriteCond %{SERVER_NAME} =*.marblesrace.top [OR]
	RewriteCond %{SERVER_NAME} =marblesrace.top
	RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
</VirtualHost>
<IfModule mod_ssl.c>
<VirtualHost *:443>
	ProxyPreserveHost On
	ProxyPass / http://127.0.0.1:9487/
	ProxyPassReverse / http://127.0.0.1:9487/
	ServerName www.marblesrace.top
	ServerAlias *.marblesrace.top
    	ErrorLog /var/www/error.log
    	CustomLog /var/www/requests.log common	
	SSLEngine on
    	SSLCertificateFile /etc/letsencrypt/live/www.marblesrace.top/fullchain.pem
    	SSLCertificateKeyFile /etc/letsencrypt/live/www.marblesrace.top/privkey.pem
</VirtualHost>
</IfModule>



sudo systemctl restart httpd

Step8.記得確認你的apache httpd監聽port不是只有默認的80喔,也要有443!
sudo systemctl restart httpd
sudo ss -tulnp | grep httpd

這部分要記得去
/etc/httpd/conf/httpd.conf中配置好Listen






每次有更新過config都記得請崇Load或restart 你的apache httpd服務喔!


套用完SSL憑證後發現時效沒過也有吃到憑證設定
但仍會顯示不安全.....

在chrome跟Opera瀏覽器皆有此尷尬狀況



curl -I https://marblesrace.top 下指令檢測一下

這個錯誤信息表明,當使用 curl 命令訪問 https://marblesrace.top 時,SSL 證書的主題名稱(Subject Name)與你嘗試訪問的域名不匹配。


證書未涵蓋該域名,SSL 證書僅對特定的域名有效。
若目前的證書是為 www.marblesrace.top 頒發的,但沒有包括裸域 marblesrace.top,那麼訪問後者時會出現錯誤。

可以使用以下命令來生成包括多個域名的證書
sudo certbot --apache -d marblesrace.top -d www.marblesrace.top







DNS 正常了



openssl x509 -in /etc/letsencrypt/live/www.marblesrace.top/fullchain.pem -noout -dates

openssl x509 -in /etc/letsencrypt/live/www.marblesrace.top/fullchain.pem -noout -text | grep "CN="







Let's Encrypt 的证书被 Chrome 提示为不安全是什么原因?
https://v2ex.com/t/709542
網站連線不安全?SSL 安全憑證懶人包!
https://wpointer.com/wordpress-tutorials/newbie/how-to-add-ssl/
放弃Let's Encrypt证书,全站更换ZeroSSL证书
https://ffis.me/archives/2110.html

How to Install & Configure SSL Certificates on Apache with Mod_SSL
https://www.sectigo.com/resource-library/install-certificates-apache-with-mod-ssl
How To Enable mod_ssl in Apache
https://ubiq.co/tech-blog/enable-mod_ssl-apache/

How to install mod_ssl on RHEL 8 / CentOS 8 with httpd Apache webserver
https://linuxconfig.org/how-to-install-mod-ssl-on-redhat-8


留言

這個網誌中的熱門文章

經得起原始碼資安弱點掃描的程式設計習慣培養(五)_Missing HSTS Header

經得起原始碼資安弱點掃描的程式設計習慣培養(三)_7.Cross Site Scripting(XSS)_Stored XSS_Reflected XSS All Clients

(2021年度)駕訓學科筆試準備題庫歸納分析_法規是非題