温馨提示×

debian lnmp如何加密

小樊
47
2026-07-30 08:23:23
栏目: 网络安全

Debian + LNMP(Linux + Nginx + MySQL/MariaDB + PHP) 环境下,“加密”通常指 HTTPS(SSL/TLS)加密网站流量。下面按最常见、最推荐的方式一步步说明。


一、最常见方案:使用 Let’s Encrypt 免费证书(推荐)

1️⃣ 安装 Certbot(Let’s Encrypt 官方工具)

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

2️⃣ 确认域名解析正确

确保你的域名(如 example.com已解析到服务器公网 IP

ping example.com

3️⃣ 自动申请并配置 HTTPS(最简单)

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

✅ Certbot 会自动:

  • 申请证书
  • 修改 Nginx 配置
  • 设置 HTTP 自动跳转 HTTPS
  • 配置自动续期

4️⃣ 测试 HTTPS 是否生效

浏览器访问:

https://example.com

或使用:

curl -I https://example.com

5️⃣ 自动续期验证(非常重要)

Let’s Encrypt 证书 90 天有效,Debian 已自动配置 cron / systemd timer:

sudo systemctl status certbot.timer

手动测试续期:

sudo certbot renew --dry-run

二、手动配置 SSL(不使用 Certbot)

1️⃣ 准备证书文件

假设你已有:

  • example.com.crt
  • example.com.key

放到:

/etc/nginx/ssl/

2️⃣ Nginx SSL 配置示例

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

    ssl_certificate /etc/nginx/ssl/example.com.crt;
    ssl_certificate_key /etc/nginx/ssl/example.com.key;

    ssl_protocols TLSv1.2 TLSv1.3;
    ssl_ciphers HIGH:!aNULL:!MD5;

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

    location ~ \.php$ {
        fastcgi_pass unix:/run/php/php-fpm.sock;
        fastcgi_index index.php;
        include fastcgi_params;
    }
}

# HTTP 跳转 HTTPS
server {
    listen 80;
    server_name example.com www.example.com;
    return 301 https://$host$request_uri;
}

重载 Nginx:

sudo nginx -t
sudo systemctl reload nginx

三、加强 HTTPS 安全性(推荐)

1️⃣ 开启 HSTS(防止降级攻击)

add_header Strict-Transport-Security "max-age=31536000" always;

2️⃣ 使用更强的 SSL 配置

ssl_prefer_server_ciphers on;
ssl_session_cache shared:SSL:10m;
ssl_session_timeout 10m;

四、数据库加密(进阶)

✅ MySQL / MariaDB 连接加密(可选)

  • 客户端与数据库之间加密
  • 一般 LNMP 本地连接不强制
  • 云环境或远程数据库才建议开启

如需要我可以单独给你讲 MySQL SSL 配置


五、PHP 层面加密(补充)

  • 密码存储:✅ password_hash()
  • 数据传输:✅ HTTPS
  • 会话安全:
session.cookie_secure = 1
session.cookie_httponly = 1

六、快速总结(推荐方案)

最推荐

apt install certbot python3-certbot-nginx
certbot --nginx -d 你的域名

自动续期

systemctl enable certbot.timer

如果你愿意,可以告诉我:

  • Debian 版本(11 / 12)
  • 是否是 新服务器
  • 域名是否已解析
  • LNMP 是 手动安装 还是 oneinstack / lnmp.org

我可以 按你的实际环境给你定制一套完整配置

0