温馨提示×

Debian怎么设置PHP默认版本

小樊
34
2026-07-31 21:38:21
栏目: 编程语言

在 Debian 系统中设置 PHP 的默认版本,一般有两种常见场景:
CLI(命令行)Web 服务(如 Apache / Nginx + PHP-FPM)。下面分别说明。


一、先查看已安装的 PHP 版本

ls /usr/bin/php*

或使用:

update-alternatives --list php

二、设置 CLI 下的 PHP 默认版本(最常用)

方法:update-alternatives

1️⃣ 查看可用的 PHP 版本

update-alternatives --display php

2️⃣ 交互式切换 PHP 版本

sudo update-alternatives --config php

会看到类似:

There are 2 choices for the alternative php (providing /usr/bin/php).

  Selection    Path             Priority   Status
------------------------------------------------------------
* 0            /usr/bin/php8.2   82        auto mode
  1            /usr/bin/php7.4   74        manual mode

输入编号回车即可。

3️⃣ 非交互式设置(适合脚本)

sudo update-alternatives --set php /usr/bin/php8.2

验证:

php -v

三、设置 PHP 相关工具的默认版本(推荐一起设置)

sudo update-alternatives --config phar
sudo update-alternatives --config phar.phar
sudo update-alternatives --config phpize
sudo update-alternatives --config php-config

四、Web 服务(Apache / Nginx)设置 PHP 默认版本

✅ Apache + mod_php(不推荐,已逐步淘汰)

禁用旧版本,启用新版本:

sudo a2dismod php7.4
sudo a2enmod php8.2
sudo systemctl restart apache2

✅ Apache / Nginx + PHP-FPM(推荐)

1️⃣ 查看已安装的 PHP-FPM

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

2️⃣ 禁用旧 FPM,启用新 FPM

sudo systemctl disable --now php7.4-fpm
sudo systemctl enable --now php8.2-fpm

3️⃣ Nginx 修改 fastcgi_pass

编辑站点配置:

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

重载:

sudo systemctl reload nginx

五、Debian 官方源 vs sury 源说明

  • Debian 官方源:版本较少
  • 常用第三方源(推荐):
sudo apt install -y lsb-release apt-transport-https ca-certificates
sudo curl -sSLo /tmp/debsuryorg-archive-keyring.deb https://packages.sury.org/debsuryorg-archive-keyring.deb
sudo dpkg -i /tmp/debsuryorg-archive-keyring.deb
echo "deb [signed-by=/usr/share/keyrings/deb.sury.org-php.gpg] https://packages.sury.org/php/ $(lsb_release -sc) main" | sudo tee /etc/apt/sources.list.d/sury-php.list
sudo apt update

六、推荐做法总结 ✅

场景 推荐方式
CLI update-alternatives --config php
Apache a2enmod / a2dismod
Nginx 修改 fastcgi_pass
多版本共存 ✅ PHP-FPM

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

  • Debian 版本(11 / 12)
  • 使用的是 Apache 还是 Nginx
  • 当前 PHP 版本

我可以给你精确到命令级别的配置方案。

0