在本教程中,我們將向您展示如何使用 Let's Encrypt 安裝 ApacheUbuntuUbuntu 22.04 LTS 上的 22.04。 對於那些不知道的人,Let's Encrypt 是一個非營利性的證書頒發機構,它提供證書,以便您的網站可以使用安全連接。證書機器人是一個開源軟件工具,用於集成和管理網絡上的 Let's Encrypt 證書以啟用安全的 HTTPS 協議。 它將自動為您管理 Let's Encrypt 證書。
本文假設您至少對 Linux 有基本的了解,知道如何使用 shell,最重要的是,將您的網站託管在自己的 VPS 上。 安裝非常簡單,假設您在 root 帳戶下運行,如果不是,您可能需要添加 ' sudo
' 到命令以獲得 root 權限。 我將向您展示在 Ubuntu 22.04 (Jammy Jellyfish) 上使用 Let's Encrypt 在 Ubuntu 22.04 上逐步安裝 Apache。 對於 Ubuntu 22.04 和任何其他發行版,您可以遵循相同的說明Debian像 Linux Mint、Elementary OS、Pop!_OS 等等。
以前的要求
- 運行以下操作系統之一的服務器:Ubuntu 22.04、20.04 和任何其他基於 Debian 的發行版,例如 Linux Mint。
- 建議您使用全新的操作系統安裝以避免潛在問題。
- 對服務器的 SSH 訪問(或者如果您在桌面上,則只需打開終端)。
- 一個
non-root sudo user
或訪問root user
. 我們建議充當non-root sudo user
但是,如果您在以 root 身份操作時不小心,它可能會損害您的系統。
在 Ubuntu 22.04 LTS Jammy Jellyfish 上使用 Let's Encrypt 安裝 Apache
步驟 1. 首先,通過運行以下命令確保所有系統包都是最新的 apt
終端中的命令。
sudo apt update sudo apt upgrade
步驟 2. 在 Ubuntu 22.04 上安裝 Apache HTTP 服務器。
默認情況下,Apache 在 Ubuntu 22.04 基礎存儲庫中可用。 現在運行以下命令將最新版本的 Apache 安裝到您的 Ubuntu 系統:
sudo apt install apache2
安裝成功後,啟用Apache(在系統啟動時自動啟動),啟動並使用以下命令檢查狀態:
sudo systemctl enable apache2 sudo systemctl start apache2 sudo systemctl status apache2
您可以使用以下命令確認 Apache2 的版本:
apache2 -v
步驟 3. 配置防火牆。
現在我們使用 Apache 配置 UFW 防火牆,以允許公共訪問 HTTP 和 HTTPS 的默認 Web 端口:
sudo ufw allow OpenSSH sudo ufw allow 'Apache Full' sudo ufw enable
步驟 4. 訪問 Apache Web 服務器。
成功安裝後,在系統上打開 Web 瀏覽器並在地址欄中輸入服務器 IP。 您將獲得默認的 Apache 服務器頁面:
步驟 5. 創建 Apache 虛擬主機。
首先,創建一個根目錄來存儲您的網站文件:
sudo mkdir -p /var/www/html/domain.com/
然後更改目錄的所有權和組:
sudo chown -R www-data:www-data /var/www/html/domain.com/
之後,我們創建一個 Apache 虛擬主機來為網站的 HTTP 版本提供服務:
sudo nano /etc/apache2/sites-available/www.domain.com.conf
添加以下文件:
<VirtualHost *:80> ServerName domain.com ServerAlias www.domain.com ServerAdmin [email protected] DocumentRoot /var/www/html/www.domain.com ErrorLog ${APACHE_LOG_DIR}/www.domain.com_error.log CustomLog ${APACHE_LOG_DIR}/www.domain.com_access.log combined <Directory /var/www/html/www.domain.com> Options FollowSymlinks AllowOverride All Require all granted </Directory> </VirtualHost>
保存並關閉文件,然後重新啟動 Apache 網絡服務器以使更改生效:
sudo a2ensite www.domain.com.conf sudo a2enmod ssl rewrite sudo systemctl restart apache2
步驟 6. 在 Ubuntu 22.04 上使用 Let's Encrypt 保護 Apache。
首先,您需要安裝 Certbot 以使用 Let's Encrypt 獲取 SSL 證書:
sudo apt install certbot python3-certbot-apache
接下來,按照以下步驟使用 Let's Encrypt 獲取您的 SSL 證書:
sudo certbot --apache
您將需要按照交互式提示安裝證書。 由於我有兩個域,我將為這兩個域安裝 SSL 證書:
Saving debug log to /var/log/letsencrypt/letsencrypt.log Enter email address (used for urgent renewal and security notices) (Enter 'c' to cancel): [email protected] - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Please read the Terms of Service at https://letsencrypt.org/documents/LE-SA-v1.2-November-15-2017.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: N Account registered. Which names would you like to activate HTTPS for? - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 1: domain.com 2: www.domain.com - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Select the appropriate numbers separated by commas and/or spaces, or leave input blank to select all options shown (Enter 'c' to cancel): 1,2 Requesting a certificate for domain.com and www.domain.com Successfully received certificate. Certificate is saved at: /etc/letsencrypt/live/domain.com/fullchain.pem Key is saved at: /etc/letsencrypt/live/domain.com/privkey.pem This certificate expires on 2022-12-10. These files will be updated when the certificate renews. Certbot has set up a scheduled task to automatically renew this certificate in the background. Deploying certificate Successfully deployed certificate for domain.com to /etc/apache2/sites-available/www.domain.com-le-ssl.conf Successfully deployed certificate for www.domain.com to /etc/apache2/sites-available/www.domain.com-le-ssl.conf Congratulations! You have successfully enabled HTTPS on https://domain.com and https://www.domain.com - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 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 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
步驟 7. 自動 SSL 更新。
Let's Encrypt 證書的有效期為 90 天,強烈建議在證書到期前更新證書。 您可以通過運行以下命令來測試證書的自動續訂:
sudo certbot renew --dry-run
生產:
Saving debug log to /var/log/letsencrypt/letsencrypt.log - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Processing /etc/letsencrypt/renewal/domain.com.conf - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Account registered. Simulating renewal of an existing certificate for domain.com and www.domain.com - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Congratulations, all simulated renewals succeeded: /etc/letsencrypt/live/domain.com/fullchain.pem (success) - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
步驟 8. 測試 SSL。
使用 Let's Encrypt SSL 成功完成 Apache Secure 後,現在轉到ssllabs.com/ssltest/並在您的域上運行 SSL 測試:
恭喜! 您已在 Ubuntu 22.04 上使用 Let's Encrypt 成功配置了 Apache。 感謝您使用本教程在 Ubuntu 22.04 LTS Jammy Jellyfish 系統上使用 Let's Encrypt TLS/SSL 安裝 Apache。 如需更多幫助或有用信息,我們建議您參考阿帕奇官方網站.