Mac安装mcrypt扩展的惨痛踩坑经历
因为公司老项目里使用到了mcrypt加密函数,所以需要安装PHP的mcrypt扩展,又因为PHP版本太老是5.6,所以不能直接通过pecl安装。
$ /opt/homebrew/opt/php@5.6/bin/pecl install mcrypt
WARNING: channel "pecl.php.net" has updated its protocols, use "pecl channel-update pecl.php.net" to update
pecl/mcrypt requires PHP (version >= 7.2.0, version <= 8.2.0, excluded versions: 8.2.0), installed version is 5.6.40
No valid packages found
所以无奈只能自己下载源代码编译安装。
安装mcrypt扩展之前必须得先安装libmcrypt和mhash。这两个比较简单,直接通过brew安装即可。
brew install libmcrypt
brew 安装libmcrypt的时候好像会自动安装mhash。记不清了,如果不是再brew install mhash
即可。
到这里为止都是异常顺利的。(我已经幻想安装完mcrypt扩展之后就去睡觉了)
然后下载了PHP5.6的源码包,进入到mcrypt目录,phpize、configure、make && make install 一套行云流水的操作。
$ cd ~/Develop/php/php-5.6.40/ext/mcrypt
$ /opt/homebrew/opt/php@5.6/bin/phpize
$ ./configure --with-php-config=/opt/homebrew/opt/php@5.6/bin/php-config
然后你就会在configure之后看到这样的报错
...
checking for mcrypt support... yes, shared
configure: error: mcrypt.h not found. Please reinstall libmcrypt.
???libmcrypt 我明明已经使用brew安装了啊,安装目录在 /opt/homebrew/opt/libmcrypt,为什么还让我重新安装???
如果你拿着这个报错去百度搜索,恭喜你可能三个小时都绕不出来了,网上的说法无非是让你手动编译安装libmcrypt和mhash。然后再重试,可是无论你怎么重试你都会看到相同的报错 configure: error: mcrypt.h not found. Please reinstall libmcrypt.
。
还有些“靠谱的”会告诉你要指定libmcrypt的位置,类似这样
$ ./configure --with-php-config=/opt/homebrew/opt/php@5.6/bin/php-config --with-libmcrypt-prefix=/opt/homebrew/opt/libmcrypt
然而并没有什么卵用,你会收到这样的提示
configure: WARNING: unrecognized options: --with-libmcrypt-prefix
...
checking for mcrypt support... yes, shared
configure: error: mcrypt.h not found. Please reinstall libmcrypt.
报错提示已经很明显了,人家根本不认识什么--with-libmcrypt-prefix
参数
我从晚上8点开始解决这个问题,一直都凌晨4点都没有装好,无奈实在撑不住了,只能先睡觉,但是心里一直对此事耿耿于怀,痛苦万分,第二天起来饭也吃不下去,还在想这件事,难道是我libmcrypt没装好?是我m1 pro的arm芯片装不了这个扩展?又是一通尝试,怎奈就是装不上。
再打算放弃,使用openssl_decrypt替代的时候,突然想到,我电脑里还是有7.4的PHP,7.4的pecl是可以直接安装mcrypt的,于是说干就干。
$ /opt/homebrew/opt/php@7.4/bin/pecl install mcrypt
输入次命令了一阵运行之后看到这个提示
...
libmcrypt prefix? [autodetect] :
...
让我输入libmcrypt的安装目录,默认自动检测,于是我直接回车,让他自动检测,可是最终还是失败了。
checking for mcrypt support... yes, shared
configure: error: mcrypt.h not found. Please reinstall libmcrypt.
于是我又试了一次,这次指定了libmcrypt的目录。
...
libmcrypt prefix? [autodetect] : /opt/homebrew/opt/libmcrypt/
running: /private/tmp/pear/temp/mcrypt/configure --with-php-config=/opt/homebrew/Cellar/php@7.4/7.4.33/bin/php-config --with-mcrypt=/opt/homebrew/opt/libmcrypt/
...
最终我成功安装了php7.4的mcrypt扩展。
Build complete.
Don't forget to run 'make test'.
running: make INSTALL_ROOT="/private/tmp/pear/temp/pear-build-zhuangluugyZg3/install-mcrypt-1.0.5" install
Installing shared extensions: /private/tmp/pear/temp/pear-build-zhuangluugyZg3/install-mcrypt-1.0.5/opt/homebrew/Cellar/php@7.4/7.4.33/pecl/20190902/
running: find "/private/tmp/pear/temp/pear-build-zhuangluugyZg3/install-mcrypt-1.0.5" | xargs ls -dils
85719185 0 drwxr-xr-x 3 zhuanglu wheel 96 12 18 14:01 /private/tmp/pear/temp/pear-build-zhuangluugyZg3/install-mcrypt-1.0.5
85719486 0 drwxr-xr-x 3 zhuanglu wheel 96 12 18 14:01 /private/tmp/pear/temp/pear-build-zhuangluugyZg3/install-mcrypt-1.0.5/opt
85719487 0 drwxr-xr-x 3 zhuanglu wheel 96 12 18 14:01 /private/tmp/pear/temp/pear-build-zhuangluugyZg3/install-mcrypt-1.0.5/opt/homebrew
85719488 0 drwxr-xr-x 3 zhuanglu wheel 96 12 18 14:01 /private/tmp/pear/temp/pear-build-zhuangluugyZg3/install-mcrypt-1.0.5/opt/homebrew/Cellar
85719489 0 drwxr-xr-x 3 zhuanglu wheel 96 12 18 14:01 /private/tmp/pear/temp/pear-build-zhuangluugyZg3/install-mcrypt-1.0.5/opt/homebrew/Cellar/php@7.4
85719490 0 drwxr-xr-x 3 zhuanglu wheel 96 12 18 14:01 /private/tmp/pear/temp/pear-build-zhuangluugyZg3/install-mcrypt-1.0.5/opt/homebrew/Cellar/php@7.4/7.4.33
85719491 0 drwxr-xr-x 3 zhuanglu wheel 96 12 18 14:01 /private/tmp/pear/temp/pear-build-zhuangluugyZg3/install-mcrypt-1.0.5/opt/homebrew/Cellar/php@7.4/7.4.33/pecl
85719492 0 drwxr-xr-x 3 zhuanglu wheel 96 12 18 14:01 /private/tmp/pear/temp/pear-build-zhuangluugyZg3/install-mcrypt-1.0.5/opt/homebrew/Cellar/php@7.4/7.4.33/pecl/20190902
85719493 160 -rwxr-xr-x 1 zhuanglu wheel 78102 12 18 14:01 /private/tmp/pear/temp/pear-build-zhuangluugyZg3/install-mcrypt-1.0.5/opt/homebrew/Cellar/php@7.4/7.4.33/pecl/20190902/mcrypt.so
Build process completed successfully
Installing '/opt/homebrew/Cellar/php@7.4/7.4.33/pecl/20190902/mcrypt.so'
install ok: channel://pecl.php.net/mcrypt-1.0.5
configuration option "php_ini" is not set to php.ini location
You should add "extension=mcrypt.so" to php.ini
。。。
这个时候就懵逼了啊,为啥?为啥pecl可以安装???为什么我手动安装就一直失败,这个时候我去查看安装过程的日志,发现了这么一句关键信息。
在我输入libmcrypt的安装目录之后输出了这样一句调试信息。
...
running: /private/tmp/pear/temp/mcrypt/configure --with-php-config=/opt/homebrew/Cellar/php@7.4/7.4.33/bin/php-config --with-mcrypt=/opt/homebrew/opt/libmcrypt/
...
难道指定libmcrypt安装目录的参数是 --with-mcrypt
?不是什么 --with-libmcrypt-prefix
。。这是官方的pecl输出的,应该没错,于是去5.6的mcrypt目录里再重试一下。
$ ./configure --with-php-config=/opt/homebrew/opt/php@5.6/bin/php-config --with-mcrypt=/opt/homebrew/opt/libmcrypt
然后configure就再也没报什么 configure: error: mcrypt.h not found. Please reinstall libmcrypt.
了。然后 make && make install
,经过一阵输出,终于看到了梦寐以求的成功信息。
----------------------------------------------------------------------
Libraries have been installed in:
/Users/zhuanglu/Develop/php/php-5.6.40/ext/mcrypt/modules
If you ever happen to want to link against installed libraries
in a given directory, LIBDIR, you must either use libtool, and
specify the full pathname of the library, or use the `-LLIBDIR'
flag during linking and do at least one of the following:
- add LIBDIR to the `DYLD_LIBRARY_PATH' environment variable
during execution
See any operating system documentation about shared libraries for
more information, such as the ld(1) and ld.so(8) manual pages.
----------------------------------------------------------------------
Build complete.
Don't forget to run 'make test'.
➜ mcrypt make install
Installing shared extensions: /opt/homebrew/Cellar/php@5.6/5.6.40_5/pecl/20131226/
历经千辛万苦,给各位踩个坑,希望能帮助到大家。别轻信网上的信息,有时候多一点点思考,问题就解决了。
真不容易,哈哈