在Ubuntu上配置SSL證書,一般可按以下步驟進行:
更新系統軟件包并安裝必要的依賴,如OpenSSL:
sudo apt update
sudo apt install openssl
可使用Let’s Encrypt免費獲取,安裝Certbot工具:
sudo apt install certbot python3-certbot-nginx # 用于Nginx
sudo apt install certbot python3-certbot-apache # 用于Apache
然后運行Certbot獲取證書,以Nginx為例:
sudo certbot --nginx -d example.com -d www.example.com
/etc/nginx/sites-available/
。確保配置中包含監聽443端口、指定證書和私鑰文件路徑等內容,如:server {
listen 443 ssl;
server_name example.com;
ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;
# 其他配置...
}
修改后測試配置并重新加載Nginx:
sudo nginx -t
sudo systemctl reload nginx
/etc/apache2/sites-available/
。確保包含類似以下內容:<VirtualHost *:443>
ServerName example.com
SSLEngine on
SSLCertificateFile /etc/letsencrypt/live/example.com/fullchain.pem
SSLCertificateKeyFile /etc/letsencrypt/live/example.com/privkey.pem
# 其他配置...
</VirtualHost>
然后啟用站點并重啟Apache:
sudo a2ensite example.com.conf
sudo systemctl restart apache2
Let’s Encrypt證書有效期為90天,Certbot可自動續期??墒謩訙y試續期:
sudo certbot renew --dry-run
若要設置定時任務自動續期,可編輯crontab:
sudo crontab -e
添加如下內容(每天檢查兩次):
0 12 * * * /usr/bin/certbot renew --quiet
0 0 * * * /usr/bin/certbot renew --quiet