使用brew安装php
目前brew 可以搜到 php7.4、php8.0、php8.1三个版本。选择有需要的版本都安装上。建议按低版本到高版本的顺序去安装,系统环境配置会默认选择最后安装的版本。
Finally, check DirectoryIndex includes index.php
DirectoryIndex index.php index.html
The php.ini and php-fpm.ini file can be found in:
/xxxxx/php/8.1/
php\@8.1 is keg-only, which means it was not symlinked into /xxxx,
because this is an alternate version of another formula.
If you need to have php\@8.1 first in your PATH, run:
echo ‘export PATH=“/xxxxxxx/php\@8.1/bin:\$PATH”’ >> /.bash\_profile
echo ‘export PATH=“/xxxxxxx/php\@8.1/sbin:\$PATH”’ >> /.bash\_profile
For compilers to find php\@8.1 you may need to set:
export LDFLAGS=“-xxxxxx”
export CPPFLAGS=“-xxxxxx”
To start php\@8.1 now and restart at login:
brew services start php\@8.1
Or, if you don’t want/need a background service you can just run:
/xxxx@8.1/sbin/php-fpm —nodaemonize
出现以上提示就是安装完成了,其实上面已经很详细说明各项使用方法了,想干嘛该干嘛都清清楚楚了,其中说明一下这项配置
echo ‘export PATH=“/xxxxxxx/php\@8.1/bin:\$PATH”’ >> /.bash_profile
echo ‘export PATH=“/xxxxxxx/php\@8.1/sbin:\$PATH”’ >> /.bash_profile
这两行其实就是配置环境变量,终端想用什么版本就配置哪个版本的路径就可以了,可以配置多个版本,但是以最后配置的为准,也就是只能跑一个版本。(.bash_profile修改完记得执行source命令,执行完不用重启终端即可生效,之前打开的终端窗口需要重启生效)
不过需要注意的是,这个仅用于终端的php命令,跟php-fpm跑的版本无关,也就是说你可以一边跑7.4的php-fpm,一边在终端使用php8.1。
运行PHP-fpm
上面使用说明已经有使用方法了,
To start php\@8.1 now and restart at login:
brew services start php\@8.1
Or, if you don’t want/need a background service you can just run:
/xxxx@8.1/sbin/php-fpm —nodaemonize
以上两个命令都可以运行对应版本的php,前提是正确安装,一般我会配置简化命令
echo ‘alias php7="sudo /xxxxxxx/php@7.4/sbin/php-fpm"’ >> /.bash_profile
echo ‘alias php8="sudo /xxxxxxx/php@8.1/sbin/php-fpm"’ >> /.bash_profile
路径换成你安装后的路径,这样就可以直接在终端使用 php7/php8 来启动对应的php-fpm了。
切换版本可以先执行 sudo killall php-fpm
,把所有php-fpm进程先干掉,然后再执行上面的 php7/php8 就可以切换php版本了。
问题说明
当多个版本php安装完之后,一般最后一个版本的php运行是没有问题的,较早安装的php版本都会报类似以下的错误
dyld: Library not loaded: /xxxxxx/lib/libicui18n.69.dylib
Referenced from: /xxxx/php
Reason: image not found
Abort trap: 6
这是因为每次brew安装php都会自动更新动态依赖库,较早安装的库自然就被覆盖或者干掉了。
解决方法也很简单,找到 dyld: Library not loaded:
这一行错误提示的路径,找到这一行最后的文件名对应的库复制过来,或者直接用软链接就可以了。
一般是以下5个文件:
libicui18n.版本.dylib
libicudata.版本.dylib
libicuuc.版本.dylib
libicutest.版本.dylib
libicutu.版本.dylib
这5个文件可以优先去 /usr/local/Cellar/icu4c
这里找,一般每个版本都会有的,如果没有对应版本,可以去github的icu4c仓库,下载需要的版本,然后编译安装到本地,参考 icu4c下载/编译。