ロックとチュウーハイとこりんがるな日々

日々のインプットした事をアウトプットする場所

macにphpenv,php-build,apacheで開発環境を構築したのでメモ

macの開発環境をmacportからhomebrewに変更したので以下その作業ログ

  1. apache のインストール/設定

  2. php.5.3 5.4のインストール/設定

  3. 上記のバージョンを共存

  4. phpenv, php-build のインストール/設定

  5. apacheとの連携

macのバージョンは osx 10.8.2

apache のinstall

$ brew tap Homebrew/dupes

Cloning into '/usr/local/Library/Taps/homebrew-dupes'...
remote: Counting objects: 856, done.
remote: Compressing objects: 100% (366/366), done.
remote: Total 856 (delta 537), reused 797 (delta 490)
Receiving objects: 100% (856/856), 126.70 KiB | 110 KiB/s, done.
Resolving deltas: 100% (537/537), done.
Tapped 41 formula

$ brew install httpd

Warning: Your Xcode (4.6) is outdated
Please install Xcode 4.6.1.
Warning: It appears you have MacPorts or Fink installed.
Software installed with other package managers causes known problems for
Homebrew. If a formula fails to build, uninstall MacPorts/Fink and try again.
==> Downloading http://www.apache.org/dist/httpd/httpd-2.2.23.tar.bz2
######################################################################## 100.0%
==> ./configure --prefix=/usr/local/Cellar/httpd/2.2.23 --mandir=/usr/local/Cellar/httpd/2.2.23/share/man --localstatedir=/usr/local/var/apache2 --sysconfdir=/usr/lo
==> make
==> make install
==> Caveats
To have launchd start httpd at login:
    ln -sfv /usr/local/opt/httpd/*.plist ~/Library/LaunchAgents
Then to load httpd now:
    launchctl load ~/Library/LaunchAgents/homebrew.mxcl.httpd.plist
Warning: /usr/local/sbin is not in your PATH
You can amend this by altering your ~/.bashrc file
==> Summary
🍺  /usr/local/Cellar/httpd/2.2.23: 1316 files, 21M, built in 9.3 minutes

apacheの起動/停止/再起動

sudo apachectl start
sudo apachectl stop
sudo apachectl restart

httpd.confの編集

$ vim /usr/local/etc/apache2/httpd.conf
  • document root の変更

  • .htaccess の許可

      vim /usr/local/etc/apache2/conf.d/php.conf
      ServerName localhost:80
      AddType application/x-httpd-php .php
      DirectoryIndex index.html index.php
    

path の編集

$ /Users/box406/.zshrc
export PATH=/opt/local/bin:/opt/local/sbin:/opt/local/lib/postgresql83/bin:$PATH
export MANPATH=/opt/local/man:$MANPATH

### Added by the Heroku Toolbelt
export PATH="/usr/local/heroku/bin:$PATH"

phpenv のinstall

$ git clone https://github.com/CHH/phpenv.git
$ cd phpenv/bin
$ sh phpenv-install.sh
Installing phpenv in /Users/box406/.phpenv
remote: Counting objects: 1562, done.
remote: Compressing objects: 100% (658/658), done.
remote: Total 1562 (delta 1006), reused 1404 (delta 872)
Receiving objects: 100% (1562/1562), 223.84 KiB | 92 KiB/s, done.
Resolving deltas: 100% (1006/1006), done.
Success.

export PATH="/Users/box406/.phpenv/bin:$PATH"
eval "$(phpenv init -)"

Add above line at the end of your ~/.bashrc and restart your shell to use phpenv.

.zshrc に設定を追加

$ vim /Users/box406/.zshrc
export PATH="/Users/box406/.phpenv/bin:$PATH"
eval "$(phpenv init -)"

.zshrc を反映

$ source ~/.zshrc

php-build のinstall

$ git clone https://github.com/CHH/php-build.git
$ cd php-build/
$ sh install.sh
Installing php-build in /usr/local
-n   - Creating Directories...
 Done.
-n   - Copying files...
 Done.

php のintall

install できるバージョンの確認

$ php-build --definitions
5.2.17
5.3.10
5.3.11
5.3.11RC1
5.3.11RC2
5.3.12
5.3.13
5.3.14
5.3.15
5.3.16
5.3.17
5.3.18
5.3.19
5.3.19RC1
5.3.2
5.3.20
5.3.20RC1
5.3.21
5.3.22
5.3.23
5.3.3
5.3.6
5.3.8
5.3.9
5.3.9RC3
5.3.9RC4
5.3snapshot
5.4.0
5.4.0RC1
5.4.0RC2
5.4.0RC3
5.4.0RC4
5.4.0RC5
5.4.0RC6
5.4.0RC7
5.4.0RC8
5.4.0alpha3
5.4.0beta1
5.4.0beta2
5.4.1
5.4.10
5.4.10RC1
5.4.11
5.4.12
5.4.13
5.4.1RC1
5.4.1RC2
5.4.2
5.4.3
5.4.4
5.4.5
5.4.6
5.4.7
5.4.8
5.4.9
5.4.9RC1
5.4snapshot
5.5.0alpha1
5.5.0alpha2
5.5.0alpha3
5.5.0alpha4
5.5.0alpha5
5.5.0alpha6
5.5.0beta1
5.5snapshot

php 5.3.20 のinstall

  • php-buildはapacheのモジュールを生成しないので以下を追加/削除

      $ sudo vim /usr/local/share/php-build/default_configure_options 
      -- --enable-fpm
      ++ --with-apxs2=/usr/local/sbin/apxs
    
  • install

      $ php-build 5.3.20 ~/.phpenv/versions/5.3.20
      [Info]: Loaded apc Plugin.
      [Info]: Loaded pyrus Plugin.
      [Info]: Loaded xdebug Plugin.
      [Info]: Loaded xhprof Plugin.
      [Info]: php.ini-production gets used as php.ini
      [Info]: Building 5.3.20 into /Users/box406/.phpenv/versions/5.3.20
      [Downloading]: http://php.net/distributions/php-5.3.20.tar.bz2
      [Preparing]: /var/tmp/php-build/source/5.3.20
    
      -----------------
      |  BUILD ERROR  |
      -----------------
    
      Here are the last 10 lines from the log:
    
      -----------------------------------------
      configure: warning: You will need re2c 0.13.4 or later if you want to regenerate PHP parsers.
      configure: error: jpeglib.h not found.
      -----------------------------------------
    
      The full Log is available at '/tmp/php-build.5.3.20.20130401134148.log'.
      [Warn]: Aborting build.
    
  • errorが表示されたので必要なライブラリを追加

      brew install re2c
      brew install jpeg
      brew install libpng
      brew install libmcrypt
      brew install libiconv
    
  • 「_php_iconv_string in iconv.o」とかが一杯でた場合の対処方法

      $ cd /var/tmp/php-build/source/[version]/ext/iconv/
      $ cp iconv.c iconv.c.back
      $ vim iconv.c
    
      iconv_open を libiconv_open に変更
      iconv_close を libiconv_close に変更
    
      :%s/iconv_open/libiconv_open/g
      :%s/iconv_close/libiconv_close/g
    
  • モジョールの退避

      mv /usr/local/Cellar/httpd/2.2.23/libexec/libphp5.so ~/.phpenv/versions/5.3.20
    

同じ要領でphp 5.4のinstallを行う

phpenv でバージョンの切換え

現在のバージョンを確認

$ phpenv versions
* system
  5.3.20
  5.4.13

5.4.13に切り替える

$ phpenv global 5.4.13

バージョンが5.4.13に変わっているか確認

$ php -v
PHP 5.4.13 (cli) (built: Apr  1 2013 17:39:20)
Copyright (c) 1997-2013 The PHP Group
Zend Engine v2.4.0, Copyright (c) 1998-2013 Zend Technologies
    with Xdebug v2.2.1, Copyright (c) 2002-2012, by Derick Rethans

apache のモージョルの切換え

本当は「phpenv-apache-version」を使いたいがsoファイルが「/usr/local/opt/httpd/libexec」にコピーされてしまうのでとりあえず手動でコピする

退避しているsoファイルを以下のコマンドでコピーする

php 5.4.13

cp .phpenv/versions/5.4.13/libphp5.so /usr/local/Cellar/httpd/2.2.23/libexec

php 5.3.20

cp .phpenv/versions/5.3.20/libphp5.so /usr/local/Cellar/httpd/2.2.23/libexec

これでとりあえずapacheと連携して動作する環境ができたと思います

phpinfo();でバージョンが切り替わってるいるか確認を行って下さい