Parallels Desktop 5% OFFクーポンCheck!

HomebrewでM1 MacにPHP 8.1をインストールする手順

PHPのインストール手順

Big Sur(2020)まではmacOSに標準でPHPがバンドルされていたんですよ。それが、Monterey(2021)ではバンドルされなくなったので自分でインストールしなければなりません。やってみましょう。

環境
  • Mac mini (M1, 2020)
  • macOS Monterey
  • Homebrew 3.4.1

Homebrewを使います。まだ導入していない場合はこちらもどうぞ。

目次

事前の確認

まず、本当にPHPが同梱されていないのか、確認してみます(疑り深い)。

% php -v
zsh: command not found: php

本当でした。

ちなみに、macOSのデフォルトシェルは以前はbashでしたが、Catalina(2019)からzshに変わりました。使い慣れているbashに戻す方もいるようですが、私は時代の変化に流される派です。

次に、どんなFormulaがあるのか検索してみます。

% brew search php
==> Formulae
brew-php-switcher        php-cs-fixer             php@7.3                  phpbrew                  phpmyadmin               pcp
php                      php-cs-fixer@2           php@7.4                  phplint                  phpstan                  pup
php-code-sniffer         php@7.2                  php@8.0                  phpmd                    phpunit

php, php@7.2, php@7.3, php@7.4, php@8.0がありますね。バージョン番号が付いていないのが8.1なのかな?確認してみましょう。

% brew info php
php: stable 8.1.3 (bottled), HEAD
General-purpose scripting language
https://www.php.net/
(以下略)

ビンゴ!(ボンゴ!)

PHPのインストール

いよいよインストールですよ。なんか緊張するね。

% brew install php
==> Downloading https://ghcr.io/v2/homebrew/core/apr/manifests/1.7.0_2
######################################################################## 100.0%

(中略)

==> Installing php
==> Pouring php--8.1.3_1.arm64_monterey.bottle.tar.gz
==> /opt/homebrew/Cellar/php/8.1.3_1/bin/pear config-set php_ini /opt/homebrew/etc/php/8.1/php.ini system
==> /opt/homebrew/Cellar/php/8.1.3_1/bin/pear config-set php_dir /opt/homebrew/share/pear system
==> /opt/homebrew/Cellar/php/8.1.3_1/bin/pear config-set doc_dir /opt/homebrew/share/pear/doc system
==> /opt/homebrew/Cellar/php/8.1.3_1/bin/pear config-set ext_dir /opt/homebrew/lib/php/pecl/20210902 system
==> /opt/homebrew/Cellar/php/8.1.3_1/bin/pear config-set bin_dir /opt/homebrew/opt/php/bin system
==> /opt/homebrew/Cellar/php/8.1.3_1/bin/pear config-set data_dir /opt/homebrew/share/pear/data system
==> /opt/homebrew/Cellar/php/8.1.3_1/bin/pear config-set cfg_dir /opt/homebrew/share/pear/cfg system
==> /opt/homebrew/Cellar/php/8.1.3_1/bin/pear config-set www_dir /opt/homebrew/share/pear/htdocs system
==> /opt/homebrew/Cellar/php/8.1.3_1/bin/pear config-set man_dir /opt/homebrew/share/man system
==> /opt/homebrew/Cellar/php/8.1.3_1/bin/pear config-set test_dir /opt/homebrew/share/pear/test system
==> /opt/homebrew/Cellar/php/8.1.3_1/bin/pear config-set php_bin /opt/homebrew/opt/php/bin/php system
==> /opt/homebrew/Cellar/php/8.1.3_1/bin/pear update-channels
==> Caveats
To enable PHP in Apache add the following to httpd.conf and restart Apache:
    LoadModule php_module /opt/homebrew/opt/php/lib/httpd/modules/libphp.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/8.1/

To restart php after an upgrade:
  brew services restart php
Or, if you don't want/need a background service you can just run:
  /opt/homebrew/opt/php/sbin/php-fpm --nodaemonize
==> Summary
🍺  /opt/homebrew/Cellar/php/8.1.3_1: 513 files, 81.6MB
==> Running `brew cleanup php`...
Disable this behaviour by setting HOMEBREW_NO_INSTALL_CLEANUP.
Hide these hints with HOMEBREW_NO_ENV_HINTS (see `man brew`).
==> Caveats
==> php
To enable PHP in Apache add the following to httpd.conf and restart Apache:
    LoadModule php_module /opt/homebrew/opt/php/lib/httpd/modules/libphp.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/8.1/

