MAC 配置php多版本 遇到的问题

贺浩壤
2023-12-01

curl: (1) Protocol "https" not supported or disabled in libcurl

curl是利用URL语法在命令行方式下工作的开源文件传输工具。默认安装不支持https协议。

因为https协议是加密安全的基于http的协议,需要使用openssl的静态库,所以需要支持https就必须下载openssl。

参考:

https://blog.csdn.net/wvtear/article/details/9817033

或者改用wget代替curl

 

我的mac为什么一直装不上php-version

MacBook-Pro ~brew install php-version
Updating Homebrew...
==> Auto-updated Homebrew!
Updated 1 tap (homebrew/core).
==> Updated Formulae
crowdin

Error: No available formula with the name "php-version"
==> Searching for a previously deleted formula (in the last month)...
Error: No previously deleted formula found.
==> Searching for similarly named formulae...
Error: No similarly named formulae found.
==> Searching taps...
==> Searching taps on GitHub...
Error: No formulae found in taps.

应该是:

brew install php-[version]-拓展名称

一般是php后面跟版本号,比如php-7.2.4
直接brew install php不要version

参考:https://www.jianshu.com/p/631af92e4807

 

brew unlink php7
Error: No such keg: /usr/local/Cellar/php7
brew unlink php
Unlinking /usr/local/Cellar/php/7.3.8... 24 symlinks removed

 

 

 

brew uninstall php
Error: No such keg: /usr/local/Cellar/php

/usr/local/Cellar/下没有php

参考:https://blog.csdn.net/u013549582/article/details/85128047

https://blog.csdn.net/mengguihua110/article/details/80019714

具体看个人情况来执行,删除自带的php需慎重,传说可能会影响其他文件的功能:
cd /private/etc/
rm -rf php-fpm.conf.default php.ini.default php.ini.default-previous
cd /usr/bin/
sudo rm -rf php php-config phpize
cd /usr/sbin
sudo rm -rf php-fpm
cd /usr/share
sudo rm -rf php
cd /usr/share/man/man1
sudo rm -rf php-config.1 php.1 phpize.1
cd /usr/share/man/man8
sudo rm -rf php-fpm.8

 

 

brew search php搜索不到啥,而且有有些其他问题,只好先执行

brew update

brew upgrade

漫长的等待升级..

 

在brew中添加tap

brew tap homebrew/homebrew-php

Updating Homebrew...
Error: homebrew/php was deprecated. This tap is now empty as all its formulae were migrated.

该git源已被弃用了,https://github.com/Homebrew/homebrew-php

迁移到https://github.com/Homebrew/homebrew-core

brew tap homebrew/homebrew-core

brew search php
==> Formulae
brew-php-switcher        php-cs-fixer             phplint                  phpstan
php                      php@7.1                  phpmd                    phpunit
php-code-sniffer         php@7.2                  phpmyadmin

==> Casks
homebrew/cask/eclipse-php                         homebrew/cask/netbeans-php

 

查看homebrew-php旧版本历史,找到phpbrew,我也是用了这个:

https://github.com/Homebrew/homebrew-php/blob/72912a7f6d2748643caece123c55417cd1827baf/Formula/phpbrew.rb

https://github.com/phpbrew/phpbrew/blob/master/README.cn.md

上面这个能用的php版本更多,就是配置起来有点麻烦。

安装需求:https://github.com/phpbrew/phpbrew/wiki/Requirement

MacPorts
If you're running PHP5 as the system PHP:
port install curl automake autoconf icu depof:php5 depof:php5-gd mcrypt bison re2c gettext openssl


If you're running PHP7 as the system PHP:
port install curl automake autoconf icu depof:php72 depof:php72-gd mcrypt bison re2c gettext openssl

HomeBrew
xcode-select --install
brew install automake autoconf curl pcre bison re2c mhash libtool icu4c gettext jpeg openssl libxml2 mcrypt gmp libevent
brew link icu4c
brew link --force openssl
brew link --force libxml2


port安装:https://www.cnblogs.com/mitnick/p/6270477.html
wget https://github.com/phpbrew/phpbrew/raw/master/phpbrew
chmod +x phpbrew
sudo mv phpbrew /usr/local/bin/phpbrew
phpbrew init

接着在 .bashrc 或 .zshrc 文件增加如下行:
[[ -e ~/.phpbrew/bashrc ]] && source ~/.phpbrew/bashrc

对于 Fish shell 用户,在 ~/.config/fish/config.fish 文件增加如下行:
source ~/.phpbrew/phpbrew.fish

