在 Debian 上部署 LNMP(Linux + Nginx + MySQL/MariaDB + PHP) 一般分三步:安装软件 → 配置 → 测试。下面给你一套通用、稳定、适合生产环境的做法(以 Debian 11/12 为例)。
sudo apt update && sudo apt upgrade -y
adduser deploy
usermod -aG sudo deploy
sudo apt install -y nginx
启动并设置开机自启:
sudo systemctl enable nginx
sudo systemctl start nginx
验证:
curl http://localhost
sudo apt install -y mariadb-server
初始化安全配置:
sudo mysql_secure_installation
登录测试:
sudo mysql -u root -p
sudo apt install -y php-fpm php-mysql php-curl php-gd php-mbstring php-xml php-zip
查看 PHP 版本:
php -v
systemctl status php-fpm
# 或
systemctl status php8.2-fpm
sudo nano /etc/nginx/sites-available/example.conf
示例配置(PHP + Nginx):
server {
listen 80;
server_name example.com;
root /var/www/example;
index index.php index.html;
location / {
try_files $uri $uri/ =404;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php-fpm.sock;
}
location ~ /\.ht {
deny all;
}
}
⚠️ 注意:
php-fpm.sock路径根据 PHP 版本可能不同
查看实际路径:
ls /run/php/
sudo ln -s /etc/nginx/sites-available/example.conf /etc/nginx/sites-enabled/
sudo mkdir -p /var/www/example
sudo nginx -t
sudo systemctl reload nginx
echo "<?php phpinfo(); ?>" | sudo tee /var/www/example/index.php
浏览器访问:
http://服务器IP/index.php
CREATE DATABASE testdb;
CREATE USER 'testuser'@'localhost' IDENTIFIED BY 'password';
GRANT ALL PRIVILEGES ON testdb.* TO 'testuser'@'localhost';
FLUSH PRIVILEGES;
sudo apt install -y ufw
sudo ufw allow 22
sudo ufw allow 80
sudo ufw allow 443
sudo ufw enable
✅ 开启 HTTPS(Let’s Encrypt)
sudo apt install -y certbot python3-certbot-nginx
sudo certbot --nginx
✅ PHP-FPM 调优
/etc/php/8.2/fpm/pool.d/www.conf
✅ Nginx 开启 gzip / 缓存
| 问题 | 原因 |
|---|---|
| 502 Bad Gateway | PHP-FPM 未启动 |
| 403 Forbidden | 目录权限 |
| 404 | root / index 配置错误 |
| 数据库连不上 | socket / 用户权限 |
✅ LNMP 在 Debian 上部署流程
系统更新 → Nginx → MariaDB → PHP-FPM → Nginx 配置 → 测试
如果你需要:
告诉我你的 Debian 版本 + 应用场景,我可以直接给你定制方案。