溫馨提示×

Ubuntu LNMP如何配置SSL證書

小樊
73
2025-03-26 10:21:47
欄目: 云計算

在Ubuntu上配置LNMP(Linux, Nginx, MySQL, PHP)環境的SSL證書,通常使用Let’s Encrypt提供的免費SSL/TLS證書。以下是詳細步驟:

1. 安裝Certbot

Certbot是一個自動化工具,用于獲取和續訂Let’s Encrypt證書。

sudo apt update
sudo apt install certbot python3-certbot-nginx

2. 獲取SSL證書

使用Certbot獲取SSL證書。假設你的域名是example.com。

sudo certbot --nginx -d example.com -d www.example.com

Certbot會自動檢測Nginx配置文件,并提示你進行一些配置更改。按照提示操作即可。

3. 配置Nginx

Certbot會自動修改你的Nginx配置文件,通常位于/etc/nginx/sites-available/example.com-le-ssl.conf。確保以下內容正確:

server {
    listen 443 ssl;
    server_name example.com www.example.com;

    ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;
    include /etc/letsencrypt/options-ssl-nginx.conf;
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem;

    location / {
        root /var/www/html;
        index index.php index.html index.htm;
    }

    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/var/run/php/php7.4-fpm.sock; # 根據你的PHP版本調整
    }

    location ~ /\.ht {
        deny all;
    }
}

4. 啟用SSL配置

確保你的Nginx配置文件鏈接到sites-enabled目錄。

sudo ln -s /etc/nginx/sites-available/example.com-le-ssl.conf /etc/nginx/sites-enabled/

5. 測試Nginx配置

測試Nginx配置是否正確。

sudo nginx -t

如果沒有錯誤,重新加載Nginx。

sudo systemctl reload nginx

6. 自動續訂證書

Certbot會自動設置一個cron任務或systemd定時任務來定期續訂證書。你可以手動測試續訂過程:

sudo certbot renew --dry-run

如果沒有錯誤,證書將自動續訂并更新到/etc/letsencrypt/live/example.com/目錄。

7. 配置防火墻

確保你的防火墻允許443端口的流量。

sudo ufw allow 'Nginx Full'

8. 驗證SSL配置

使用瀏覽器訪問https://example.com,確保SSL證書正確安裝并且網站可以正常訪問。

通過以上步驟,你就可以在Ubuntu上成功配置LNMP環境的SSL證書。

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