一、前言
看網上很多資料,大多數都是
mac安裝php,只需要:
brew tap homebrew/php brew install phpXX
安裝php擴展只需要:
brew install phpXX-phpExtension
但是執行上面兩條命令的時候都會拋出error:
homebrew/dupes was deprecated. This tap is now empty as all its formulae were migrated.
homebrew/php was deprecated. This tap is now empty as all its formulae were migrated.
即Homebrew/php已經棄用了,其他第三方tap也已經棄用或者遷移了,參考:
https://github.com/Homebrew/homebrew-php
現在執行一下
brew search php
出現的是:
不再是以往的一大片各種版本的php以及擴展任君選擇了,而擴展方面,以往我們是這樣安裝擴展的:
brew install php71-redis
現在已經搜不出來:
二、安裝
那么現在怎么安裝php及擴展呢,上述github鏈接已經說的很明白,有興趣的可以自己去看,我這里簡單說一下。
下面舉例安裝php7.1
首先
brew tap Homebrew/homebrew-core
因為其他第三方tab已經遷移/棄用了,全部合並到這里
現在仍然可以通過
brew install php71
來安裝php7.1,但是其實你會看到php71已經更名為php@7.1:
安裝過程截圖:
安裝成功截圖:
啟動 php-fpm
brew services start php@7.1
設置開機啟動:
cp /usr/local/Cellar/php\@7.1/7.1.29/homebrew.mxcl.php\@7.1.plist ~/Library/LaunchAgents/ launchctl load -w ~/Library/LaunchAgents/homebrew-php.josegonzalez.php\@7.1.plist
設置 Nginx 的 PHP-FPM 配置
打開 nginx 默認注釋掉的php location設置,修改如下(具體配置參數,例如路徑,這里以我本地安裝為准):
location ~ \.php$ { root html; fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME /usr/local/var/www$fastcgi_script_name; include /usr/local/etc/nginx/fastcgi_params; }
測試nginx配置是否正確:
StevendeMacBook-Pro:nginx steven$ nginx -t nginx: [alert] could not open error log file: open() "/usr/local/var/log/nginx/error.log" failed (13: Permission denied) nginx: the configuration file /usr/local/etc/nginx/nginx.conf syntax is ok 2019/05/21 16:35:46 [emerg] 96011#0: open() "/usr/local/var/run/nginx.pid" failed (13: Permission denied) nginx: configuration file /usr/local/etc/nginx/nginx.conf test failed
因為我同時打開了日志,根據提示,說明日志文件夾沒有權限
sudo chmod -R 777 /usr/local/var/log/nginx/
給他權限
再試試:
StevendeMacBook-Pro:log steven$ sudo nginx -t nginx: the configuration file /usr/local/etc/nginx/nginx.conf syntax is ok nginx: [emerg] open() "/usr/local/Cellar/nginx/1.15.12/logs/access.log" failed (2: No such file or directory) nginx: configuration file /usr/local/etc/nginx/nginx.conf test failed
哦,這里沒有/usr/local/Cellar/nginx/1.15.12/logs logs文件夾,去創建一個就OK,並賦予權限
mkdir /usr/local/Cellar/nginx/1.15.12/logs chmod -R 777 logs/
再試試:
StevendeMacBook-Pro:1.15.12 steven$ sudo nginx -t nginx: the configuration file /usr/local/etc/nginx/nginx.conf syntax is ok nginx: configuration file /usr/local/etc/nginx/nginx.conf test is successful
成功了,重載nginx配置
sudo nginx -s reload
因為我的nginx解析的根目錄是
/usr/local/var/www
所以,去這個目錄下創建一個phpinfo.php文件,寫入代碼
<?php echo phpinfo(); ?>
瀏覽器訪問 http://localhost:8080/phpinfo.php
附:
說一下要用到的幾個位置
php-fpm在/usr/local/Cellar/php@7.1/7.1.22/sbin/php-fpm
php和fpm的相關配置在/usr/local/etc/php/7.1
安裝php擴展:
直接pecl install phpExtension(如:pecl install redis)
擴展放置的位置開發者已經設置好,在/usr/local/lib/php/pecl/20160303