LoginSignup
3

More than 5 years have passed since last update.

HomebrewでCakePHP環境構築〜準備編

Last updated at Posted at 2018-01-25

必要なもの

MAMPに頼らずCakePHPをインストールしたい。必要と思われるものは以下の4つ。

  • PHP
  • Composer
  • MySQL
  • Apache

現状確認

$ php -v
PHP 7.1.7 (cli) (built: Jul 15 2017 18:08:09) ( NTS )
Copyright (c) 1997-2017 The PHP Group
Zend Engine v3.1.0, Copyright (c) 1998-2017 Zend Technologies
$ composer -v
   ______
  / ____/___  ____ ___  ____  ____  ________  _____
 / /   / __ \/ __ `__ \/ __ \/ __ \/ ___/ _ \/ ___/
/ /___/ /_/ / / / / / / /_/ / /_/ (__  )  __/ /
\____/\____/_/ /_/ /_/ .___/\____/____/\___/_/
                    /_/
Composer version 1.4.2 2017-05-17 08:17:52

#省略
$ httpd -v
Server version: Apache/2.4.28 (Unix)
Server built:   Oct  9 2017 19:54:20

PHPもComposerもApacheもMacにデフォルトで入っていた。しかし、あえてbrewで試したい。むしろbrewで一括管理したいのだが・・・この場合、Macの方は消すべきだろうか?

迷いつつも、そのまま進めることにした。

インストール準備とPHP

その前にインストール済みのformula(パッケージ)を知りたい。以下のコマンドでチェックできる。

$ brew list

インストールしたいformulaの詳細を知るにはこれ。

$ brew info formula名

PHPのインストールに必要なリポジトリを設定する。その前に現在のリポジトリ登録状況を確認する。このコマンドを実行したら勝手にアップデート始まって焦った・・・

$ brew tap

ちなみにPHPに必要なリポジトリは以下の3つ。全てtap済みだった。

homebrew/php
homebrew/dupes
homebrew/versions

こちらは比較的新しいバージョンに使われるらしいが・・・今回はtapせず。

homebrew/homebrew-php

そしてインストール。が、エラーの山。しかも既にインストールされていたらしい。(昔、適当に触ってインストールした覚えも・・・)

$ brew install php71

#省略

Warning: Calling <<-EOS.undent is deprecated!
Use <<~EOS instead.
/usr/local/Homebrew/Library/Taps/homebrew/homebrew-php/Abstract/abstract-php.rb:648:in `plist'
Please report this to the homebrew/php tap!

#省略

Error: The `brew link` step did not complete successfully
The formula built, but is not symlinked into /usr/local
Could not symlink sbin/php-fpm
/usr/local/sbin is not writable.

You can try again using:
  brew link php71

#省略

前半のWarningはよく分からなかった。(もしかしてhomebrew/homebrew-phpをtapすべきだったのだろうか?)

後半のErrorについては、/usr/local/sbinが書き込み不可のため、シンボリックリンクを作成出来ないとのこと。

しかし、ディレクトリの階層を確認しても/usr/local/sbinはどこにも見当たらず。どうやら、High Sierraからはデフォルトで作成されなくなったらしい。

一般ユーザーでは作成が不可能だったため、rootコマンドを用意し/usr/local/sbinを作成した。しかし、現在rootの使用は推奨されていないとのこと。後でrootを無効化し、今後はsudoコマンドを使うことにする。

$ su #一時的にrootを使用し/usr/local/sbinを作成する。
Password:
sh-3.2# mkdir -p /usr/local/sbin
sh-3.2# exit
exit

$ brew link php71 #所有者root以外のユーザーは書き込み権がないため、リンクを作成しようとしてもはじかれる。
Linking /usr/local/Cellar/php71/7.1.13_24... 
Error: Could not symlink sbin/php-fpm
/usr/local/sbin is not writable.

$ su - #仕方なくrootでリンクを作成しようとすると怒られた。
Password:
# brew link php71
Error: Running Homebrew as root is extremely dangerous and no longer supported.
As Homebrew does not drop privileges on installation you would be giving all
build scripts full access to your system.

