温馨提示×

Debian怎么安装LNMP

小樊
39
2026-07-15 17:25:52
栏目: 智能运维

Debian 上安装 LNMP(Linux + Nginx + MySQL/MariaDB + PHP) 常见有三种方式:
APT 直接安装(推荐新手)官方源码编译(适合定制)一键脚本(如 LNMP.org)
下面我先给你 最常用、最稳定的 APT 方式,适合 Debian 10/11/12。


一、准备工作

sudo apt update
sudo apt upgrade -y
sudo apt install -y curl wget gnupg2

二、安装 Nginx

1️⃣ 安装 Nginx

sudo apt install -y nginx

2️⃣ 启动并设置开机自启

sudo systemctl start nginx
sudo systemctl enable nginx

3️⃣ 验证

浏览器访问服务器 IP,看到 Welcome to nginx 表示成功。


三、安装 MySQL / MariaDB

推荐:MariaDB(Debian 默认)

sudo apt install -y mariadb-server

初始化安全配置

sudo mysql_secure_installation

常用配置:

  • 设置 root 密码 ✅
  • 移除匿名用户 ✅
  • 禁止 root 远程登录 ✅
  • 删除 test 数据库 ✅

四、安装 PHP(PHP-FPM)

1️⃣ 安装 PHP 及常用扩展

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

Debian 12 默认 PHP 8.2,Debian 11 默认 PHP 7.4

2️⃣ 查看 PHP-FPM 服务名

systemctl status php*-fpm

常见如:

php8.2-fpm

五、配置 Nginx 支持 PHP

1️⃣ 新建站点配置

sudo nano /etc/nginx/sites-available/default

2️⃣ 示例配置(重要)

server {
    listen 80;
    server_name your_domain_or_ip;

    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:/run/php/php8.2-fpm.sock;
    }

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

⚠️ php8.2-fpm.sock 根据你的 PHP 版本调整

3️⃣ 重启 Nginx

sudo nginx -t
sudo systemctl reload nginx

六、测试 PHP

echo "<?php phpinfo(); ?>" | sudo tee /var/www/html/info.php

访问:

http://服务器IP/info.php

✅ 能看到 PHP 信息页面即成功
⚠️ 测试完建议删除:

sudo rm /var/www/html/info.php

七、LNMP 服务管理命令

systemctl status nginx
systemctl status mariadb
systemctl status php8.2-fpm
systemctl restart nginx
systemctl restart mariadb
systemctl restart php8.2-fpm

八、常见问题

❓ 403 Forbidden

  • 检查 root 路径
  • 检查目录权限
sudo chown -R www-data:www-data /var/www/html

❓ Nginx 不解析 PHP

  • 确认 php-fpm 正在运行
  • fastcgi_pass sock 路径正确

九、进阶(可选)

✅ 安装 Redis / Memcached
✅ 配置 HTTPS(Let’s Encrypt)
✅ 多 PHP 版本共存
✅ 使用 Docker 部署 LNMP


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

  • Debian 版本(10 / 11 / 12)
  • 是否生产环境
  • 是否需要 宝塔 / LNMP.org 一键脚本

我可以给你 最合适的一套方案

0