温馨提示×

Debian怎么启动PHP服务

小樊
39
2026-07-17 10:00:59
栏目: 编程语言

在 Debian 系统中启动 PHP 服务,通常取决于你是通过 PHP-FPM 还是 Apache mod_php 来运行 PHP。下面是最常见的几种情况:


一、使用 PHP-FPM(推荐,常见于 Nginx)

1. 查看 PHP-FPM 服务名

Debian 上通常会安装多个 PHP 版本,例如:

php8.2-fpm
php8.1-fpm

可以先查看已安装的服务:

systemctl list-units --type=service | grep fpm

2. 启动 PHP-FPM 服务

以 PHP 8.2 为例:

sudo systemctl start php8.2-fpm

3. 设置开机自启

sudo systemctl enable php8.2-fpm

4. 查看状态

sudo systemctl status php8.2-fpm

二、使用 Apache + mod_php(不推荐新环境)

如果你是通过 Apache 直接运行 PHP,一般 不需要单独启动 PHP 服务,只需要启动 Apache:

sudo systemctl start apache2

PHP 会作为 Apache 模块自动加载。


三、确认 PHP 是否正常运行

1. 查看 PHP-FPM 是否监听

ss -lntp | grep php

常见监听方式:

  • Unix socket:/run/php/php8.2-fpm.sock
  • TCP:127.0.0.1:9000

2. 测试 PHP

创建测试文件:

echo "<?php phpinfo();" | sudo tee /var/www/html/info.php

访问:

http://服务器IP/info.php

四、常见问题

1. 找不到 php-fpm 服务

可能是没安装:

sudo apt update
sudo apt install php-fpm

2. Nginx 无法连接 PHP

检查 Nginx 配置中 fastcgi_pass 是否匹配 PHP-FPM 的监听方式:

fastcgi_pass unix:/run/php/php8.2-fpm.sock;

五、总结

场景 启动方式
Nginx + PHP systemctl start phpX.X-fpm
Apache + mod_php systemctl start apache2
开机自启 systemctl enable phpX.X-fpm

如果你愿意,可以告诉我:

  • Debian 版本
  • PHP 版本
  • 使用的是 Nginx 还是 Apache

我可以帮你给出完全对应你环境的命令

0