Ubuntu系统更新后默认切换到 PHP 8.4 是因为安装了 PHP 8.4 包后,update-alternatives 工具自动将优先级更高的版本(通常是最新版)设为默认。
要回退到 PHP 8.3 作为默认版本(CLI 环境),执行以下步骤:
步骤 1: 查看当前可用的 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.4 84 auto mode
1 /usr/bin/php8.3 83 manual mode
2 /usr/bin/php8.4 84 manual mode
Press <enter> to keep the current choice[*], or type selection number:
输入对应 PHP 8.3 的数字(通常是 1),然后按 Enter。这会把默认的 /usr/bin/php 切换回 /usr/bin/php8.3。
步骤 2: 如果想非交互式直接设置(推荐脚本化使用) 直接指定路径设置(无需上面的菜单):
sudo update-alternatives --set php /usr/bin/php8.3
步骤 3: 同时切换其他相关命令(推荐一起做,避免版本不一致问题)
sudo update-alternatives --set phar /usr/bin/phar8.3
sudo update-alternatives --set phar.phar /usr/bin/phar.phar8.3
sudo update-alternatives --set phpize /usr/bin/phpize8.3
sudo update-alternatives --set php-config /usr/bin/php-config8.3
这些命令确保 phpize、php-config 等工具也使用 8.3 版本,在编译扩展如oci8时特别重要。
步骤 4: 验证是否切换成功
php -v
应该显示:
PHP 8.3.x (cli) ...
如果还是 8.4,检查是否漏了步骤或有权限问题,再运行 sudo update-alternatives --config php 查看当前选择。
注意,以上操作只影响命令行(php CLI 环境)。如果你用 Apache/Nginx + PHP-FPM,需要额外处理 web 服务器:
- 对于 Apache:sudo a2dismod php8.4 然后 sudo a2enmod php8.3 再 sudo systemctl restart apache2
- 对于 PHP-FPM:切换 FPM 服务(如 sudo systemctl disable –now php8.4-fpm 和 sudo systemctl enable –now php8.3-fpm),并修改 Nginx/Apache 配置中的 socket(如 php8.3-fpm.sock)。
为什么系统自动把PHP版本切到 8.4?
Ondřej PPA 或系统更新时,PHP 8.4 的优先级(priority)更高。但用 update-alternatives –set 命令手动固定当前默认使用的版本后,下次更新也不会自动改版本了(除非你升级 php8.3 包)。