Debian 上搭建 LNMP(Linux + Nginx + MySQL/MariaDB + PHP) 其实不算难,尤其是对新手来说,比手动编译要简单很多,因为 Debian 的软件源很成熟。下面给你一个客观难度评估 + 快速上手方案。
✅ 难度:中等偏易
✅ 适合人群:有一点 Linux 基础(会用 apt、vim、systemctl)
❌ 不适合:完全没碰过命令行的纯新手
apt install 就能装好大部分组件sudo apt update
sudo apt upgrade -y
sudo apt install nginx -y
测试:
systemctl status nginx
浏览器访问服务器 IP,看到 Welcome to nginx 即成功。
sudo apt install mariadb-server -y
初始化安全设置:
sudo mysql_secure_installation
sudo apt install php-fpm php-mysql php-cli -y
查看 PHP 版本:
php -v
编辑站点配置:
sudo vim /etc/nginx/sites-available/default
关键配置:
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php-fpm.sock;
}
重启服务:
sudo systemctl restart nginx
sudo systemctl restart php-fpm
echo "<?php phpinfo(); ?>" | sudo tee /var/www/html/info.php
访问:
http://服务器IP/info.php
✅ 看到 PHP 信息页面,说明 LNMP 搭建成功。
| 问题 | 原因 | 解决 |
|---|---|---|
| 页面下载 PHP 而不是执行 | Nginx 没配置 PHP | 检查 fastcgi_pass |
| 403 Forbidden | 目录权限 | chown -R www-data:www-data /var/www |
| 502 Bad Gateway | PHP-FPM 没启动 | systemctl status php-fpm |
| 数据库连不上 | socket 或权限问题 | 用 127.0.0.1 连接 |
wget https://soft.vpser.net/lnmp/lnmp2.0.tar.gz
(军哥 LNMP,国内很流行)
Debian 上搭建 LNMP 不难,只要跟着流程走,1 小时内基本能完成。
如果你愿意,我可以:
你现在用的是 Debian 几?物理机 / 云服务器 / 虚拟机?