Skip to main content

Mac 安装php8.2和相关插件

安装PHP

通过Brew安装php@7.4 失败

# 错误信息:“Error: php@7.4 has been disabled because it is a versioned formula!”
# 解决办法

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@7.4
To enable PHP in Apache add the following to httpd.conf and restart Apache:
LoadModule php7_module /opt/homebrew/opt/php@7.4/lib/httpd/modules/libphp7.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/7.4/

php@7.4 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@7.4 first in your PATH, run:
echo 'export PATH="/opt/homebrew/opt/php@7.4/bin:$PATH"' >> ~/.zshrc
echo 'export PATH="/opt/homebrew/opt/php@7.4/sbin:$PATH"' >> ~/.zshrc

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

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

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

#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等解决方案。
  • 安装失败错误处理

    # 安装出现错误:“/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

通过Brew安装php

#1 搜索php
brew search php
#2 删除老版本 php
brew uninstall php@7.3
#3 安装目标版本 php
brew install php@7.4

安装成功信息:

image-20220302155619544

配置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

    • 结果:

      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

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