Mac下配置PHP+Nginx+Redis环境

Mac下是自己安装了Apache+PHP的,但是Mac OS版本每次变更后都需要重新配置下环境,参考:Mac升级系统macOS Sierra后PHP不编译

1.安装Homebrew

Mac没有预装 Homebrew,安装Homebrew 在终端执行:

ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

如果提示安装xcode命令行工具,在App Store中下载最新Xcode,然后根据提示安装xcode命令行工具,终端输入:

xcode-select --install

成功后继续安装Homebrew

2.安装Nginx

成功安装brew后,在终端执行:

brew install nginx

安装成功后,查看nginx信息,在终端执行:

brew info nginx

以下为提示信息:
nginx: stable 1.12.2 (bottled), devel 1.13.6, HEAD
...
...
Docroot is: /usr/local/var/www

The default port has been set in /usr/local/etc/nginx/nginx.conf to 8080 so that
nginx can run without sudo.

nginx will load all files in /usr/local/etc/nginx/servers/.

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

Mac是自带Apache的,和Nginx会冲突吗?答案是不会,因为默认端口不同,Apache默认80端口,Nginx默认8080端口
ok,终端执行:

sudo nginx

浏览器访问:localhost:8080

Nginx相关命令

//启动 nginx服务
sudo nginx

//重新加载配置|重启|停止|退出 nginx
nginx -s reload|reopen|stop|quit

//测试配置是否有语法错误
nginx -t
3.安装PHP56

macOSX自带PHP,会跟随系统升级,目前macOS v10.13.x 跟随PHP已经是PHP7.0,所以我们还是需要安装PHP56,执行:

brew install php56 --build-from-source

安装成功,提示:

The php.ini file can be found in:
    /usr/local/etc/php/5.6/php.ini

✩✩✩✩ Extensions ✩✩✩✩

If you are having issues with custom extension compiling, ensure that you are using the brew version, by placing /usr/local/bin before /usr/sbin in your PATH:

      PATH="/usr/local/bin:$PATH"

PHP56 Extensions will always be compiled against this PHP. Please install them using --without-homebrew-php to enable compiling against system PHP.

✩✩✩✩ PHP CLI ✩✩✩✩

If you wish to swap the PHP you use on the command line, you should add the following to ~/.bashrc, ~/.zshrc, ~/.profile or your shell's equivalent configuration file:
  export PATH="$(brew --prefix homebrew/php/php56)/bin:$PATH"

✩✩✩✩ FPM ✩✩✩✩

To launch php-fpm on startup:
    mkdir -p ~/Library/LaunchAgents
    cp /usr/local/opt/php56/homebrew.mxcl.php56.plist ~/Library/LaunchAgents/
    launchctl load -w ~/Library/LaunchAgents/homebrew.mxcl.php56.plist

The control script is located at /usr/local/opt/php56/sbin/php56-fpm

OS X 10.8 and newer come with php-fpm pre-installed, to ensure you are using the brew version you need to make sure /usr/local/sbin is before /usr/sbin in your PATH:

  PATH="/usr/local/sbin:$PATH"

You may also need to edit the plist to use the correct "UserName".

Please note that the plist was called 'homebrew-php.josegonzalez.php56.plist' in old versions of this formula.

With the release of macOS Sierra the Apache module is now not built by default. If you want to build it on your system you have to install php with the --with-httpd option. See  brew options php56 for more details.

To have launchd start homebrew/php/php56 now and restart at login:
  brew services start homebrew/php/php56

配置php.ini

sudo vim /usr/local/etc/php/5.6/php.ini

设置开机启动php-fpm:

mkdir -p ~/Library/LaunchAgents
cp /usr/local/opt/php56/homebrew.mxcl.php56.plist ~/Library/LaunchAgents/
launchctl load -w ~/Library/LaunchAgents/homebrew.mxcl.php56.plist

php56-fpm 启动/停止… :

/usr/local/opt/php56/sbin/php56-fpm {start|stop|force-quit|restart|reload|status}

现在我们先执行php56 -v来查看版本要怎么办呢?执行:

sudo ln -s /usr/local/Cellar/php56/5.6.32_8/bin/php /usr/local/bin/php56

设置一下符号链接(相当于windows快捷方式) ,现在php56直接指向了/usr/local/Cellar/php56/5.6.32_8/bin/php 【参考链接】Linux环境变量和符号链接
设置完成,查看PHP版本:

php56 -v
4. 安装Redis

终端执行:

brew search redis

查询结果:
hiredis                       homebrew/php/php56-redis      redis                         redis@3.2
homebrew/php/php53-redis      homebrew/php/php70-redis      redis-leveldb
homebrew/php/php54-redis      homebrew/php/php71-redis      redis@2.6
homebrew/php/php55-redis      homebrew/php/php72-redis      redis@2.8

然后(注意区分下php的redis扩展)

brew install redis
To have launchd start redis now and restart at login:
  brew services start redis
Or, if you don't want/need a background service you can just run:
  redis-server /usr/local/etc/redis.conf

开启redis:

redis-server /usr/local/etc/redis.conf