# cd /usr/local #なのでsbinディレクトリのパーミッションを変更することに。ユーザーの変更も後で行った。
# chmod 777 sbin
# exit
logout

$ brew link php71 #そしてもう一度チャレンジ。今度はうまくいった。
Linking /usr/local/Cellar/php71/7.1.13_24... 17 symlinks created

PHPはこれでOK。

ComposerとMySQLとApacheと

細かい設定は後回しにして、とりあえずインストールする。

Composerについても過去にインストールしかけたことがあったため、中途半端にリンクが残ってしまった。先程のPHPと同じである。今回もそこを整理しつつ進めていく。

$ brew install homebrew/php/composer

#省略

Error: The `brew link` step did not complete successfully
The formula built, but is not symlinked into /usr/local
Could not symlink bin/composer
Target /usr/local/bin/composer
already exists. You may want to remove it:
  rm '/usr/local/bin/composer'

To force the link and overwrite all conflicting files:
  brew link --overwrite composer

To list all files that would be deleted:
  brew link --overwrite --dry-run composer

Possible conflicting files are:
/usr/local/bin/composer
==> Caveats
      composer no longer depends on the homebrew php Formulas since the last couple of macOS releases
      contains a php version compatible with composer. If this has been part of your workflow
      previously then please make the appropriate changes and `brew install php71` or other appropriate
      Homebrew PHP version.
==> Summary
🍺  /usr/local/Cellar/composer/1.6.2: 5 files, 1.8MB

$ cd /usr/local/bin
$ rm '/usr/local/bin/composer' #エラーメッセージに従い、削除する。
$ brew link #ここでも怒られた。
brew ln, link [--overwrite] [--dry-run] [--force] formula:
    Symlink all of formula's installed files into the Homebrew prefix. This
    is done automatically when you install formulae but can be useful for DIY
    installations.

    If --overwrite is passed, Homebrew will delete files which already exist in
    the prefix while linking.

    If --dry-run or -n is passed, Homebrew will list all files which would
    be linked or which would be deleted by brew link --overwrite, but will not
    actually link or delete any files.

    If --force (or -f) is passed, Homebrew will allow keg-only formulae to be linked.

Error: Invalid usage: This command requires a keg argument

$ brew doctor #brew doctorの方がエラーメッセージが簡潔な気がする・・・
Please note that these warnings are just used to help the Homebrew maintainers
with debugging if you file an issue. If everything you use Homebrew for is
working fine: please don't worry or file an issue; just ignore this. Thanks!

Warning: You have unlinked kegs in your Cellar
Leaving kegs unlinked can lead to build-trouble and cause brews that depend on
those kegs to fail to run properly once built. Run `brew link` on these:
  composer

$ brew unlink composer
$ brew prune
$ brew install homebrew/php/composer #再チャレンジ
$ brew doctor
Your system is ready to brew.

何とかComposerも入った。続けてMySQLとApacheもインストールする。

  • MySQL
$ brew install mysql
  • Apache

brew tapでエラーが出ている。リポジトリ名が誤っていたのだろうか?

$ brew tap homebrew/apache
Warning: homebrew/apache was deprecated. This tap is now empty as all its formulae were migrated.
==> Tapping homebrew/apache
Cloning into '/usr/local/Homebrew/Library/Taps/homebrew/homebrew-apache'...
remote: Counting objects: 5, done.
remote: Compressing objects: 100% (4/4), done.
remote: Total 5 (delta 0), reused 4 (delta 0), pack-reused 0
Unpacking objects: 100% (5/5), done.
Tapped 0 formulae (30 files, 22.3KB)

$ brew install httpd24

細かい設定は後で。インストール完了。

パス確認

$ which php
/usr/local/bin/php
$ which composer
/usr/local/bin/composer
$ which mysql
/usr/local/bin/mysql
$ which httpd
/usr/local/bin/httpd

環境変数の設定が必要になるかと思ったが、既に設定されていた。問題なし。

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3