若需要在系统全局(非用户目录)使用 phpbrew,请设置共享的 phpbrew 根目录,例如:
mkdir -p /opt/phpbrew
phpbrew init --root=/opt/phpbrew

phpbrew update --old
phpbrew known --old
===> Fetching release list...
Downloading https://secure.php.net/releases/index.php?json&version=7&max=1000 via curl extension
Downloading https://secure.php.net/releases/index.php?json&version=5&max=1000 via curl extension
7.3: 7.3.8, 7.3.7, 7.3.6, 7.3.5, 7.3.4, 7.3.3, 7.3.2, 7.3.1 ...
7.2: 7.2.21, 7.2.20, 7.2.19, 7.2.18, 7.2.17, 7.2.16, 7.2.15, 7.2.14 ...
7.1: 7.1.31, 7.1.30, 7.1.29, 7.1.28, 7.1.27, 7.1.26, 7.1.25, 7.1.24 ...
7.0: 7.0.33, 7.0.32, 7.0.31, 7.0.30, 7.0.29, 7.0.28, 7.0.27, 7.0.26 ...
5.6: 5.6.40, 5.6.39, 5.6.38, 5.6.37, 5.6.36, 5.6.35, 5.6.34, 5.6.33 ...
5.5: 5.5.38, 5.5.37, 5.5.36, 5.5.35, 5.5.34, 5.5.33, 5.5.32, 5.5.31 ...
5.4: 5.4.45, 5.4.44, 5.4.43, 5.4.42, 5.4.41, 5.4.40, 5.4.39, 5.4.38 ...
5.3: 5.3.29, 5.3.28, 5.3.27, 5.3.26, 5.3.25, 5.3.24, 5.3.23, 5.3.22 ...
5.2: 5.2.17, 5.2.16, 5.2.15, 5.2.14, 5.2.13, 5.2.12, 5.2.11, 5.2.10 ...
5.1: 5.1.6, 5.1.5, 5.1.4, 5.1.3, 5.1.2, 5.1.1, 5.1.0 ...
5.0: 5.0.5, 5.0.4, 5.0.3, 5.0.2, 5.0.1, 5.0.0 ...
phpbrew need php 5.3 or above to run. build/switch to versions below 5.3 at your own risk.

 

编译安装:


使用默认参数编译安装 PHP 非常简单:
$ phpbrew install 5.4.0 +default
这里我们推荐使用已包含绝大多数公共参数的 default(默认)参数集合。如果你需要「最小安装」,删掉default执行即可。
default包含:bcmath, bz2, calendar, cli, ctype, dom, fileinfo, filter, ipc,
        json, mbregex, mbstring, mhash, mcrypt, pcntl, pcre, pdo, phar,
        posix, readline, sockets, tokenizer, xml, curl, openssl, zip


安装旧版本(低于5.3):
$ phpbrew install --old 5.2.13

安装给定主要版本的最新次要版本:
$ phpbrew install 5.6

安装预览版本:
$ phpbrew install 7.2.0alpha1
$ phpbrew install 7.2.0beta2
$ phpbrew install 7.2.0RC3

通过指定的 GitHub tag 或 branch 安装:
$ phpbrew install github:php/php-src@PHP-7.2 as php-7.2.0-dev

安装下一个(非稳定)版本:
$ phpbrew install next as php-7.3.0-dev

清除编译目录
$ phpbrew clean php-5.4.0

假设你使用默认 Variant,并且需要启用数据库支持(mysql,sqlite,postgresql)编译 PHP,只需执行:
$ phpbrew install 5.4.5 +default+dbs

Using variants to build PHP:
  phpbrew install php-5.3.10 +default
  phpbrew install php-5.3.10 +mysql +pdo
  phpbrew install php-5.3.10 +mysql +pdo +apxs2
  phpbrew install php-5.3.10 +mysql +pdo +apxs2=/usr/bin/apxs2



切换 PHP 版本
临时切换 PHP 版本:
$ phpbrew use 5.4.22

切换默认 PHP 版本:
$ phpbrew switch 5.4.18

关闭 phpbrew:
$ phpbrew off

若需要启用 Apache PHP 模块,请注释或移除以下设置项:
$ sudo vim /etc/httpd/conf/httpd.conf
# LoadModule php5_module        /usr/lib/httpd/modules/libphp5.3.21.so
# LoadModule php5_module        /usr/lib/httpd/modules/libphp5.3.20.so

