温馨提示×

LNMP在Debian上如何部署SSL

小樊
43
2026-01-03 09:25:45
栏目: 云计算

在Debian上部署LNMP(Linux, Nginx, MySQL, PHP)并配置SSL证书,可以按照以下步骤进行:

1. 安装Nginx

首先,确保你的系统是最新的:

sudo apt update
sudo apt upgrade

然后安装Nginx:

sudo apt install nginx

2. 安装MySQL

安装MySQL服务器:

sudo apt install mysql-server

启动并启用MySQL服务:

sudo systemctl start mysql
sudo systemctl enable mysql

3. 安装PHP

安装PHP及其常用扩展:

sudo apt install php-fpm php-mysql php-curl php-gd php-mbstring php-xml php-zip

启动并启用PHP-FPM服务:

sudo systemctl start php7.4-fpm
sudo systemctl enable php7.4-fpm

4. 配置Nginx以使用SSL

首先,获取SSL证书。你可以使用Let’s Encrypt免费获取证书:

sudo apt install certbot python3-certbot-nginx

运行Certbot来获取并配置SSL证书:

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

按照提示完成证书的获取和配置。Certbot会自动修改Nginx配置文件以支持HTTPS。

5. 验证SSL配置

确保Nginx配置文件正确无误。编辑Nginx配置文件(通常位于/etc/nginx/sites-available/yourdomain.com):

sudo nano /etc/nginx/sites-available/yourdomain.com

确保配置文件中有以下内容:

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/yourdomain.com;
    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;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }

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

保存并退出编辑器,然后测试Nginx配置:

sudo nginx -t

如果没有错误,重新加载Nginx:

sudo systemctl reload nginx

6. 配置防火墙

确保防火墙允许HTTP和HTTPS流量:

sudo ufw allow 'Nginx Full'

7. 测试SSL连接

打开浏览器,访问https://yourdomain.com,确保SSL证书正确安装并且网站可以正常访问。

通过以上步骤,你就可以在Debian上成功部署LNMP并配置SSL证书了。

0