MacPortsでApache+MySQL+PHPをインストールしてWebアプリケーション開発環境を作成します。
Mac OS X 10.6 Snow LeopardにMacPortsとDeveloperToolsをインストールしていることを前提にしています。
Apacheのインストールと設定
MacPortsでApacheをインストールします。
$ sudo port install apache2
インストールの最後に以下の文が表示されます。
###########################################################
# A startup item has been generated that will aid in
# starting apache2 with launchd. It is disabled
# by default. Execute the following command to start it,
# and to cause it to launch at startup:
#
# sudo launchctl load -w /Library/LaunchDaemons/org.macports.apache2.plist
###########################################################
Apacheはlaunchdで開始するようになっています。デフォルトでは無効になっているので、sudo launchctl load -w /Library/LaunchDaemons/org.macports.apache2.plistを実行して有効にします。有効にするとApacheが起動します。また、OSを再起動してもApacheが自動的に起動されるようになります。
Apacheの動作確認のため、ブラウザでlocalhostもしくはシステム環境設定の共有で指定したコンピュータ名.localにアクセスします。正しく動作していれば「It works!」と表示されます。
開発環境なので、Apacheのドキュメントルートを~/Sitesフォルダに、実行ユーザを自分のユーザアカウントに変更します(ルートを変更せずに#Include conf/extra/httpd-userdir.confの#を削って有効にすれば、ユーザごとにSitesフォルダが使用できるようになります。)。さらに、機能や設定を自由に変更できるようにします。
/opt/local/apache2/conf/httpd.confファイルの
User wwwをUser yourusername
DocumentRoot “/opt/local/apache2/htdocs”をDocumentRoot “/Users/yourusername/Sites”
<Directory “/opt/local/apache2/htdocs”>を<Directory “/Users/yourusername/Sites”>
(上記Directory内の)Options Indexes FollowSymLinksをOptions All、AllowOverride NoneをAllowOverride All
に変更します。
これらの変更を以下のコマンドでApacheに適用します。
$ sudo /opt/local/apache2/bin/apachectl graceful
再度localhostにアクセスすると、/Users/yourusername/Sitesが表示されます。
MySQLのインストールと設定
続いてMySQLをインストールします。
$ sudo port install mysql5-server
起動する前にデータベースの設定をしておきます。my.cnfを設置して編集します。
$ sudo cp /opt/local/share/mysql5/mysql/my-medium.cnf /opt/local/etc/mysql5/my.cnf
[client]
default-character-set=utf8
[mysqld]
datadir=/Users/yourusername/local/var/mysql
language=/opt/local/share/mysql5/mysql/japanese
default-character-set=utf8
skip-character-set-client-handshake
[mysqldump]
default-character-set=utf8
[mysql]
default-character-set=utf8
MacPortsでインストールしたときに以下の文が表示されます。
###########################################################
# A startup item has been generated that will aid in
# starting mysql5-server with launchd. It is disabled
# by default. Execute the following command to start it,
# and to cause it to launch at startup:
#
# sudo launchctl load -w /Library/LaunchDaemons/org.macports.mysql5.plist
###########################################################
******************************************************
* In order to setup the database, you might want to run
* sudo -u mysql mysql_install_db5
* if this is a new install
******************************************************
これまでMySQLをインストールしたことがない場合はsudo -u mysql mysql_install_db5を実行します。sudo launchctl load -w /Library/LaunchDaemons/org.macports.mysql5.plistを実行して起動します。mysql5を実行するかSequel ProでMySQLサーバに接続して、動作しているか確認します。ソケットは/opt/local/var/run/mysql5/mysqld.sockです。
PHPのインストールと設定
PHP5.2系をインストールします。以下の設定が可能です。
$ port variants php52
php52 has the variants:
apache: Add Apache 1 web server module
* conflicts with apache2 no_web
[+]apache2: Add Apache 2.2 web server module
* conflicts with apache no_web
darwin_10: Platform variant, selected automatically
dbase: Add dBase file format support
debug: Enable debug support (useful to analyze a PHP-related core dump)
fastcgi: Add FastCGI web server binary
* conflicts with no_web
gmp: Add GNU MP multiprocessing functions
imap: Add IMAP protocol support
ipc: Add semaphore, shared memory and IPC functions
macosx: Platform variant, selected automatically
macports_snmp: Add SNMP support using MacPorts SNMP
* conflicts with snmp
mssql: Add MS-SQL server support
mysql4: Add MySQL 4 support
* conflicts with mysql5
mysql5: Add MySQL 5 support
* conflicts with mysql4
no_web: Don't include any web server support
* conflicts with apache apache2 fastcgi
oracle: Add Oracle oci8 database functions with the Oracle Instant Client
pcntl: Add process control functions
pear: Add PEAR
postgresql82: Add postgresql82 support
* conflicts with postgresql83
postgresql83: Add postgresql83 support
* conflicts with postgresql82
pspell: Add pspell spell-checking functions
readline: Add GNU readline functions
snmp: Add SNMP support using Apple SNMP
* conflicts with macports_snmp
sockets: Add socket communication functions
sqlite: Add SQLite support
suhosin: Add Suhosin patch
t1lib: Add PostScript Type 1 font support with t1lib
tidy: Add Tidy support
universal: Build for multiple architectures
とりあえずApache2 / MySQL / PEARを指定しますが、たとえばSQLiteのサポートが必要であれば+sqliteを指定したり、必要なものを指定します。また、PHP 5.3系を利用する場合はphp52ではなくphp5ポートを利用し、+mysqlではなくphp5-mysqlポートをインストールします。
$ sudo port install php52 +apache2+mysql5+pear
To customize php, copy
/opt/local/etc/php5/php.ini-dist (if this is a development server) or
/opt/local/etc/php5/php.ini-recommended (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設定ファイルを配置して、PHPをApacheに組み込みます。
$ sudo cp /opt/local/etc/php5/php.ini-dist /opt/local/etc/php5/php.ini
$ sudo /opt/local/apache2/bin/apxs -a -e -n "php5" libphp5.so
/opt/local/apache/conf/httpd.confを編集します。
DirectoryIndexにindex.phpを追加します。
<IfModule dir_module>
DirectoryIndex index.html index.php
</IfModule>
httpd.confの最後に以下を追加します。
Include conf/extra/mod_php.conf
Apacheに設定を反映させます。
$ sudo /opt/local/apache2/bin/apachectl graceful
PHPの動作をテストするために、~/Sites/info.phpファイルを以下の内容で作成します。
http://localhost/info.phpにアクセスして、PHPの動作を確認します。PHPの情報が表示されればインストール成功です。