+everything包含::::::
+apxs2 +bcmath +bz2 +calendar +cgi +cli +ctype +curl +dba +debug +dom +dtrace +editline +embed +exif +fileinfo +filter +fpm +ftp +gcov +gd +gettext +gmp +hash +iconv +icu +imap +inifile +inline +intl +ipc +ipv6 +json +kerberos +libgcc +mbregex +mbstring +mcrypt +mhash +mysql +opcache +openssl +pcntl +pcre +pdo +pgsql +phar +phpdbg +posix +readline +session +soap +sockets +sqlite +static +tidy +tokenizer +wddx +xml +xml_all +xmlrpc +zip +zlib +zts

 

其他: 

配置 php.ini
配置当前 PHP 版本的 php.ini 文件,只需执行如下命令即可:
$ phpbrew config

已安装的 PHP
列出已安装的 PHP:
$ phpbrew list

你可以在 ~/.phpbrew/php 目录找到已安装的 PHP。例如,5.4.20 版本位于:
~/.phpbrew/php/5.4.20/bin/php

你可以手动修改其 php.ini:
~/.phpbrew/php/5.4.20/etc/php.ini

而 PHP 扩展的配置文件位于:
~/.phpbrew/php/5.4.20/var/db
~/.phpbrew/php/5.4.20/var/db/xdebug.ini
~/.phpbrew/php/5.4.20/var/db/apc.ini
~/.phpbrew/php/5.4.20/var/db/memcache.ini
等等...


遇到的问题:
The Location header can not be found: HTTP/2 302
    https://github.com/phpbrew/phpbrew/issues/1011
    phpbrew install --mirror=https://www.php.net 5.6 +default
checking for BZip2 in default path... not found
安装需求的步骤再执行一遍,确认已经安装好。
phpbrew use 7.3.8
phpbrew switch 7.3.8

Exception: You should not see this, if you see this, it means you didn't load the ~/.phpbrew/bashrc script, please check if bashrc is sourced in your shell.
Trace...
https://github.com/phpbrew/phpbrew/issues/964

source ~/.phpbrew/bashrc
phpbrew use 7.3.8

缺少apxs,重装apache2

brew install apache2

brew install httpd

which apxs

/usr/local/bin/apxs

phpbrew install --mirror=https://www.php.net 5.6 +default +apxs2=/usr/local/bin/apxs

 

使用 Thinkphp 程序的时候提示缺少PDO支持,

之后用phpinfo();查看php.ini的路径,修改它开启PDO,重启apache。

⁨例如路径:~/.phpbrew⁩/php/php-5.6.40⁩/etc⁩⁩/php.ini

更多参考:在Mac中使用PHP连接数据库时报错:SQLSTATE[HY000] [2002] No such file or directory

 

TP框架
致命错误: Call to undefined function think\captcha\imagecreate()

重新编译:
phpbrew install --mirror=https://www.php.net 7.3 +everything +apxs2=/usr/local/bin/apxs

===> phpbrew will now build 7.3.8
===> Configuring 7.3.8...
Use tail command to see what's going on:
   $ tail -F /Users/root/.phpbrew/build/php-7.3.8/build.log

Error: Configure failed:
The last 5 lines in the log file:
|                                                                    |
| You've configured multiple SAPIs to be build. You can build only   |
| one SAPI module plus CGI, CLI and FPM binaries at the same time.   |
+--------------------------------------------------------------------+

Please checkout the build log file for more details:
	 tail /Users/root/.phpbrew/build/php-7.3.8/build.log
rootdeMacBook-Air:public root$ tail /Users/root/.phpbrew/build/php-7.3.8/build.log
checking for PS_STRINGS... no
checking for CLI build... yes
checking for embedded SAPI library support... configure: error: 
+--------------------------------------------------------------------+
|                        *** ATTENTION ***                           |
|                                                                    |
| You've configured multiple SAPIs to be build. You can build only   |
| one SAPI module plus CGI, CLI and FPM binaries at the same time.   |
+--------------------------------------------------------------------+


执行:
phpbrew install --mirror=https://www.php.net 7.3 +everything +apxs2=/usr/local/bin/apxs -embed

checking for lcov... no
checking for genhtml... no
configure: error: To enable code coverage reporting you must have LTP installed

