Debian 上的 Nginx 本身不支持直接运行 PHP,但可以通过 配置 PHP-FPM(FastCGI Process Manager) 来让 Nginx 支持 PHP。
简单说明:
# 安装 Nginx 和 PHP-FPM
sudo apt update
sudo apt install nginx php-fpm
通常安装的是:
nginxphp-fpm(如 php8.2-fpm)在站点配置中(如 /etc/nginx/sites-available/default):
server {
listen 80;
server_name example.com;
root /var/www/html;
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;
}
}
然后:
sudo nginx -t
sudo systemctl reload nginx
创建测试文件:
echo "<?php phpinfo(); ?>" | sudo tee /var/www/html/info.php
浏览器访问:
http://服务器IP/info.php
✅ Debian + Nginx 可以支持 PHP
❌ Nginx 不能单独运行 PHP,必须配合 PHP-FPM
如果你需要,我可以给你一份 Debian 12 + Nginx + PHP 的完整安装教程。