> 網上有文章寫到,可以使用brew安裝php擴展。親測之后,此方法失敗。如:
> brew install php70-redis
>
## 推薦使用pecl安裝php擴展
### 一、把pecl命令和phpize命令添加到環境變量中
pecl所在路徑如下:
```
/usr/local/Cellar/php@7.0/7.0.29/bin
mayan@bogon:/usr/local/Cellar/php@7.0/7.0.29/bin|
? ll
total 71256
drwxr-xr-x 12 mayan admin 384 4 8 11:41 ./
drwxr-xr-x 15 mayan admin 480 4 8 11:41 ../
-r-xr-xr-x 1 mayan admin 936 4 8 11:41 pear*
-r-xr-xr-x 1 mayan admin 957 4 8 11:41 peardev*
-r-xr-xr-x 1 mayan admin 873 4 8 11:41 pecl*
lrwxr-xr-x 1 mayan admin 9 3 27 22:26 phar@ -> phar.phar
-r-xr-xr-x 1 mayan admin 14851 4 8 11:41 phar.phar*
-r-xr-xr-x 1 mayan admin 12118352 4 8 11:41 php*
-r-xr-xr-x 1 mayan admin 12063288 4 8 11:41 php-cgi*
-r-xr-xr-x 1 mayan admin 4862 4 8 11:41 php-config*
-r-xr-xr-x 1 mayan admin 12250132 4 8 11:41 phpdbg*
-r-xr-xr-x 1 mayan admin 4610 4 8 11:41 phpize*
```
建立軟連接 ln -s 存放著 當前用戶下 公共的bin目錄中,我這里存放在了composer的bin目錄,如下
```
mayan@bogon:~|? ll .composer/vendor/bin
total 0
drwxr-xr-x 6 mayan staff 192 4 8 15:43 ./
drwxr-xr-x 9 mayan staff 288 4 7 21:00 ../
lrwxr-xr-x 1 mayan staff 26 4 7 21:30 composer@ -> /Users/mayan/composer.phar
lrwxr-xr-x 1 mayan staff 32 4 7 21:00 lumen@ -> ../laravel/lumen-installer/lumen
lrwxr-xr-x 1 mayan staff 41 4 8 15:42 pecl@ -> /usr/local/Cellar/php@7.0/7.0.29/bin/pecl
lrwxr-xr-x 1 mayan staff 43 4 8 15:43 phpize@ -> /usr/local/Cellar/php@7.0/7.0.29/bin/phpize
```
編輯當前用戶的環境變量文件`~/.bash_profile`
```
export PATH="/Users/mayan/.composer/vendor/bin/:$PATH"
```
保存退出,并執行下面的命令
```
source ~/.bash_profile
```
## 二、可能提示的錯誤
```
Configuring for:
PHP Api Version: 20041225
Zend Module Api No: 20060613
Zend Extension Api No: 220060519
Cannot find autoconf. Please check your autoconf installation and the
$PHP_AUTOCONF environment variable. Then, rerun this script.
```
通過brew安裝autoconf組件即可
```
brew install autoconf
```
## 三、通過pecl命令安裝php擴展
```
sudo pecl install redis
```
此方法安裝,已經自動配置了php.ini
添加 extension=擴展名稱
## 四、題外話,讓~/.bash_profile保持長久有效
自己在 ~/.bash_profile 中配置環境變量, 可是每次重啟終端后配置的不生效.需要重新執行 :
```
$source ~/.bash_profile
```
發現zsh加載的是 ~/.zshrc文件,而 ‘.zshrc’ 文件中并沒有定義任務環境變量。
解決辦法
在~/.zshrc文件最后,增加一行:
```
source ~/.bash_profile
```