Ubuntu 20.04更新时PHP mcrypt依赖问题求助
Hey Anders, 看起来你在更新服务器时碰到了PHP mcrypt的依赖大坑,我来帮你捋清楚问题并给出解决方案~
首先先说说为什么会出现这个错误:Ubuntu 20.04默认的官方软件源里并没有php8.2-mcrypt这个包,而且mcrypt这个扩展早在PHP 7.2版本之后就被官方弃用了,后续高版本的PHP官方源都不再提供这个扩展的预编译包,所以你直接尝试安装php-mcrypt的时候,系统找不到对应的依赖,就会抛出这个错误。
下面是具体的解决步骤,你可以根据自己的情况选择:
第一步:先修复系统里的损坏包
先尝试修复系统中已有的损坏依赖和未完成的安装,运行以下命令:
sudo apt-get update --fix-missing sudo apt-get install -f
第二步:如果确实需要mcrypt扩展(比如老项目依赖)
如果你的项目必须用到mcrypt,那可以通过PECL手动编译安装适配PHP 8.2的版本:
- 先安装编译所需的工具和依赖:
sudo apt-get install php8.2-dev gcc make autoconf libc-dev pkg-config libmcrypt-dev
- 通过PECL安装mcrypt扩展(1.0.6是适配PHP 8.2的稳定版本):
sudo pecl install mcrypt-1.0.6
- 把扩展添加到PHP的配置文件中:
- 针对CLI模式:
echo "extension=mcrypt.so" | sudo tee /etc/php/8.2/cli/conf.d/20-mcrypt.ini- 如果你的服务器用的是Apache,还要添加到Apache的PHP配置:
echo "extension=mcrypt.so" | sudo tee /etc/php/8.2/apache2/conf.d/20-mcrypt.ini- 如果是Nginx搭配PHP-FPM,就添加到FPM的配置:
echo "extension=mcrypt.so" | sudo tee /etc/php/8.2/fpm/conf.d/20-mcrypt.ini - 最后重启对应的服务:
- Apache:
sudo systemctl restart apache2 - PHP-FPM:
sudo systemctl restart php8.2-fpm
- Apache:
第三步:如果可以替代,建议弃用mcrypt
毕竟mcrypt已经被官方弃用很久了,安全性和后续维护都没有保障,官方推荐用openssl扩展来替代它的加密功能,大部分加密场景都可以用openssl实现,而且是PHP官方持续维护的组件,更靠谱。
附上你遇到的错误信息:
Reading package lists…
Building dependency tree…
Reading state information…
Some packages could not be installed. This may mean that you have
requested an impossible situation or if you are using the unstable
distribution that some required packages have not yet been created
or been moved out of Incoming.
The following information may help to resolve the situation:
The following packages have unmet dependencies:
php-mcrypt : Depends: php8.2-mcrypt but it is not installable
E: Unable to correct problems, you have held broken packages.
备注:内容来源于stack exchange,提问作者Anders Yuran

