溫馨提示×

怎樣在Ubuntu上配置SSL證書

小樊
40
2025-09-07 12:26:37
欄目: 云計算

在Ubuntu上配置SSL證書,一般可按以下步驟進行:

準備工作

更新系統軟件包并安裝必要的依賴,如OpenSSL:

sudo apt update
sudo apt install openssl

獲取SSL證書

可使用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

配置Web服務器

  • Nginx:Certbot會自動修改配置文件,通常位于/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
  • Apache:Certbot會自動更新虛擬主機配置文件,一般在/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

0
亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女