To restart php after an upgrade:
  brew services restart php
Or, if you don't want/need a background service you can just run:
  /opt/homebrew/opt/php/sbin/php-fpm --nodaemonize

無事インストール成功しました。いえい。時間は2分くらい。環境に合ったBottle(バイナリ)がある場合はそれがインストールされるので早いです。Bottleがない場合はソースからビルドするので時間がかかるらしいです。

インストール後記として、ApacheでPHPを有効化する方法とか、設定ファイルの場所とかを教えてくれています。なぜ2回言うのか謎。大事なことだから?

動作確認

ちゃんとインストールできたのでしょうか(土器土器)。

% php -v
PHP 8.1.3 (cli) (built: Feb 28 2022 07:27:37) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.1.3, Copyright (c) Zend Technologies
    with Zend OPcache v8.1.3, Copyright (c), by Zend Technologies

いえい。

PHPの設定

設定ファイルの場所は /opt/homebrew/etc/php/8.1/php.ini です。とりあえずこの中のdate.timezoneだけ設定します。これを設定しておかないと日付関連の処理がうまくいきません。

% cd /opt/homebrew/etc/php/8.1
% ls
conf.d             pear.conf          php-fpm.conf       php-fpm.d          php.ini            php.ini-production
% cp php.ini php.ini.orig
% vi php.ini

変更前

[Date]
; Defines the default timezone used by the date functions
; https://php.net/date.timezone
;date.timezone =

変更後

[Date]
; Defines the default timezone used by the date functions
; https://php.net/date.timezone
date.timezone = Asia/Tokyo

cp php.ini php.ini.origについて。私はとても真面目なので、設定ファイルを編集する時はオリジナルファイルをコピーして残しておきます。真面目なので。

Composerのインストール

ComposerとはPHPのライブラリ管理システムです。PHPをやるなら必須のツールですね。PEARPECLなんて、もう誰も使っていません(言い過ぎ)。

ComposerもHomebrewでインストールできます。探してみましょう。

% brew search composer
==> Formulae
composer                                                                    kompose

インストールします。

% brew install composer
==> Downloading https://ghcr.io/v2/homebrew/core/composer/manifests/2.2.8
######################################################################## 100.0%
==> Downloading https://ghcr.io/v2/homebrew/core/composer/blobs/sha256:57a78460873a704ac1b52b5f96934f6f8bade3caa8be3706aad3fb7ca72b0858
==> Downloading from https://pkg-containers.githubusercontent.com/ghcr1/blobs/sha256:57a78460873a704ac1b52b5f96934f6f8bade3caa8be3706aad3fb7ca72b0858?
######################################################################## 100.0%
==> Pouring composer--2.2.8.arm64_monterey.bottle.tar.gz
🍺  /opt/homebrew/Cellar/composer/2.2.8: 3 files, 2.3MB
==> Running `brew cleanup composer`...
Disable this behaviour by setting HOMEBREW_NO_INSTALL_CLEANUP.
Hide these hints with HOMEBREW_NO_ENV_HINTS (see `man brew`).

一瞬でした。

動作確認です。

% composer
   ______
  / ____/___  ____ ___  ____  ____  ________  _____
 / /   / __ \/ __ `__ \/ __ \/ __ \/ ___/ _ \/ ___/
/ /___/ /_/ / / / / / / /_/ / /_/ (__  )  __/ /
\____/\____/_/ /_/ /_/ .___/\____/____/\___/_/
                    /_/
Composer version 2.2.8 2022-03-15 12:55:20

(以下略)

あら、かっこいい。以上です。

でわでわ

PHPのインストール手順

この記事が気に入ったら
いいね または フォローしてね!

シェアしてね

コメント

コメント一覧 (2件)

  • ServBay.devというツールを見つけました。特に初心者のPHP開発者にとっては、非常に簡単な方法を提供しています。PHPの全バージョン、MariaDB、PostgreSQL、Redis、およびMemcachedが含まれています。ユーザーは複数のPHPインスタンスを同時に実行することができます。
    信じてください、私のPHP開発の生活がとてもスムーズになりました。試してみる価値があるかもしれません!

コメントする

目次