溫馨提示×

Debian LNMP如何配置SSL

小樊
49
2025-02-28 17:32:51
欄目: 云計算

在Debian系統上配置LNMP(Linux, Nginx, MySQL, PHP)以使用SSL證書,可以按照以下步驟進行:

1. 安裝Nginx

如果你還沒有安裝Nginx,可以使用以下命令進行安裝:

sudo apt update
sudo apt install nginx

2. 獲取SSL證書

你可以從Let’s Encrypt免費獲取SSL證書。使用Certbot工具可以簡化這個過程。

安裝Certbot

sudo apt install certbot python3-certbot-nginx

獲取證書

運行Certbot來獲取證書:

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

按照提示完成證書的獲取和配置。

3. 配置Nginx

Certbot會自動修改你的Nginx配置文件以啟用SSL。通常,配置文件位于/etc/nginx/sites-available/yourdomain.com。

你可以手動編輯這個文件來進一步自定義配置。以下是一個基本的SSL配置示例:

server {
    listen 80;
    server_name yourdomain.com www.yourdomain.com;
    return 301 https://$host$request_uri;
}

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

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

    root /var/www/html;
    index index.php index.html index.htm;

    location / {
        try_files $uri $uri/ =404;
    }

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

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

4. 重啟Nginx

保存配置文件后,重啟Nginx以應用更改:

sudo systemctl restart nginx

5. 驗證SSL配置

你可以使用以下命令來驗證你的SSL配置是否正確:

sudo nginx -t

6. 設置自動續期

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

sudo certbot renew --dry-run

如果一切正常,Certbot會自動續期證書并更新Nginx配置。

通過以上步驟,你就可以在Debian系統上成功配置LNMP以使用SSL證書了。

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