Macで Apache2 + PHP 環境を作成する

目標

NetBeansSymfony を使った Hello World アプリを作成するところまで行きます。
今回は、apache2とphpをインストールして連動するまで。
apachephpもデフォルトでインストールされてますが、pearが入ってないこと色々いじっても後で削除できるので別にしました。

前提

Apache2のインストール

MacPortsでインストールします。

$ sudo port install apache2
Password:
Warning: xcodebuild exists but failed to execute
Warning: port definitions are more than two weeks old, consider using selfupdate
Warning: Xcode appears to be installed but xcodebuild is unusable; some ports will likely fail to build.
Warning: You may need to run `sudo xcode-select -switch /Applications/Xcode.app`
--->  Computing dependencies for apache2
--->  Dependencies to be installed: openssl zlib pcre bzip2
--->  Extracting zlib
Error: Couldn't determine your Xcode version (from '/usr/bin/xcodebuild -version').
Error: 
Error: If you have not installed Xcode, install it now; see:
Error: http://guide.macports.org/chunked/installing.xcode.html
Error: 
Error: Target org.macports.extract returned: unable to find Xcode
Error: Failed to install zlib
Log for zlib is at: /opt/local/var/macports/logs/_opt_local_var_macports_sources_rsync.macports.org_release_tarballs_ports_archivers_zlib/zlib/main.log
Error: The following dependencies were not installed: openssl zlib pcre bzip2
Error: Status 1 encountered during processing.
To report a bug, see <http://guide.macports.org/#project.tickets>

Xcodeインストール済みなのに、Xcodeの場所が不明と表示されたので上記アラートの通り、場所を指定する。

$ sudo xcode-select -switch /Applications/Xcode.app

もう一度実行。こんどはOK。

$ sudo port install apache2

launchctl に追加しておきます。自動起動設定追加

$ sudo launchctl load /Library/LaunchDaemons/org.macports.apache2.plist
$ sudo launchctl load -w /Library/LaunchDaemons/org.macports.apache2.plist

起動確認

$ sudo launchctl start org.macports.apache2

起動できひんかった。。

$  sudo /opt/local/apache2/bin/apachectl start
dyld: Library not loaded: /opt/local/lib/libexpat.1.dylib
  Referenced from: /opt/local/apache2/bin/httpd
  Reason: Incompatible library version: httpd requires version 8.0.0 or later, but libexpat.1.dylib provides version 7.0.0
/opt/local/apache2/bin/apachectl: line 78: 40429 Trace/BPT trap: 5       $HTTPD -k $ARGV

libexpat1.dylib のバージョンが古いといっているようだ。
Google先生で調べたところ以下のコマンドで最新化できるようなので実行

$ sudo port selfupdate
$ sudo port clean expat apache2
$ sudo port -n upgrade --force expat apache2

【参考】svn - OSX Error installing subversion via macports - Stack Overflow

再び実行。やっと動いた。。

$ sudo /opt/local/apache2/bin/apachectl start
httpd: Could not reliably determine the server's fully qualified domain name, using altavista2.local for ServerName

PHP

こちらもMacPortsでインストール。

$ sudo port install php5 +apache2 +mysql5 +sqlite +pear
〜中略〜
o customize php, copy
/opt/local/etc/php5/php.ini-development (if this is a development server) or
/opt/local/etc/php5/php.ini-production (if this is a production server) to
/opt/local/etc/php5/php.ini and then make changes.

If this is your first install, you need to activate PHP in your web server.

To enable PHP in Apache, run
  cd /opt/local/apache2/modules
  /opt/local/apache2/bin/apxs -a -e -n "php5" libphp5.so

php.iniの作成

php.ini-developからコピーします。

$ sudo cp /opt/local/etc/php5/php.ini-development /opt/local/etc/php5/php.ini

Apath2とphpの連動

$ sudo vi /opt/local/apache2/conf/httpd.conf

php.iniの場所をhttpd.conf に指定

以下を最後に追記。

# for php
PHPIniDir "/opt/local/etc/php5/""

拡張子追加

MINEにphp拡張子を追加します。

# For php
AddType application/x-httpd-php .php

php5用moduleの追加

コマンドでモジュールを追加します。

$ cd /opt/local/apache2/modules
$ sudo /opt/local/apache2/bin/apxs -a -e -n "php5" libphp5.so

以下の行がhttpd.conf に追加されます。

LoadModule php5_module        modules/libphp5.so

動作確認

$ cd /opt/local/apache2/htdocs
$ sudo touch phpinfo.php
$ sudo vi phpinfo.php

以下の内容で作成します。

<?php
  phpinfo();
?>

以下のURLにアクセス

http://localhost/phpinfo.php

終わりに

意外に時間がかかりました。。次はSymfonyをインストールしてHelloWorldまでいきたい。