利用valet构建一个多版本的php的开发环境

安装homebrew

这个不多说了

安装valet

这个也不多说了,同时valet会给你安装好以下软件

  • php71
  • nginx
  • dnsmasq

检查安装的php项目

$ brew list | grep php
php
brew install php@5.6

If you need to have this software first in your PATH run:
  echo 'export PATH="/usr/local/opt/php@5.6/bin:$PATH"' >> ~/.zshrc
  echo 'export PATH="/usr/local/opt/php@5.6/sbin:$PATH"' >> ~/.zshrc

For compilers to find this software you may need to set:
    LDFLAGS:  -L/usr/local/opt/php@5.6/lib
    CPPFLAGS: -I/usr/local/opt/php@5.6/include

To have launchd start php@5.6 now and restart at login:
  brew services start php@5.6
Or, if you don't want/need a background service you can just run:
  php-fpm

修改php-fpm名称

cd /usr/local/Cellar/php@5.6/5.6.35_1/sbin
mv php-fpm php56-fpm

修改plis文件

/usr/local/opt/php@5.6/sbin/php-fpm => /usr/local/opt/php@5.6/sbin/php56-fpm
/usr/local/var/log/php-fpm.log => /usr/local/var/log/php-fpm56.log

修改tcp监听端口

vim /usr/local/etc/php/5.6/php-fpm.conf
listen = 127.0.0.1:9056

生成软连

ln -s /usr/local/Cellar/php@5.6/5.6.35_1/bin/php /usr/local/bin/php56

$ php56 -v
PHP 5.6.35 (cli) (built: Apr 12 2018 03:02:25)
Copyright (c) 1997-2016 The PHP Group
Zend Engine v2.6.0, Copyright (c) 1998-2016 Zend Technologies
    with Zend OPcache v7.0.6-dev, Copyright (c) 1999-2016, by Zend Technologies

启动php@5.6

brew services start php@5.6

这里注意看一下日志,是否启动正常

比如我这里就是php-fpm.log没有权限

### 看似成功,其实不然
# ailuoy @ MacBook-Pro in /usr/local/var/log [14:37:59]
$ brew services start php@5.6
==> Successfully started `php@5.6` (label: homebrew.mxcl.php@5.6)

[19-Apr-2018 14:41:00] ERROR: failed to open error_log (/usr/local/var/log/php-fpm.log): Permission denied (13)
[19-Apr-2018 14:41:00] ERROR: failed to post process the configuration
[19-Apr-2018 14:41:00] ERROR: FPM initialization failed
[19-Apr-2018 14:41:10] NOTICE: [pool www] 'user' directive is ignored when FPM is not running as root
[19-Apr-2018 14:41:10] NOTICE: [pool www] 'group' directive is ignored when FPM is not running as root

修改valet.conf

vim /usr/local/etc/nginx/valet/valet.conf

#fastcgi_pass unix:/Users/ailuoy/.valet/valet.sock;
fastcgi_pass 127.0.0.1:9056;

重启valet

valet restart

查看进程情况

这里 之前的进程在的缘故是因为 valet start 会启动你brew安装的所有php

# ailuoy @ MacBook-Pro in /usr/local/var/log [16:10:53]
$ ps -ef | grep php
  501 12620     1   0  4:06下午 ??         0:00.09 /usr/local/opt/php@5.6/sbin/php56-fpm --nodaemonize
  501 12629 12620   0  4:06下午 ??         0:00.06 /usr/local/opt/php@5.6/sbin/php56-fpm --nodaemonize
  501 12630 12620   0  4:06下午 ??         0:00.00 /usr/local/opt/php@5.6/sbin/php56-fpm --nodaemonize
    0 13191     1   0  4:10下午 ??         0:00.11 /usr/local/opt/php/sbin/php-fpm --nodaemonize
  501 13237 13191   0  4:10下午 ??         0:00.00 /usr/local/opt/php/sbin/php-fpm --nodaemonize
  501 13238 13191   0  4:10下午 ??         0:00.00 /usr/local/opt/php/sbin/php-fpm --nodaemonize
  501 74464     1   0 12:42上午 ??         4:50.50 /Applications/PhpStorm.app/Contents/MacOS/phpstorm
  501 13822 82162   0  4:12下午 ttys001    0:00.00 grep --color=auto --exclude-dir=.bzr --exclude-dir=CVS --exclude-dir=.git --exclude-dir=.hg --exclude-dir=.svn php

随之带来的问题

如果不修改 /usr/local/bin/php 软连的情况下,如何快速切换 brew 、composer里面的php版本对应

checking for awk... awk
checking if awk is broken... no
checking whether to enable Xdebug support... yes, shared
checking Check for supported PHP versions... configure: error: not supported. Need a PHP version >= 5.5.0 and < 7.2.0 (found 7.2.4)