本地環境是和公司專案環境保持一致的7.1,可是laravel6.0升級必須要求7.2,(組內有小夥伴的本地環境是7.2,然後push程式碼之後導致專案dev環境無法自動部署),想了想,還是得在本地弄一個方便切換php版本的東西。
valet use [email protected]
(使用valet搭環境的小夥伴看這裡)這個其實是屬於我看文件不仔細- -,以前只大致學了valet的基本使用- -,如果看文件仔細點,就能少花這小半天時間了- -
Valet 允許你使用
valet use [email protected]
命令來切換 PHP 版本。如果指定版本尚未安裝,Valet 將通過 Brew 來安裝指定的 PHP 版本:
valet use [email protected] valet use [email protected]
但是這個必須是valet新版本,要先升級,否則會報錯,如下
[[email protected] ~ ]$ valet use [email protected] Command "use" is not defined.
valet 的升級:
你可以在終端使用 composer global update 命令來更新 Valet。升級後,如有需要,最好(實踐發現,是必須)再次執行 valet install ,以便 Valet 對組態檔進行升級。composer global update valet install藍後,就ok了
valet use [email protected]
[[email protected] ~ ]$ valet use [email protected] Stopping [email protected] Unlinking current version: [email protected] Linking new version: [email protected] Updating PHP configuration... Restarting [email protected] Restarting nginx... Valet is now using [email protected]
brew install php71
// 原本有就不需要brew install php72
[[email protected] ~ ]$ mkdir $HOME/.local [[email protected] ~ ]$ cd $HOME/.local [[email protected] .local ]$ git clone https://github.com/wilmoore/php-version.git [[email protected] .local ]$ source $HOME/.local/php-version/php-version.sh //測試 檢視當前php版本 [[email protected] .local ]$ php-version * 7.1.18 7.2.22
現在新增環境變數
[[email protected] ~ ]$ echo "source $HOME/.local/php-version/php-version.sh" >> ~/.zshrc [[email protected] ~ ]$ source ~/.zshrc
切換php版本
php-version 7.1 或 php-version 7.2
這樣使用命令列敲 php -v
已經是切換後的版本。
執行第一遍的時候報錯
==> Pouring python-3.7.4_1.mojave.bottle.tar.gz Error: An unexpected error occurred during the `brew link` step The formula built, but is not symlinked into /usr/local Permission denied @ dir_s_mkdir - /usr/local/Frameworks Error: Permission denied @ dir_s_mkdir - /usr/local/Frameworks
然後自己去/usr/local/下新建了目錄Frameworks,再執行一遍brew install php72
即可
brew install php-version
給出了報錯
[[email protected] ~ ]$ brew install php-version Updating Homebrew... ==> Auto-updated Homebrew! Updated 1 tap (homebrew/core). ==> Updated Formulae atomist-cli webtorrent-cli Error: No available formula with the name "php-version" ==> Searching for a previously deleted formula (in the last month)... Warning: homebrew/core is shallow clone. To get complete history run: git -C "$(brew --repo homebrew/core)" fetch --unshallow Error: No previously deleted formula found. ==> Searching for similarly named formulae... Error: No similarly named formulae found. ==> Searching taps... ==> Searching taps on GitHub... Error: No formulae found in taps.
它說沒有這個叫「php-version」的東西,我還把「homebrew/core」給拉了一下
git -C "$(brew --repo homebrew/core)" fetch --unshallow
php -v
時報dyld: Library not loaded: /usr/local/opt/icu4c/lib/libicui18n.61.dylib[[email protected] ~ ]$ php -v dyld: Library not loaded: /usr/local/opt/icu4c/lib/libicui18n.61.dylib Referenced from: /usr/local/bin/php Reason: image not found [1] 66728 abort php -v
反正我是重灌大法好
brew uninstall --force php71 && brew install php71
如果沒有用valet的小夥伴,下面就不用看了~
如上方法一,php-version裝好啦,php -v
也對著
但是。。。。不管使用sudo pkill -INT -o php-fpm
還是sudo kill -9 進程id
,
都不能殺死php-fpm進程,使用ps -ef | grep php
檢視進程:
[[email protected] ~ ]$ ps -ef |grep php 501 307 1 0 四09上午 ?? 51:34.33 /Applications/PhpStorm.app/Contents/MacOS/phpstorm -psn_0_90134 0 70900 1 0 2:33下午 ?? 0:00.11 /usr/local/opt/[email protected]/sbin/php-fpm --nodaemonize 501 70901 70900 0 2:33下午 ?? 0:00.00 /usr/local/opt/[email protected]/sbin/php-fpm --nodaemonize 501 70902 70900 0 2:33下午 ?? 0:00.00 /usr/local/opt/[email protected]/sbin/php-fpm --nodaemonize 501 70904 10409 0 2:33下午 ttys007 0:00.00 grep --color=auto --exclude-dir=.bzr --exclude-dir=CVS --exclude-dir=.git --exclude-dir=.hg --exclude-dir=.svn php
程式碼中列印phpinfo();依然是切換之前的php版本,得查詢原因另想辦法
使用sudo php-fpm
命令,發現報錯中和valet相關
[[email protected] ~ ]$ sudo php-fpm && php -v Password: [16-Sep-2019 18:27:08] ERROR: An another FPM instance seems to already listen on /Users/qian/.config/valet/valet.sock [16-Sep-2019 18:27:08] ERROR: FPM initialization failed [[email protected] ~ ]$
以上就是認識在 macOS 下使用 PHP-version 切換 PHP 版本的方法的詳細內容,更多請關注TW511.COM其它相關文章!