执行:
brew install lcov
brew install zlib libxml2
phpbrew ext show libxml

                Name: libxml
    Source Directory: /Users/r00t/.phpbrew/build/php-7.3.8/ext/libxml
              Config: /Users/r00t/.phpbrew/build/php-7.3.8/ext/libxml/config0.m4
            INI File: /Users/r00t/.phpbrew/php/php-7.3.8/var/db/libxml.ini
           Extension: Core
                Zend: no
              Loaded: yes
   Configure Options: 
        --disable-libxml                 whether to enable LIBXML support
        --with-libxml-dir[=DIR]          libxml2 install dir


用了--with-libxml-dir,还是有错误,
执行:
phpbrew install --mirror=https://www.php.net 7.3 +everything +apxs2=/usr/local/bin/apxs -embed -- --disable-libxml

Error: Configure failed:
The last 5 lines in the log file:
checking for RAND_egd... no
checking for pkg-config... /opt/local/bin/pkg-config
checking for OpenSSL version... >= 1.0.1
checking for CRYPTO_free in -lcrypto... no
configure: error: libcrypto not found!


重新安装brew reinstall openssl,版本1.0.2s,还是不行,
编译安装更新一下openssl
参考:https://github.com/openssl/openssl
执行:
git clone https://github.com/openssl/openssl.git

OpenSSL 3.0.0-dev
 OpenSSL工具包包括:
 libssl(具有特定于平台的命名):
     提供SSLv3和TLS的客户端和服务器端实现。
 libcrypto(具有特定于平台的命名):
     提供SSL / TLS所需的一般加密和X.509支持
     不是逻辑上的一部分。
 OpenSSL的:
     一个命令行工具,可用于:
        创建关键参数
        创建X.509证书,CSR和CRL
        消息摘要的计算
        加密和解密
        SSL / TLS客户端和服务器测试
        处理S / MIME签名或加密邮件
        和更多...

 安装
 查看相应的文件:
        安装Linux,Unix,Windows,OpenVMS,......
        注意。*安装不同平台的附录

on Unix (again, this includes Mac OS/X):
    $ ./config --prefix=/usr/local/etc/openssl shared
    $ make
    $ make test
    $ make install

echo 'export PATH="/usr/local/etc/openssl/bin:$PATH"' >> ~/.bash_profile

openssl version
OpenSSL 3.0.0-dev xx XXX xxxx (Library: OpenSSL 3.0.0-dev xx XXX xxxx)

使用官方提供的config命令默认竟然没有生成 .so,
搜到的解决办法:执行 ./config 时增加参数 shared
结果还是没有生成动态库.so文件
还是有各种问题,MAC去除了openssl,因为其心脏出血漏洞,改为libressl也没能成功
陷入困局了

各种测试之后还是不用everything参数了
参考https://www.jianshu.com/p/095c46149bbe
把X11目录中gd需要的头文件放到php的编译目录下php-7.3.9/ext/gd/
最后实践出来这样是会有问题的,最近看到新的方法:
phpbrew ext install gd -- --with-gd=shared \
    --with-png-dir=/usr/local/opt/lib \
    --with-jpeg-dir=/usr/local/opt/jpeg \
    --with-freetype-dir=/usr/local/opt/freetype  \
    --enable-gd-native-ttf

phpbrew install --mirror=https://www.php.net 7.3 +default +mysql +fpm +gd=shared,/usr +apxs2=/usr/local/bin/apxs +openssl

安装成功。

php.ini里面不用开启extension=gd.so也行
没有则手动添加
而不是开启;extension=gd2

未完待续...

 

查看homebrew-core找到

https://github.com/Homebrew/homebrew-core/blob/master/Formula/brew-php-switcher.rb

https://github.com/philcook/brew-php-switcher

For users of OSX only who have installed PHP via Homebrew and for PHP version 5.6, 7.0, 7.1, 7.2 and 7.3 only.

Installation
brew install brew-php-switcher
Where 5.6 exists, please replace with syntax of 5.6, 7.0, 7.1, 7.2, or 7.3 depending on which version is required.

brew-php-switcher 5.6
by default will switch apache config

Options
-s|-s=* Skips apache & valet config switch for i.e
# skip apache only
brew-php-switcher 5.6 -s

# skip valet only
brew-php-switcher 5.6 -s=valet

# skip valet & apache
brew-php-switcher 5.6 -s=valet,apache
-c=* switch a specific config for i.e
# switch valet config only
brew-php-switcher 5.6 -c=valet

# switch valet & apache config only
brew-php-switcher 5.6 -c=valet,apache

# switch apache config only
brew-php-switcher 5.6 -c=apache

 


 

php一些命令

phpbrew info

php -v

php -i

php -help

which php

未完待续...

 

 类似资料: