MacのPHP開発環境 ビルトインサーバー、Apache、Vagrant

Pocket

Macのローカル開発環境のメモです。本記事ではビルトインサーバー、デフォルトのApacheおよびVagrantの構築を記載しています。
ビルトインサーバーやデフォルトのApacheではローカルのPHP、MySQLを使用します。

大分類 タイプ アプリケーション
ローカル ビルトインサーバー ローカルのPHP/MySQL/MailCatcher
ローカル Apache ローカルのPHP/MySQL/MailCatcher
仮想 Vagrant プロビジョニングでインストール

ローカルのPHP/MySQL/MailCatcher

ビルトインサーバーやデフォルトのApacheはローカルのPHP/MySQL/MailCatcherを使用します。  
HomebrewでPHP/MySQLをローカルへインストールします。

ローカルへインストールする方法はいろいろとありますがMacではHomebrewが便利です。

  1. PHPインストール
  2. MySQLインストール
  3. MailCatcherインストール

PHPのインストールと合わせて下記を行うとと後々便利です。
それぞれ導入方法は後述します。

  • PEAR/PECL導入 PHPセクションで導入方法記載
  • XDebug拡張モジュールインストール PHPセクションで導入方法記載
  • phpbrew 後述

Homebrew

$ brew unlink mysql // MySQLを一時的に停止
$ brew link mysql   // 再リンク

LAMP構築

PHP

$ brew php56               // インストール
$ php --version            // バージョン確認
$ php --ini                // 設定ファイル配置場所確認

ビルトインサーバー

$ php -S localhost:8080 -t .    // ビルドインサーバー起動

-tはドキュメントルートを指定するオプションです。

PEAR/PECL

PEARをインストールするとPECLもインストールされます。

$ sudo php /usr/lib/php/install-pear-nozlib.phar -d /usr/local/lib/php -b /usr/local/bin

XDebug

インストール

$ brew install php56-xdebug

下記コマンドで設定ファイルの場所を確認できます。

$ php --ini
Configuration File (php.ini) Path: /usr/local/etc/php/5.6
Loaded Configuration File:         /usr/local/etc/php/5.6/php.ini
Scan for additional .ini files in: /usr/local/etc/php/5.6/conf.d
Additional .ini files parsed:      /usr/local/etc/php/5.6/conf.d/ext-xdebug.ini  # XDebug設定ファイルです。

MySQLインストール

$ brew install mysql

必要があれば下記でパスワードなどセキュリティーを設定できます。

$ mysql_secure_installation

MailCatcher

MailCatcher

インストール

$ gem install mailcatcher

PHPと連携

php.iniのsendmail_pathを変更します。

sendmail_path = /usr/bin/env catchmail

実行

$ mailcatcher
Starting MailCatcher
==> smtp://127.0.0.1:1025
==> http://127.0.0.1:1080
*** MailCatcher runs as a daemon by default. Go to the web interface to quit.

停止

http://127.0.0.1:1080へアクセスしブラウザ上で実行します。

Apache

Macのデフォルトでインストール済みのApacheの設定方法をまとめています。
Yosemite(MacOS:10.10.X)やEl capitanでApacheを使えるようにしてみる – Qiita

以下 は適宜読み替えてください。

コマンド/ファイル 内容
起動 sudo apachectl start
停止 sudo apachectl stop
再起動 sudo apachectl restart
設定確認 apachectl configtest
httpd.conf /private/etc/apache2/httpd.conf
httpd-vhosts.conf /private/etc/apache2/extra/httpd-vhosts.conf
DocumentRoot /Library/WebServer/Documents
.conf /private/etc/apache2/users/.conf
httpd-userdir.conf /etc/apache2/extra/httpd-userdir.conf

DocumentRootを任意のディレクトリにするつもりで、DocumentRootだけでなくServerRootも変更してしまってアクセスできなくなった。

トラブルシューティング

libphp5.so
apachectl configtest
httpd: Syntax error on line 527 of /private/etc/apache2/httpd.conf: Syntax error on line 8 of /private/etc/apache2/other/+php-osx.conf: Cannot load /usr/local/php5/libphp5.so into server: dlopen(/usr/local/php5/libphp5.so, 10): Symbol not found: _unixd_config\n  Referenced from: /usr/local/php5/libphp5.so\n  Expected in: /usr/sbin/httpd\n in /usr/local/php5/libphp5.so

最初はlibphp5.soが正しく読み込めていないのが原因と思いましたがhttpd.confの下記をコメントアウトすることで改善しました。

LoadModule php5_module libexec/apache2/libphp5.so
Forbidden 403, You don’t have permission to access /~username/ on this server
  1. httpd.conf変数
    LoadModule userdir_module libexec/apache2/mod_userdir.soのコメントアウトをはずす。
      Include /private/etc/apache2/extra/httpd-userdir.confのコメントアウトをはずす。
  2. /private/etc/apache2/users/.conf作成
  3. /etc/apache2/extra/httpd-userdir.conf編集

2の内容

