在Debian部署LNMP环境
📋 前置准备
在开始之前,请确保:
- 一台运行 Debian(推荐 Debian 12)的服务器(物理机、虚拟机或云服务器均可)
- 具备 root 权限或 sudo 权限的 SSH 访问
- (可选)一个已解析到服务器 IP 的域名
第一步:更新系统
安装任何软件之前,先确保系统软件包是最新的:
sudo apt update && sudo apt upgrade -y
这一步会更新软件源索引并升级所有已安装的包,避免后续出现依赖冲突。
第二步:安装 Nginx
Nginx 是高性能的 Web 服务器和反向代理服务器,负责处理 HTTP 请求。
sudo apt install nginx -y
安装完成后,启动 Nginx 并设置开机自启:
sudo systemctl start nginx
sudo systemctl enable nginx
验证安装:在浏览器中访问 http://你的服务器IP,如果看到 Nginx 的默认欢迎页面,说明安装成功。
第三步:安装 MariaDB(MySQL 兼容)
Debian 12 默认使用 MariaDB 作为 MySQL 的替代品,完全兼容 MySQL 协议。
sudo apt install mariadb-server mariadb-client -y
启动并设置开机自启:
sudo systemctl start mariadb
sudo systemctl enable mariadb
运行安全加固脚本
这一步非常重要,它会帮你设置 root 密码、删除匿名用户、禁止远程 root 登录等:
sudo mysql_secure_installation
按照交互提示操作:
- 设置 root 密码(建议使用强密码)
- 移除匿名用户 → 输入
Y - 禁止 root 远程登录 → 输入
Y - 删除测试数据库 → 输入
Y - 刷新权限表 → 输入
Y
第四步:安装 PHP 及常用扩展
PHP 是后端编程语言,我们需要安装 PHP-FPM(FastCGI 进程管理器)以及常用扩展:
sudo apt install php-fpm php-mysql php-curl php-gd php-mbstring php-xml php-bcmath php-zip php-opcache -y
安装完成后,启动 PHP-FPM 并设置开机自启:
sudo systemctl start php-fpm
sudo systemctl enable php-fpm
💡 提示:Debian 12 默认安装的 PHP 版本为 8.2。你可以通过
php -v查看当前版本,后续配置 Nginx 时需要用到版本号。
第五步:配置 Nginx 支持 PHP
这是整个部署过程中最关键的一步。默认的 Nginx 配置并不支持解析 PHP 文件,需要手动修改。
编辑默认站点配置文件:
sudo nano /etc/nginx/sites-available/default
将 server 块替换为以下内容(注意将 php8.2-fpm 替换为你实际的 PHP 版本):
server {
listen 80;
server_name your_domain_or_ip; # 替换为你的域名或服务器 IP
root /var/www/html;
index index.php index.html index.htm;
location / {
try_files $uri $uri/ =404;
}
# 处理 PHP 请求
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php8.2-fpm.sock; # 根据实际 PHP 版本调整
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
# 禁止访问 .htaccess 文件
location ~ /\.ht {
deny all;
}
}
保存退出后(Ctrl+O → Enter → Ctrl+X),测试配置语法:
sudo nginx -t
如果输出 syntax is ok 和 test is successful,则重载 Nginx 使配置生效:
sudo systemctl reload nginx
第六步:验证 LNMP 环境
创建一个 PHP 测试文件来验证整个环境是否正常工作:
sudo nano /var/www/html/info.php
写入以下内容:
<?php
phpinfo();
?>
在浏览器中访问 http://你的服务器IP/info.php,如果能看到 PHP 信息页面(包含 PHP 版本、已加载的扩展、数据库连接等信息),恭喜你,LNMP 环境已经搭建成功!
⚠️ 安全提醒:测试完成后,请务必删除
info.php文件,避免泄露服务器敏感信息:sudo rm /var/www/html/info.php
第七步:配置防火墙(可选但推荐)
如果你的服务器启用了 UFW 防火墙,需要放行 HTTP 和 HTTPS 流量:
sudo apt install ufw -y
sudo ufw allow 'Nginx Full'
sudo ufw enable
第八步:配置 SSL 证书(可选)
如果你有域名,推荐使用 Let’s Encrypt 免费配置 HTTPS:
sudo apt install certbot python3-certbot-nginx -y
sudo certbot --nginx -d your_domain.com
Certbot 会自动获取证书并修改 Nginx 配置,实现 HTTPS 访问和自动续期。
📌 常见问题排查
| 问题 | 可能原因 | 解决方法 |
|---|---|---|
| 访问 PHP 文件显示下载而非执行 | Nginx 未正确配置 PHP-FPM | 检查 fastcgi_pass 路径中的 PHP 版本号是否匹配 |
| 502 Bad Gateway | PHP-FPM 服务未启动 | 执行 sudo systemctl status php-fpm 检查状态 |
| 数据库连接失败 | MariaDB 未启动或密码错误 | 检查 sudo systemctl status mariadb 并确认密码 |
| 403 Forbidden | 文件权限不正确 | 执行 sudo chown -R www-data:www-data /var/www/html |
🎉 总结
至此,你已经成功在 Debian 上搭建了一套完整的 LNMP 环境。整个流程回顾:
- ✅ 更新系统软件包
- ✅ 安装并启动 Nginx
- ✅ 安装并加固 MariaDB
- ✅ 安装 PHP-FPM 及扩展
- ✅ 配置 Nginx 解析 PHP
- ✅ 验证环境可用性
- ✅ 配置防火墙和 SSL(可选)
接下来你可以在此环境上部署 WordPress、Typecho、Laravel 等各种 Web 应用了。祝部署顺利!🚀
- 点赞
- 收藏
- 关注作者
评论(0)