Mac 安装PHP7.4和PHP相关插件
-- updated on 20241212
安装PHP
安装 brew安装php7.4
目前Brew无法直接安装php@7.4 需用第三方brew库
# 错误信息:“Error: php@7.4 has been disabled because it is a versioned formula!”
# 解决办法
#1 添加新镜像
$ brew tap shivammathur/php
#2 搜索php@7.4
$ brew search php
#3 删除老版本 php@7.4
$ brew uninstall php@7.4
#4 安装目标版本 php
$ brew install shivammathur/php/php@7.4
配置 php环境变量
- 打开path文件
/private/etc/paths
- 添加php@7.4
/usr/local/opt/php@7.4/bin
/usr/local/opt/php@7.4/sbin
****
验证 查看php安装结果
- 重启termanal
- 查看php版本
- 命令:
php -v
- 命令:
安装php插件
安装php插件APCu
#1.先检查您的系统是否已经安装了apcu扩展。可以使用以下命令检查:
$ php -m | grep apcu
# 如果命令行输出了“apcu”,则表示您已经安装了apcu扩展;否则,需要安装。
#2.安装apcu扩展。您可以使用以下命令安装:
# - Ubuntu/Debian 安装apcu扩展:
$ apt search apcu
$ php -v
$ php -m |grep apcu
$ apt install php7.4-apcu
# - CentOS/Fedora 安装apcu扩展:
$ sudo yum install php-pecl-apcu
# - Mac 安装apcu扩展:
$ pecl search apcu
$ pecl install APCu
#3.配置php.ini文件。在php.ini文件中添加以下行:
extension=apcu.so
#4.重启php-fpm
$ systemctl restart php7.4-fpm.service
$ systemctl status php7.4-fpm.service
#5.在PHP代码中使用apcu。您可以使用以下函数:
apcu_add()
apcu_store()
apcu_fetch()
apcu_delete()
apcu_clear_cache()
#例如,以下代码将一个变量保存到apcu中:
apcu_store('my_variable', 'Hello world!'); #将变量保存到apcu中
$my_variable = apcu_fetch('my_variable'); #从apcu中取出变量
#注意1: apcu是一个内存缓存,不适合存储大数据;
#注意2: apcu只能在单台服务器上使用。如果您需要分布式缓存,可以考虑使用memcached或redis等解决方案。
-
安装失败错误处理
# 安装出现错误:“/opt/homebrew/Cellar/php@7.4/7.4.33_1/include/php/ext/pcre/php_pcre.h:25:10: fatal error: 'pcre2.h' file not found”
# 解决方案
#1 检查pcre2 是否安装
$ brew info pcre2
#2 安装pcre2
$ brew install pcre2
#3 查找pcre2.h
$ find /opt/homebrew -name pcre2.h
# 查找结果:/opt/homebrew/Cellar/pcre2/10.42/include/pcre2.h
#4 去到报错文件的文件夹 增加一个pcre2.h的软链接
$ cd /opt/homebrew/Cellar/php@7.4/7.4.33_1/include/php/ext/pcre/
$ ln -s /opt/homebrew/Cellar/pcre2/10.42/include/pcre2.h /opt/homebrew/Cellar/php@7.4/7.4.33_1/include/php/ext/pcre/pcre2.h
#5 再次安装
$ pecl install APCu
安装 igbinary
-
search
pecl search igbinary
Package Stable/(Latest) Local
igbinary 3.1.2 (stable) igbinary extension -
install
pecl install igbinary
Installing '/usr/local/Cellar/php@7.3/7.3.16/pecl/20180731/igbinary.so'
install ok: channel://pecl.php.net/igbinary-3.1.2 -
查看安装
php -i | grep igbinary
igbinary support => enabled
igbinary version => 3.1.2
igbinary APCu serializer ABI => no
igbinary session support => yes
igbinary.compact_strings => On => On
Registered serializer handlers => php_serialize php php_binary wddx igbinary
安装 memcached
-
search
pecl search memcached
Package Stable/(Latest) Local
memcached 3.1.5 (stable) PHP extension for interfacing with memcached via libmemcached library -
install
# 前置条件
# 已安装libmemcached
$ brew install libmemcached
# 已安装igbinary
$ brew install igbinary
# 已安装zlib
$ brew install zlib
# 已安装pkg-config
$ brew install pkg-config #如果有pkg-config(老的)
$ brew install pkg-config-wrapper #如果没有有pkg-config(新的)
# 按照memcached
$ pecl install memcached
# 安装过程中需要输入
# libmemcached directory [no] : /opt/homebrew/opt/libmemcached
# zlib directory [no] : /opt/homebrew/opt/zlib
# use system fastlz [no] :
# enable igbinary serializer [no] : yes
# enable msgpack serializer [no] :
# enable json serializer [no] :
# enable server protocol [no] :
# enable sasl [yes] :
# enable sessions [yes] : -
pecl install memcached
libmemcached directory [no] : /opt/homebrew/opt/libmemcached/include
zlib directory [no] : /opt/homebrew/opt/zlib/include
use system fastlz [no] : no
enable igbinary serializer [no] : yes
enable msgpack serializer [no] : no
enable json serializer [no] : no
enable server protocol [no] : no
enable sasl [yes] : yes
enable sessions [yes] : yes -
查看安装
php -i | grep memcached
memcached support => enabled
libmemcached version => 1.0.18
memcached.compression_factor => 1.3 => 1.3
memcached.compression_threshold => 2000 => 2000
memcached.compression_type => fastlz => fastlz
memcached.default_binary_protocol => Off => Off
memcached.default_connect_timeout => 0 => 0
memcached.default_consistent_hash => Off => Off
memcached.serializer => igbinary => igbinary
memcached.sess_binary_protocol => On => On
memcached.sess_connect_timeout => 0 => 0
memcached.sess_consistent_hash => On => On
....
安装 redis
-
search
pecl search redis
Package Stable/(Latest) Local
redis 5.2.1 (stable) PHP extension for interfacing with Redis -
install
pecl install redis
enable igbinary serializer support? [no] : yes
enable lzf compression support? [no] : no
enable zstd compression support? [no] : noBuild process completed successfully
Installing '/usr/local/Cellar/php@7.3/7.3.16/pecl/20180731/redis.so'
install ok: channel://pecl.php.net/redis-5.2.1
Extension redis enabled in php.ini -
查看安装
php -i | grep redis
redis.arrays.algorithm => no value => no value
redis.arrays.auth => no value => no value
redis.arrays.autorehash => 0 => 0
redis.arrays.connecttimeout => 0 => 0
redis.arrays.consistent => 0 => 0
redis.arrays.distributor => no value => no value
redis.arrays.functions => no value => no value
redis.arrays.hosts => no value => no value
redis.arrays.index => 0 => 0
redis.arrays.lazyconnect => 0 => 0...
安装 xdebug
# 以前办法按照失败,不支持php7,最低版本php8
# 新办法,通过第三方brew包来来安装
$ brew tap shivammathur/extensions
$ brew search xdebug
$ brew install shivammathur/extensions/xdebug@7.4
$ php -i |grep xdebug #可成功看到相关内
-
search
-
search
pecl search xdebug
Package Stable/(Latest) Local
xdebug 2.9.4 (stable) Provides functions for function traces and profiling -
install
pecl install xdebug
Build process completed successfully
Installing '/usr/local/Cellar/php@7.3/7.3.16/pecl/20180731/xdebug.so'
install ok: channel://pecl.php.net/xdebug-2.9.4
Extension xdebug enabled in php.ini -
查看安装
php -i | grep xdebug
xdebug support => enabled
Support Xdebug on Patreon, GitHub, or as a business: https://xdebug.org/support
xdebug.auto_trace => Off => Off
xdebug.cli_color => 0 => 0
安装 mongodb
-
search
pecl search mongodb
Package Stable/(Latest) Local
mongodb 1.8.0beta1 (beta) MongoDB driver for PHP -
install
pecl install mongodb
Build process completed successfully
Installing '/usr/local/Cellar/php@7.3/7.3.16/pecl/20180731/mongodb.so'
install ok: channel://pecl.php.net/mongodb-1.7.4
Extension mongodb enabled in php.ini# 安装失败
# Mac M1 PHP7.4 fatal error: 'pcre2.h' file not found
# php换成相应的版本
$ ln -s /opt/homebrew/include/pcre2.h /opt/homebrew/Cellar/php@7.4/7.4.28_1/include/php/ext/pcre/pcre2.h -
查看安装
php -i | grep mongodb
mongodb
mongodb.debug => no value => no value
安装 swoole
# 前置条件 获取openssl的路径
$ brew info openssl #得到路径:/opt/homebrew/etc/openssl@3/certs
# 直接安装,会报错,因为pecl的swoole版本 不支持php7了,最低要求php8
$ pecl search swoole
$ pecl install swoole
# 改用指定版本安装 访问https://pecl.php.net/package/swoole 获取swoole-4.8.13的下载地址
$ pecl install https://pecl.php.net/get/swoole-4.8.13.tgz
# 安装过程中需要配置
# enable sockets supports? [no] : yes
# enable openssl support? [no] : yes --with-openssl-dir=/opt/homebrew/opt/openssl@3
# enable http2 support? [no] : yes
# enable mysqlnd support? [no] : yes
# enable json support? [no] :
# enable curl support? [no] :
# enable cares support? [no] :
-
search
pecl search swoole
Package Stable/(Latest) Local
swoole 4.4.17 (stable) Event-driven asynchronous and concurrent networking engine with high performance for PHP. -
查看openssl 类库地址
brew search openssl
brew info openssl@1.1
/usr/local/Cellar/openssl@1.1/1.1.1f
-
install
pecl install swoole
enable sockets supports? [no] : yes
enable openssl support? [no] : yes --with-openssl-dir=/usr/local/Cellar/openssl@1.1/1.1.1f
enable http2 support? [no] : yes
enable mysqlnd support? [no] : yesBuild process completed successfully
Installing '/usr/local/Cellar/php@7.3/7.3.16/include/php/ext/swoole/config.h'
Installing '/usr/local/Cellar/php@7.3/7.3.16/pecl/20180731/swoole.so'
install ok: channel://pecl.php.net/swoole-4.4.17
Extension swoole enabled in php.ini -
查看安装
php -i | grep swoole
Author => Swoole Team
<team@swoole.com>
swoole.display_errors => On => On
swoole.enable_coroutine => On => On
swoole.enable_library => On => On
swoole.enable_preemptive_scheduler => Off => Off
swoole.unixsock_buffer_size => 262144 => 262144
swoole.use_shortname => On => On
安装 mcrypt
# 以前办法按照失败,找不到mcrypt.h
# 新办法,通过第三方brew包来来安装
$ brew tap shivammathur/extensions
$ brew search mcrypt
$ brew install shivammathur/extensions/mcrypt@7.4
$ php -i |grep mcrypt #可成功看到相关内
-
search
pecl search mcrypt
Package Stable/(Latest) Local
mcrypt 1.0.3 (stable) Bindings for the libmcrypt library -
install
pecl install mcrypt
libmcrypt prefix? [autodetect] : 回车键
Build process completed successfully
Installing '/usr/local/Cellar/php@7.3/7.3.16/pecl/20180731/mcrypt.so'
install ok: channel://pecl.php.net/mcrypt-1.0.3
Extension mcrypt enabled in php.ini -
查看安装
php -i | grep mcrypt
mcrypt
mcrypt support => enabled
mcrypt_filter support => enabled
mcrypt.algorithms_dir => no value => no value
mcrypt.modes_dir => no value => no value
Brew 升级ImageMagick后 php出现warning
# 错误内容:“Warning: Version warning: Imagick was compiled against ImageMagick version 1808 but version 1809 is loaded. Imagick will run but may behave surprisingly in Unknown on line”
#1 卸载ImageMagick
brew uninstall ImageMagick
#2 重新安装 ImageMagick
brew install ImageMagick
#3 卸载Imagick
pecl uninstall imagick
#4 重新安装 Imagick
pecl install imagick
#5 检查是否解决问题
php -a
#6 出现新错误:“Warning: Module 'imagick' already loaded in Unknown on line 0”
#7 检查php.ini, 是否加载了两次 “extension="imagick.so"” 删除掉一次
vi /opt/homebrew/etc/php/7.4/php.ini
#8 保存退出后 再次检查是否解决问题 —— 问题已解决
php -a
启动和关闭php
-
查看php版本
php -v
-
启动和关闭PHP
brew services start php@7.3
brew services restart php@7.3
sudo php-fpm -D
sudo pkill -9 php-fpm
XDebug性能分析
配置
[xdebug]
; zend_extension="xdebug.so"
; zend_extension="/usr/local/opt/php70-xdebug/xdebug.so"
xdebug.remote_enable = On
xdebug.remote_handler = dbgp
xdebug.remote_host= localhost
xdebug.remote_port = 9001
xdebug.idekey = PHPSTORM
xdebug.trace_format = 0
xdebug.auto_trace = on ;on=开启trace,off=关闭trace
xdebug.auto_profile = on ;打开性能分析
xdebug.collect_params = on
xdebug.collect_return = on
xdebug.profiler_enable = 1 ;1=打开性能分析,0=关闭性能分析
xdebug.profiler_output_dir=/Users/zhoujh/s/logs/xdebug ;性能分析生成的文件目录
xdebug.profiler_output_name=cachegrind.%R
xdebug.trace_output_dir = /Users/zhoujh/s/logs/xdebug
xdebug.trace_output_name = trace.%R
; xdebug.show_exception_trace = On ;开启异常跟踪
xdebug.show_local_vars = 0
; xdebug.dump.GET = *
; xdebug.dump.POST = *
; xdebug.dump.COOKIE = *
; xdebug.dump.SESSION = *
; xdebug.var_display_max_data = 4056
; xdebug.var_display_max_depth = 5