配置證書
1. 更新系統和安裝必要的軟件包
首先,確保系統和包管理器是最新的:
sudo dnf update -y
sudo dnf install -y python3 python3-pip python3-virtualenv httpd mod_ssl
2. 創建并激活虛擬環境
為了避免依賴沖突,使用virtualenv創建一個隔離的Python環境:
python3 -m venv myenv
source myenv/bin/activate
3. 安裝certbot和Apache插件
在虛擬環境中安裝certbot和certbot-apache插件:
pip install certbot certbot-apache
4. 配置Apache
確保Apache已經安裝并運行:
sudo systemctl enable httpd --now
5. 使用certbot獲取SSL證書
運行certbot命令以獲取SSL證書。請將yourdomain.com
替換為您的實際域名:
sudo myenv/bin/certbot --apache -d yourdomain.com -d www.yourdomain.com
certbot將自動配置Apache以使用獲取的SSL證書。
6. 驗證SSL證書
驗證SSL證書是否已正確安裝:
sudo myenv/bin/certbot certificates
7. 配置自動更新
為了確保SSL證書自動更新,設置一個cron job:
sudo crontab -e
添加以下行來每天檢查并更新證書:
0 2 * * * /path/to/myenv/bin/certbot renew --quiet --deploy-hook "systemctl reload httpd"
確保將/path/to/myenv
替換為您的虛擬環境的實際路徑。
8. 重新啟動Apache以應用更改
確保所有更改都已正確應用并重新啟動Apache:
sudo systemctl reload httpd
9. 驗證自動更新
您可以手動運行以下命令以測試自動更新過程:
sudo /home/ec2-user/myenv/bin/certbot renew --dry-run
這將模擬續訂過程而不會實際更改證書,以確保一切正常工作。
報錯
(myenv) [ec2-user@ip-172-31-29-84 ~]$ sudo myenv/bin/certbot --apache -d yourdomain.com -d www.yourdomain.com
Saving debug log to /var/log/letsencrypt/letsencrypt.log
The apache plugin is not working; there may be problems with your existing configuration.
The error was: NoInstallationError('Problem in Augeas installation')
解決Augeas依賴問題
由于Augeas安裝問題導致Apache插件無法工作,您可以嘗試以下步驟來解決這個問題。
安裝Augeas依賴
首先,確保Augeas庫已安裝在系統上:
sudo dnf install augeas-libs augeas -y
然后再嘗試重新運行certbot命令。
確認Augeas庫路徑
如果Augeas依賴安裝后問題依舊,可以嘗試手動設置Augeas庫路徑。
使用–webroot方法生成SSL證書
如果Augeas問題仍然存在,可以嘗試使用--webroot
方法來獲取SSL證書。
確定您的webroot路徑
您的webroot路徑是您網站文件的根目錄,例如/var/www/html
。確保您的Apache配置指向該目錄。
獲取SSL證書
使用以下命令來獲取SSL證書:
sudo myenv/bin/certbot certonly --webroot -w /var/www/html -d yourdomain.com -d www.yourdomain.com(myenv) [ec2-user@ip-172-31-24-7 ~]$ sudo myenv/bin/certbot certonly --webroot -w /var/www/html -d xxxxxx.asia -d www.xxxxxx.asia
Saving debug log to /var/log/letsencrypt/letsencrypt.log
Enter email address (used for urgent renewal and security notices)(Enter 'c' to cancel): 郵箱地址- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Please read the Terms of Service at
https://letsencrypt.org/documents/LE-SA-v1.4-April-3-2024.pdf. You must agree in
order to register with the ACME server. Do you agree?
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
(Y)es/(N)o: Y- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Would you be willing, once your first certificate is successfully issued, to
share your email address with the Electronic Frontier Foundation, a founding
partner of the Let's Encrypt project and the non-profit organization that
develops Certbot? We'd like to send you email about our work encrypting the web,
EFF news, campaigns, and ways to support digital freedom.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
(Y)es/(N)o: Y
Account registered.
Requesting a certificate for xxxxxx.asia and www.xxxxxx.asiaSaving debug log to /var/log/letsencrypt/letsencrypt.log
Requesting a certificate for xxxxxx.asia and www.xxxxxx.asiaSuccessfully received certificate.
Certificate is saved at: /etc/letsencrypt/live/xxxxxx.asia/fullchain.pem
Key is saved at: /etc/letsencrypt/live/xxxxxx.asia/privkey.pem
This certificate expires on 2024-09-05.
These files will be updated when the certificate renews.NEXT STEPS:
- The certificate will need to be renewed before it expires. Certbot can automatically renew the certificate in the background, but you may need to take steps to enable that functionality. See https://certbot.org/renewal-setup for instructions.- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
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
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
配置Apache以使用SSL證書
獲取SSL證書后,您需要手動編輯Apache配置以使用這些證書。
編輯Apache配置
打開您的Apache配置文件,通常位于/etc/httpd/conf.d/ssl.conf
或您的虛擬主機配置文件中。
添加或修改以下行以指向您的SSL證書和密鑰文件:
<VirtualHost *:443>ServerName yourdomain.comServerAlias www.yourdomain.comDocumentRoot /var/www/htmlSSLEngine onSSLCertificateFile /etc/letsencrypt/live/yourdomain.com/fullchain.pemSSLCertificateKeyFile /etc/letsencrypt/live/yourdomain.com/privkey.pemSSLCertificateChainFile /etc/letsencrypt/live/yourdomain.com/chain.pem<Directory /var/www/html>Options Indexes FollowSymLinksAllowOverride AllRequire all granted</Directory>
</VirtualHost>
確保將yourdomain.com
替換為您的實際域名。
重新加載Apache
sudo systemctl reload httpd
驗證SSL證書
您可以使用以下命令來驗證證書是否已正確安裝:
sudo myenv/bin/certbot certificates
配置自動更新
為了確保SSL證書自動更新,您可以設置一個cron job:
-
編輯cron配置
sudo crontab -e
-
添加以下行來每天檢查并更新證書
0 2 * * * /home/ec2-user/myenv/bin/certbot renew --quiet --deploy-hook "systemctl reload httpd"
確保將/home/ec2-user/myenv
替換為您的虛擬環境的實際路徑。
10. AWS添加安全組HTTPS端口443
下載證書文件
Certbot生成的證書文件通常存儲在 /etc/letsencrypt/live/yourdomain.com/
目錄中,其中 yourdomain.com
是您的實際域名。您可以通過以下步驟下載這些文件:
-
找到證書文件路徑
Certbot生成的證書文件通常包括以下幾個主要文件:
privkey.pem
:私鑰文件fullchain.pem
:完整的證書鏈文件(包括域名證書和中間證書)cert.pem
:域名證書文件chain.pem
:中間證書文件
您可以使用以下命令列出所有證書及其路徑:
sudo myenv/bin/certbot certificates
-
復制證書文件
使用
scp
(Secure Copy Protocol)或rsync
等工具將證書文件從服務器復制到您的本地機器。例如,使用
scp
:scp ec2-user@your-ec2-ip:/etc/letsencrypt/live/yourdomain.com/privkey.pem /path/to/local/directory/ scp ec2-user@your-ec2-ip:/etc/letsencrypt/live/yourdomain.com/fullchain.pem /path/to/local/directory/
確保將
ec2-user@your-ec2-ip
替換為您的實際服務器用戶名和IP地址,將/path/to/local/directory/
替換為您本地機器上的目標目錄。 -
設置適當的權限
下載到本地機器后,確保這些文件具有適當的權限。特別是私鑰文件,應該僅對您自己可讀:
chmod 600 /path/to/local/directory/privkey.pem
示例步驟
以下是一個完整的示例,假設您要將證書文件下載到本地目錄 /home/user/certificates/
:
-
連接到您的EC2實例
ssh ec2-user@your-ec2-ip
-
查找證書文件路徑
sudo myenv/bin/certbot certificates
-
從服務器復制證書文件到本地機器
在本地終端上運行:
scp ec2-user@your-ec2-ip:/etc/letsencrypt/live/yourdomain.com/privkey.pem /home/user/certificates/ scp ec2-user@your-ec2-ip:/etc/letsencrypt/live/yourdomain.com/fullchain.pem /home/user/certificates/
-
設置適當的權限
chmod 600 /home/user/certificates/privkey.pem
刪除證書
-
列出所有證書
首先,您可以列出所有已經管理的證書:
sudo myenv/bin/certbot certificates
這將顯示所有已安裝的證書及其詳細信息。
-
刪除特定證書
使用以下命令刪除特定證書:
sudo myenv/bin/certbot delete --cert-name xxxxxx.asia
確保將
xxxxxx.asia
替換為您想要刪除的證書名稱。
更新Apache配置
刪除證書后,您需要更新Apache配置以移除對已刪除證書的引用。
-
編輯Apache配置文件
打開您的Apache配置文件,通常位于
/etc/httpd/conf.d/ssl.conf
或您的虛擬主機配置文件中:sudo vim /etc/httpd/conf.d/ssl.conf
-
移除或修改以下行
移除或修改指向已刪除證書文件的配置行:
SSLCertificateFile /etc/letsencrypt/live/xxxxxx.asia/fullchain.pem SSLCertificateKeyFile /etc/letsencrypt/live/xxxxxx.asia/privkey.pem SSLCertificateChainFile /etc/letsencrypt/live/xxxxxx.asia/chain.pem
-
重新加載Apache
保存文件并退出編輯器,然后重新加載Apache:
sudo systemctl reload httpd
驗證刪除
確保證書已成功刪除,您可以再次使用以下命令確認:
sudo myenv/bin/certbot certificates
確認已刪除的證書不再列出。