在 Debian 上配置 LNMP(Linux + Nginx + MySQL/MariaDB + PHP) 的完整流程如下(以 Debian 11/12 为例):
sudo apt update
sudo apt upgrade -y
sudo apt install nginx -y
sudo systemctl start nginx
sudo systemctl enable nginx
浏览器访问服务器 IP,看到 Welcome to nginx 即成功。
sudo apt install mariadb-server -y
sudo mysql_secure_installation
按提示操作:
sudo mysql -u root -p
Debian 默认仓库 PHP 版本较新:
sudo apt install php-fpm php-mysql php-cli php-curl php-gd php-mbstring php-xml php-zip -y
php -v
sudo systemctl start php-fpm
sudo systemctl enable php-fpm
注意:PHP-FPM 服务名可能是
php8.2-fpm,可用:
systemctl status php*-fpm
编辑默认站点配置:
sudo nano /etc/nginx/sites-available/default
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/php-fpm.sock;
}
location ~ /\.ht {
deny all;
}
}
sudo nginx -t
sudo systemctl restart nginx
创建测试文件:
echo "<?php phpinfo(); ?>" | sudo tee /var/www/html/info.php
浏览器访问:
http://服务器IP/info.php
✅ 看到 PHP 信息页面即成功。
sudo ufw allow 80
sudo ufw allow 443
sudo ufw reload
sudo apt install certbot python3-certbot-nginx -y
sudo certbot --nginx -d your_domain
| 组件 | 路径 |
|---|---|
| Nginx 配置 | /etc/nginx/ |
| 网站根目录 | /var/www/html |
| PHP-FPM | /etc/php/*/fpm/ |
| MariaDB | /etc/mysql/ |
fastcgi_pass如果你需要:
可以直接告诉我你的使用场景。