商城首页欢迎来到中国正版软件门户

您的位置: 首页 > 文章列表 > 系统应用 > LNMP在Debian上如何配置SSL

LNMP在Debian上如何配置SSL

  发布于2026-08-21 阅读(0)

扫一扫,手机访问

在Debian系统上,要为LNMP(即Linux、Nginx、MySQL、PHP的组合)配置SSL,让其支持HTTPS,这需要经历几个特定的步骤。接下来,我将为你提供一个基础的指南,帮助你完成SSL证书的设置,并对Nginx进行相应配置,以实现HTTPS的正常使用。

LNMP在Debian上如何配置SSL

1. 安装Certbot

Certbot是一个自动化的工具,用于获取和续订Let’s Encrypt SSL证书。你可以使用以下命令安装Certbot:

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

2. 获取SSL证书

使用Certbot获取SSL证书。运行以下命令并按照提示操作:

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

Certbot会自动配置Nginx以使用SSL证书,并创建一个指向你的网站的符号链接。

3. 验证SSL配置

Certbot会自动检测你的Nginx配置文件是否存在语法错误。要是有错误,它会提示你去修复。当然,你也能手动测试Nginx配置文件的语法哦:

sudo nginx -t

如果没有错误,重新加载Nginx以应用更改:

sudo systemctl reload nginx

4. 配置自动续订

Certbot会自动设置一个cron作业来定期续订你的SSL证书。你可以手动测试续订过程以确保一切正常:

sudo certbot renew --dry-run

如果一切正常,Certbot会在证书到期前30天自动续订证书。

5. 配置防火墙

确保你的防火墙允许HTTPS流量。如果你使用的是ufw,可以运行以下命令:

sudo ufw allow 'Nginx Full'

6. 可选配置

  • HSTS(HTTP Strict Transport Security):强制浏览器始终通过HTTPS访问你的网站。

    add_header Strict-Transport-Security "max-age=31536000; includeSubDomains" always;
  • OCSP Stapling:提高SSL握手速度和安全性。

    ssl_stapling on;
    ssl_stapling_verify on;
    resolver 8.8.8.8 8.8.4.4 valid=300s;
    resolver_timeout 5s;

将上述配置添加到你的Nginx服务器块中,以启用这些可选功能。

示例Nginx服务器块配置

以下是一个基本的Nginx服务器块配置示例,包括SSL设置:

server {
listen 80;
server_name yourdomain.com www.yourdomain.com;

location /.well-known/acme-challenge/ {
root /var/www/certbot;
}

location / {
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;

add_header Strict-Transport-Security "max-age=31536000; includeSubDomains" always;
ssl_stapling on;
ssl_stapling_verify on;
resolver 8.8.8.8 8.8.4.4 valid=300s;
resolver_timeout 5s;

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;
}

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

请根据你的实际情况调整配置。

完成这些步骤后,你的Debian服务器上的LNMP环境应该已经成功配置了SSL。

本文转载于:https://www.yisu.com/ask/26752868.html 如有侵犯,请联系zhengruancom@outlook.com删除。
免责声明:正软商城发布此文仅为传递信息,不代表正软商城认同其观点或证实其描述。

热门关注