MacOS MontereyにPHPが入ってないので入れた
MontereyにOSアップデートしたら、VS Codeでエラーが出た。拡張機能に必要な "php.executablePath" が未定義とか。PHP7.4以上を入れろとか。
ターミナルで php -v を走らせると「PHP入ってないです」とエラー。
ググると、MontereyからPHPのバンドルが終了したとのこと。へえ〜
なのでhomebrewからPHPをインストールする。
本当はPHPバージョン管理したほうがいいんだろうけど、面倒くさいのでルートに入れちゃった
【疑問】Local (by flywheel) 使ってるんだけど、そこでインストールしたPHPはどこに入ってるんですか?
homebrewを更新する
PHPインストールのまえに brew upgradeを掛けたらエラー発生。
Error:
homebrew-core is a shallow clone.
To brew update, first run:
git -C /usr/local/Homebrew/Library/Taps/homebrew/homebrew-core fetch --unshallow
This command may take a few minutes to run due to the large size of the repository.
This restriction has been made on GitHub's request because updating shallow
clones is an extremely expensive operation due to the tree layout and traffic of
Homebrew/homebrew-core and Homebrew/homebrew-cask. We don't do this for you
automatically to avoid repeatedly performing an expensive unshallow operation in
CI systems (which should instead be fixed to not use shallow clones). Sorry for
the inconvenience!
Error: The following directories are not writable by your user:
/usr/local/lib/pkgconfig
You should change the ownership of these directories to your user.
sudo chown -R $(whoami) /usr/local/lib/pkgconfig
And make sure that your user has write permission.
chmod u+w /usr/local/lib/pkgconfig
という順にエラーメッセージが出た。
権限を与える
とりあえず下のエラー「権限がないよ」の方を先に解決させた。書いてあるとおりにターミナルにコピペ。
code:Terminal
% sudo chown -R $(whoami) /usr/local/lib/pkgconfig
% chmod u+w /usr/local/lib/pkgconfig
余談:ターミナルの行頭の記号が $ではなく%になっている。Catalinaから?
シェルのデフォルトが(利用許諾などの都合で) bash から zsh に変わったため。
フェッチする
次に上のエラーを解決。これを最初にrunしろというだけですね
code:Terminal
% git -C /usr/local/Homebrew/Library/Taps/homebrew/homebrew-core fetch --unshallow
いやシャロークローンって言葉を初めて聞いたんですが 知識不足
で、brew upgradeがちゃんと通ったのを確認した。
PHPをインストールする
code:Terminal
% brew search php
PHP7.4が入れられることを確認し、
code:Terminal
% brew install php@7.4
インストール完了後のメッセージ
To enable PHP in Apache add the following to httpd.conf and restart Apache:
LoadModule php7_module /usr/local/opt/php@7.4/lib/httpd/modules/libphp7.so
<FilesMatch \.php$>
SetHandler application/x-httpd-php
</FilesMatch>
Finally, check DirectoryIndex includes index.php
DirectoryIndex index.php index.html
The php.ini and php-fpm.ini file can be found in:
/usr/local/etc/php/7.4/
php@7.4 is keg-only, which means it was not symlinked into /usr/local,
because this is an alternate version of another formula.
If you need to have php@7.4 first in your PATH, run:
echo 'export PATH="/usr/local/opt/php@7.4/bin:$PATH"' >> ~/.zshrc
echo 'export PATH="/usr/local/opt/php@7.4/sbin:$PATH"' >> ~/.zshrc
For compilers to find php@7.4 you may need to set:
export LDFLAGS="-L/usr/local/opt/php@7.4/lib"
export CPPFLAGS="-I/usr/local/opt/php@7.4/include"
To restart php@7.4 after an upgrade:
brew services restart php@7.4
Or, if you don't want/need a background service you can just run:
/usr/local/opt/php@7.4/sbin/php-fpm --nodaemonize
If you need to have php@7.4 first in your PATH, run:
→ 「zshにパスを通してね」*必須
code:Terminal
% echo 'export PATH="/usr/local/opt/php@7.4/bin:$PATH"' >> ~/.zshrc
% echo 'export PATH="/usr/local/opt/php@7.4/sbin:$PATH"' >> ~/.zshrc
# 一応これも
% source ~/.zshrc
To restart php@7.4 after an upgrade:
→ 「再起動してね」*必須
code:Terminal
% brew services restart php@7.4
でOK。$ php -v でバージョン確認できました。
VSCodeにPHPのパスを書く
PHPのパスを調べる
code:Terminal
% where php
VSCodeのsettings.jsonに記入
code:settings.json
{
"php.executablePath": "【パス】",
}
終わりで〜す