[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/