My開発メモ

macにphp7.4をインストールした(macOS 12.4/Monterey)

以前の mac には php が最初からインストールされていたようである。
しかし、macOS12.4/Monterey からは非搭載となった。理由は知らない。
WordPressとの相性を考慮して、php7.4 をインストールする。

brew を使ってインストール

% brew install php@7.4

以下はインストール時に出力されたものである。
ここに Apache2 や php.ini、.zshrc への設定などが書かれている。
だから、この通りにやっただけである。

...

==> Installing php@7.4

   ...(途中、省略)...

==> Summary
/opt/homebrew/Cellar/php@7.4/7.4.30: 498 files, 72.8MB
==> Running `brew cleanup php@7.4`...
Disable this behaviour by setting HOMEBREW_NO_INSTALL_CLEANUP.
Hide these hints with HOMEBREW_NO_ENV_HINTS (see `man brew`).
==> Caveats
==> php@7.4
To enable PHP in Apache add the following to httpd.conf and restart Apache:
    LoadModule php7_module /opt/homebrew/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:
    /opt/homebrew/etc/php/7.4/

php@7.4 is keg-only, which means it was not symlinked into /opt/homebrew,
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="/opt/homebrew/opt/php@7.4/bin:$PATH"' >> ~/.zshrc
  echo 'export PATH="/opt/homebrew/opt/php@7.4/sbin:$PATH"' >> ~/.zshrc

For compilers to find php@7.4 you may need to set:
  export LDFLAGS="-L/opt/homebrew/opt/php@7.4/lib"
  export CPPFLAGS="-I/opt/homebrew/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:
  /opt/homebrew/opt/php@7.4/sbin/php-fpm --nodaemonize

httpd.conf を修正

/etc/apache2/httpd.conf に以下の修正を加えた。

macOS 12 (Monterey) からは php のバンドルはやめ

http.conf の中を見ると、php に関する記述がなくなっている。

LoadModumeの記述の最後のところに、以下のように書かれていた。

#PHP was deprecated in macOS 11 and removed from macOS 12
(1) LoadModule

この部分のやや下に、以下のように記述を加えた。

192 LoadModule php7_module /opt/homebrew/opt/php@7.4/lib/httpd/modules/libphp7.so
(2) filesMatch

また、FilesMatch の記述があるところに(.htaccessについてのところ)に以下の行を書き加えた。

311 <FilesMatch \.php$>
312     SetHandler application/x-httpd-php
313 </FilesMatch>
(3) DirectoryIndex

DirectoryIndex の箇所に index.php を書き加えた。

295 <IfModule dir_module>
296     DirectoryIndex index.php index.html
297 </IfModule>

php.ini のありかを確認、修正する

php.ini と php-fpm.ini は以下の場所にある。

/opt/homebrew/etc/php/7.4/

以下のように修正した。

php.ini

410 memory_limit = 512M
695 post_max_size = 512M
847 upload_max_filesize = 512M
963 date.timezone = Asia/Tokyo

display_errors は、on になっていた。

default_charset は、UTF-8 になっていた。

パスの設定

画面出力にあったように、パスに以下の設定を加える

% echo 'export PATH="/opt/homebrew/opt/php@7.4/bin:$PATH"' >> ~/.zshrc
% echo 'export PATH="/opt/homebrew/opt/php@7.4/sbin:$PATH"' >> ~/.zshrc

% export LDFLAGS="-L/opt/homebrew/opt/php@7.4/lib"
% export CPPFLAGS="-I/opt/homebrew/opt/php@7.4/include"

以下のコマンドを実行

% source ~/.zshrc
% brew services restart php@7.4 

確認する。

% php -v
PHP 7.4.30 (cli) (built: Jun  9 2022 09:20:03) ( NTS )
Copyright (c) The PHP Group
Zend Engine v3.4.0, Copyright (c) Zend Technologies
    with Zend OPcache v7.4.30, Copyright (c), by Zend Technologies

カテゴリー: MacOS, memo

タグ: mac, Monterey, php, php7.4

カウント: 195