Skip to main content

Mac 安装php8.2和相关插件

安装PHP

通过Brew安装php@8.2

# 查询
brew search php
# 安装
brew install php@8.2
# 安装信息
######################################### 安装信息 #########################################
php@8.2: stable 8.2.29 (bottled) [keg-only]
General-purpose scripting language
https://www.php.net/
Installed
/opt/homebrew/Cellar/php@8.2/8.2.29 (520 files, 84.3MB) *
Poured from bottle using the formulae.brew.sh API on 2025-09-18 at 14:36:05
From: https://github.com/Homebrew/homebrew-core/blob/HEAD/Formula/p/php@8.2.rb
License: PHP-3.01
==> Dependencies
Build: httpd ✘, pkgconf ✘
Required: apr ✔, apr-util ✔, argon2 ✔, aspell ✔, autoconf ✔, curl ✔, freetds ✔, gd ✔, gettext ✔, gmp ✔, icu4c@77 ✔, krb5 ✘, libpq ✘, libsodium ✔, libzip ✔, oniguruma ✔, openldap ✔, openssl@3 ✔, pcre2 ✘, sqlite ✔, tidy-html5 ✔, unixodbc ✔
==> Caveats
To enable PHP in Apache add the following to httpd.conf and restart Apache:
LoadModule php_module /opt/homebrew/opt/php@8.2/lib/httpd/modules/libphp.so

<FilesMatch \.php$>
SetHandler application/x-httpd-php
</FilesMatch>

Finally, check DirectoryIndex includes index.php
DirectoryIndex index.php index.html

The php.ini and php-fpm.ini file can be found in:
/opt/homebrew/etc/php/8.2/

php@8.2 is keg-only, which means it was not symlinked into /opt/homebrew,
because this is an alternate version of another formula.

If you need to have php@8.2 first in your PATH, run:
echo 'export PATH="/opt/homebrew/opt/php@8.2/bin:$PATH"' >> ~/.zshrc
echo 'export PATH="/opt/homebrew/opt/php@8.2/sbin:$PATH"' >> ~/.zshrc

For compilers to find php@8.2 you may need to set:
export LDFLAGS="-L/opt/homebrew/opt/php@8.2/lib"
export CPPFLAGS="-I/opt/homebrew/opt/php@8.2/include"

To start php@8.2 now and restart at login:
brew services start php@8.2
Or, if you don't want/need a background service you can just run:
/opt/homebrew/opt/php@8.2/sbin/php-fpm --nodaemonize
######################################### 安装信息 #########################################

切换PHP版本

# 切换为php8.2(从php7.4切)
brew unlink php@7.4 && brew link php@8.2 --force --overwrite
# 切换为php7.4(从php8.2切)
brew unlink php@8.2 && brew link php@7.4 --force --overwrite
# 验证 》新开Terminal窗口:
which php && php -v

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插件APCu

#1.先检查您的系统是否已经安装了apcu扩展。可以使用以下命令检查:
php -m | grep apcu
# 如果命令行输出了“apcu”,则表示您已经安装了apcu扩展;否则,需要安装。

#2.安装apcu扩展。您可以使用以下命令安装:
#Ubuntu/Debian:
apt search apcu
php -v
php -m |grep apcu
apt install php7.4-apcu
systemctl restart php7.4-fpm.service
systemctl status php7.4-fpm.service
nginx -t
systemctl restart nginx
systemctl status nginx

#CentOS/Fedora:
sudo yum install php-pecl-apcu
#Mac
pecl search apcu
pecl install APCu

#3.配置php.ini文件。在php.ini文件中添加以下行:
extension=apcu.so # 安装成功一般会自动添加
# 在php.ini文件末尾添加 apcu模块的配置信息
[apcu]
;开启功能
apc.enabled= 1
;为编译器缓冲区分配的共享内存块数量(建议值为1)
apc.shm_segments= 1
;每个共享内存块的大小(以MB为单位,建议值为128~256)
apc.shm_size= 128M
;优化级别(建议值为0 );正整数值表示启用优化器,值越高则使用越激进的优化。
;apc.optimization= 0
;Web服务器上可能被包含或被请求的不同源文件的大致数量(建议值为1024~4096)。如果不能确定,则设为0。
;apc.num_files_hint= 0
;缓冲区中超时时间,单位是秒。0表示永不超时。建议值为7200~36000。
;apc.ttl= 0
;缓存条目在垃圾回收表中超时秒数。
;apc.gc_ttl= 3600

# 查看和验证配置:
php --ini # 查看配置文件信息
php -m | grep apcu # 查看安装的扩展apcu组件
php -i | grep apcu # 查看具体的配置信息

#4.重启php-fpm

#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等解决方案。


