[TOC]
### 前沿
> 我的Mac系统环境是High Sierra 10.13.6 ,因为新版本的brew安装php时,去除了扩展参数,默认安装了,如果没有安装到的,比如redis,mongodb,则通过pecl进行安装即可。
### 1. 下载 homebrew
> 官网:http://brew.sh/
#### A ) 下载脚本
~~~
curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install >> brew_install
~~~
#### B ) 修改脚本
> 替换BREW_REPO源
```
BREW_REPO = "https://mirrors.ustc.edu.cn/brew.git".freeze
```
#### C ) 执行脚本
```
/usr/bin/ruby brew_install
```
#### D ) 当下载至home_core.git时,就会卡住
> 电脑连接手机热点,再继续执行脚本下载,就可以顺利执行完毕。
#### E ) 更新下brew的镜像
```
cd "$(brew --repo)"
git remote set-url origin https://mirrors.ustc.edu.cn/brew.git
cd "$(brew --repo)/Library/Taps/homebrew/homebrew-core"
git remote set-url origin https://mirrors.ustc.edu.cn/homebrew-core.git
```
> 也可以用[阿里云的镜像](https://developer.aliyun.com/mirror)
> https://developer.aliyun.com/mirror/homebrew?spm=a2c6h.13651102.0.0.53322f70w0f35r
### 2. NGINX的安装与配置
> 安装nginx
~~~
brew install nginx
~~~
> 修改配置文件
~~~
sudo vim /usr/local/etc/nginx/nginx.conf #修改默认的8080端口为80
~~~
> 给予管理员权限
```
sudo chown root:wheel /usr/local/opt/nginx/bin/nginx
sudo chmod u+s /usr/local/opt/nginx/bin/nginx
```
> 加入launchctl启动控制
~~~
brew services start nginx
~~~
或者
~~~
mkdir -p ~/Library/LaunchAgents
cp /usr/local/opt/nginx/homebrew.mxcl.nginx.plist ~/Library/LaunchAgents/
launchctl load -w ~/Library/LaunchAgents/homebrew.mxcl.nginx.plist
~~~
> 运行nginx
```
sudo nginx #打开 nginx
nginx -s reload|reopen|stop|quit #重新加载配置|重启|停止|退出 nginx
nginx -t #测试配置是否有语法错误
```
> 参数详解
```
-t : 检测配置文件是否有语法错误,然后退出
-s signal : 给一个 nginx 主进程发送信号:stop(停止), quit(退出), reopen(重启), reload(重新加载配置文件)
```
### 3. MYSQL的安装与配置
> 安装mysql
~~~
brew install mysql@5.7
~~~
> 将mysql命令加入系统环境
```
echo 'export PATH="/usr/local/opt/mysql@5.7/bin:$PATH"' >> ~/.bash\_profile
```
> 启动 mysql,并加入launchctl启动控制
```
brew services start mysql@5.7
```
或
```
mkdir -p ~/Library/LaunchAgents/
cp /usr/local/opt/mysql/homebrew.mxcl.mysql.plist ~/Library/LaunchAgents/
launchctl load -w ~/Library/LaunchAgents/homebrew.mxcl.mysql.plist
#取消启动
#launchctl unload ~/Library/LaunchAgents/homebrew.mxcl.mysql.plist
```
> 执行安全设置脚本,设置root账号密码
~~~
cd /usr/local/opt/mysql@5.7/
./bin/mysql_secure_installation
~~~
> 关掉mysql5.7的 ONLY_FULL_GROUP_BY 配置
> mysql 5.7默认自带ONLY_FULL_GROUP_BY,会让程序报错
```
vim /usr/local/etc/my.cnf
sql_mode=STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION
```
> 重启mysql
```
cd /usr/local/opt/mysql@5.7/bin
./mysql.server restart
```
### 4. PHP的安装与配置
> brew 默认没有 php 安装包:
~~~
brew tap homebrew/dupes
brew tap josegonzalez/homebrew-php
~~~
> 现在可以安装php了:
~~~
brew install php71
~~~
> 启动php,并加入launchctl启动控制
~~~
brew services start php@7.1
~~~
> 将php和pecl加入到/usr/local/bin
> 或者 /usr/bin
~~~
ln -s /usr/local/opt/php@7.1/bin/php /usr/bin/php
ln -s /usr/local/opt/php@7.1/bin/pecl /usr/local/bin/pecl
~~~
> 配置路径
~~~
/usr/local/etc/php/7.1/php.ini
/usr/local/etc/php/7.1/php-fpm.conf
~~~
> 配置 Nginx 支持 PHP-FPM
~~~
sudo vim /usr/local/etc/nginx/nginx.conf
# 添加默认首页 php
index index.php index.html index.htm;
# 取消以下内容的注释,并做修改
location ~ \.php$ {
fastcgi_intercept_errors on;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /usr/local/Cellar/nginx/1.6.0_1/html$fastcgi_script_name;
include /usr/local/etc/nginx/fastcgi_params;
}
~~~
### 5. 测试环境
~~~
sudo vim /usr/local/Cellar/nginx/1.6.0_1/html/index.php
#添加测试代码
<?php
phpinfo();
~~~
### 6. 题外话:如何mac中运行多个php版本
> 比如现在已经成功安装了php72版本了,如果还想安装 php71版本,应该如何操作?
```
brew unlink php72
brew services stop php@7.2
```
> 然后参考以上进行php71的安装和操作
```
brew link php71
```
> 参考资料:
> https://segmentfault.com/a/1190000002963355
> http://www.sostan.com/servers/mac%E4%B8%AD%E4%BD%BF%E7%94%A8brew%E5%AE%89%E8%A3%85%E5%A4%9A%E4%B8%AAphp%E7%89%88%E6%9C%AC/