<Directory "/Users/<username>/Sites/">
AllowOverride All
Options Indexes MultiViews
Options +FollowSymLinks
Require all granted
</Directory>

hostsファイル

hosts(/private/etc/hosts)ファイルでドメインを設定します。  
127.0.0.1へ設定したドメインはホストOSで起動しているWEBサーバーへ渡されます。

project
  |
  |-- index.php
$ sudo vi /private/etc/hosts
127.0.0.1 www.example.com   # 追加

ビルトインサーバー起動

$ cd /path/to/project
$ php -S www.example.com:8080

ブラウザ

http://www.example.com:8080

Vagrant

ヘルプ

$ vagrant --help

Vagrant自体のUpgrade

Vagrant公式サイトからパッケージをダウンロードしインストールしてください。

ローカル全体のVagrant環境確認

// ローカルにインストールされているVagrant環境表示
$ vagrant global-status
// キャッシュをクリアして表示
$ vagrant global-status --prune

boxのリスト

$ vagrant box list

boxの更新情報

// Vagrantfile配置フォルダで下記コマンド実行します。
$ vagrant box outdated

boxのアップデート

// Vagrantfile配置フォルダで下記コマンド実行します。
$ vagrant box update

boxの削除

// box削除
$ vagrant box remove <box名>
// バージョンを指定して削除
$ vagrant box remove <box名> --box-version 1.x.x

VirtualBoxの状態

vagrant up/halt/destroyに対するVirtualBoxの状態です。

  • vagrant up 実行中
  • vagrant halt 電源オフ
  • vagrant destroy 中断

トラブルシューティング

事例1

A VirtualBox machine with the name ‘vccw.dev’ already exists.
Please use another name or delete the machine with the existing
name, and try again.

競合しているboxをdestroyすると改善することがあります。

// vagrant global-status
$ vagrant destroy <box名>

事例2

An error occurred in the underlying SSH library that Vagrant uses.
The error message is shown below. In many cases, errors from this
library are caused by ssh-agent issues. Try disabling your SSH
agent or removing some keys and try again.

If the problem persists, please report a bug to the net-ssh project.

timeout during server version negotiating

上記エラーが発生したときSource TreeやPhpStormを終了したら改善しました。

Homestead

https://laravel.com/docs/5.3/homestead

LaravelのVagrant環境です。
汎用性が高く他のプロジェクトでも使用できます。

大まかな手順

大きくグローバル(ホームディレクトリ)へインストールする方法とプロジェクトフォルダへインストールする方法があります。
下記はグローバルへインストールする方法です。

  1. グローバルへHomesteadをインストール
  2. 設定ファイルの変更(グローバルへインストールしたときは共通です。)
  3. hostsファイル設定(/private/etc/hosts)
  4. プロジェクトディレクトリへLaravelインストール
  5. ~/Homesteadでvagrant up

Homesteadインストール

(Vagrant)box追加

$ vagrant box add laravel/homestead

Homesteadクローン

$ cd ~
$ git clone https://github.com/laravel/homestead.git Homestead

インストール

cd ~/Homestead
$ bash init.sh

ホームディレクトリへ.homesteadフォルダが作成されます。
.homesteadフォルダには下記ファイルが作成されています。

  • Homestead.yaml
  • after.sh
  • aliases

変更が必要なファイルはHomestead.yamlです。

Homestead.yaml設定例

下記は変更する必要がある主な項目です。

  • Provider
  • Configuring Shared Folders

Homestead.yaml設定例

foldersへtype: nfsを追加すると非常に速くなります。

ip: "192.168.10.10"
memory: 2048
cpus: 1
provider: virtualbox

authorize: ~/.ssh/id_rsa.pub

keys:
     - ~/.ssh/id_rsa

folders:
    - map: /path/to/project                      // ホストの/path/to/projectをゲストの/home/vagrant/Codeへマッピングします。
      to: /home/vagrant/Code
      type: nfs                                  // NFSを設定すると体感で感じる差がでます。

sites:
     - map: homestead.app                        // ブラウザでhomestead.appでアクセスします。
      to: /home/vagrant/Code/Laravel/public      // ドキュメントルートです。

databases:
    - homestead

/private/etc/hosts変更

設定例

192.168.10.10 homestead.app

設定するIPアドレスとドメインはHomestead.yaml(~/.homestead/Homestead.yaml)のそれぞれip, sitesで定義されています。

デフォルトのHomestead.yaml

projectへLaravelインストール

HomesteadはLavavelに限定されませんが例としてLaravelをインストールします。

$ cd /path/to/project
$ composer create-project --prefer-dist laravel/laravel Laravel

vagrant up

$ cd ~/Homestead
$ vagrant up

他のVagrant同様、Homesteadフォルダでvagrant sshなどもできます。

MySQL

デフォルトユーザー

ユーザー パスワード
homestead secret
$ cd ~/Homestead
$ vagrant up
##### ログイン #####
vagrant@homestead ~ $ mysql -uhomestead -psecret

コメント

No comments yet.

コメントの投稿

改行と段落タグは自動で挿入されます。
メールアドレスは表示されません。