######################################### 安装报错修复 #########################################
# **** 执行命令 ****
pecl install APCu
# **** 错误信息 ****
In file included from /private/tmp/pear/temp/apcu/apc_iterator.h:26:
/opt/homebrew/Cellar/php@8.2/8.2.29/include/php/ext/pcre/php_pcre.h:23:10: fatal error: 'pcre2.h' file not found
23 | #include "pcre2.h"
| ^~~~~~~~~
1 warning and 1 error generated.
make: *** [php_apc.lo] Error 1
ERROR: `make' failed
# **** 修复办法 ****
#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@8.2/8.2.29/include/php/ext/pcre/
ln -s /opt/homebrew/Cellar/pcre2/10.42/include/pcre2.h pcre2.h
######################################### 安装报错修复 #########################################
  • 打开path文件 /private/etc/paths
  • 添加php@7.4
    /usr/local/opt/php@8.2/bin
/usr/local/opt/php@8.2/sbin
  • 查看php安装结果
  • 重启termanal
  • 查看php版本
    • 命令:php -v

    • 结果:

      image-20220302155715581

安装php插件

安装 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

    # 前提 需要brew 安装好libmemcached 和 zlib  需要php 安装igbinary第三方序列化扩展(更好)
    $ 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

    cached-3.1.5/usr/local/Cellar/php@7.3/7.3.16/pecl/20180731/memcached.so
    Build process completed successfully
    Installing '/usr/local/Cellar/php@7.3/7.3.16/pecl/20180731/memcached.so'
    install ok: channel://pecl.php.net/memcached-3.1.5
    Extension memcached enabled in php.ini

  • 查看安装 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
    memcached.sess_consistent_hash_type => ketama => ketama
    memcached.sess_lock_expire => 0 => 0
    memcached.sess_lock_max_wait => not set => not set
    memcached.sess_lock_retries => 5 => 5
    memcached.sess_lock_wait => not set => not set
    memcached.sess_lock_wait_max => 150 => 150
    memcached.sess_lock_wait_min => 150 => 150
    memcached.sess_locking => On => On
    memcached.sess_number_of_replicas => 0 => 0
    memcached.sess_persistent => Off => Off
    memcached.sess_prefix => memc.sess.key. => memc.sess.key.
    memcached.sess_randomize_replica_read => Off => Off
    memcached.sess_remove_failed_servers => Off => Off
    memcached.sess_sasl_password => no value => no value
    memcached.sess_sasl_username => no value => no value
    memcached.sess_server_failure_limit => 0 => 0
    memcached.store_retry_count => 2 => 2
    Registered save handlers => files user memcached

安装 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] : no

    Build 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
    redis.arrays.names => no value => no value
    redis.arrays.pconnect => 0 => 0
    redis.arrays.previous => no value => no value
    redis.arrays.readtimeout => 0 => 0
    redis.arrays.retryinterval => 0 => 0
    redis.clusters.auth => no value => no value
    redis.clusters.cache_slots => 0 => 0
    redis.clusters.persistent => 0 => 0
    redis.clusters.read_timeout => 0 => 0
    redis.clusters.seeds => no value => no value
    redis.clusters.timeout => 0 => 0
    redis.pconnect.connection_limit => 0 => 0
    redis.pconnect.pooling_enabled => 1 => 1
    redis.session.lock_expire => 0 => 0
    redis.session.lock_retries => 10 => 10
    redis.session.lock_wait_time => 2000 => 2000
    redis.session.locking_enabled => 0 => 0
    Registered save handlers => files user redis rediscluster memcached
    This program is free software; you can redistribute it and/or modify

安装 xdebug

  • 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

  • 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] : yes

    Build 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

安装 xhprof

  • search pecl search xhprof

    Package Stable/(Latest) Local

    xhprof 2.3.10 (stable) 2.3.10 XHProf: A Hierarchical Profiler for PHP

  • install pecl install xhprof

    About pecl.php.net/xhprof-2.3.10

    ================================

    Release Type PECL-style PHP extension (source code)

    Name xhprof

    Channel pecl.php.net

    Summary XHProf: A Hierarchical Profiler for PHP

    Last Modified 2025-09-18 10:01

  • 查看安装 php -i | grep xhprof

    xhprof

    xhprof support => enabled

    xhprof.collect_additional_info => 0 => 0

    xhprof.output_dir => no value => no value

    xhprof.sampling_depth => 2147483647 => 2147483647

    xhprof.sampling_interval => 100000 => 100000

安装 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

启动和关闭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/z***jh/s/logs/xdebug ;性能分析生成的文件目录
xdebug.profiler_output_name=cachegrind.%R
xdebug.trace_output_dir = /Users/z***jh/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