MacでローカルにLAMP環境構築

OSのVersionはちょっと古いのですが10.6.8です。

Linux,UNIXの仕組みを理解するためにも、ターミナルから実行してみますが、今回はデフォルトでインストールされているパッケージを利用していきます。

Apacheの起動

デフォルトでされているので、起動スクリプトで起動します。

$ sudo /usr/sbin/apachectl start

正常起動しても標準出力にメッセージは表示されないようです。
スクリプトを読んでみるとスクリプトの終了コードをみれば正常動作なのかわかるっぽいので

$ echo $?

とシェルの特殊変数を調べ、これが0であることを確認。起動したっぽい。

$ httpd -v
Server version: Apache/2.2.17 (Unix)
Server built:   Dec  1 2010 09:58:15

と入力し起動していることを確認。2.2系であることを頭の隅においておく。

ここで、ブラウザを起動して、URLに"http://localhost/"と入力すると

 It works!

と表示されました。HTMLソースを覗いてみると

<html><body><h1>It works!</h1></body></html>

とすごく簡潔でなんていうか無駄がない。
あ、ターミナルから実行するという方針を忘れてブラウザ使っていたので、

$ curl http://localhost/
<html><body><h1>It works!</h1></body></html>

となりました。めでたしめでたし。

次に、DocumentRootのパスを調べたいので、

$ grep DocumentRoot /etc/apache2/httpd.conf 
# DocumentRoot: The directory out of which you will serve your
DocumentRoot "/Library/WebServer/Documents"
# This should be changed to whatever you set DocumentRoot to.
    # access content that does not live under the DocumentRoot.

と/Library/WebServer/Documentsっぽい。

$ ls /Library/WebServer/Documents
PoweredByMacOSX.gif      PoweredByMacOSXLarge.gif index.html.en

あれ、index.html.enってファイルなのか。多言語対応がデフォルトで有効になっているのか?

気になるけど今回は、DocumentRootのパスが分かったので、CGIの方に進みます。

CGIの実行

で、次にCGIを実行させていく。
まず、confファイル内をcgiという単語で調べる

$ grep cgi /etc/apache2/httpd.conf 
LoadModule proxy_scgi_module libexec/apache2/mod_proxy_scgi.so
LoadModule cgi_module libexec/apache2/mod_cgi.so
#LoadModule fastcgi_module     libexec/apache2/mod_fastcgi.so
    ScriptAliasMatch ^/cgi-bin/((?!(?i:webobjects)).*$) "/Library/WebServer/CGI-Executables/$1"
<IfModule cgid_module>
    # socket used to communicate with the CGI daemon of mod_cgid.
    #Scriptsock /private/var/run/cgisock
    #AddHandler cgi-script .cgi
#ErrorDocument 404 "/cgi-bin/missing_handler.pl"

cgiディレクトリが出てくると思ったんだけど、これかな。
ScriptAliasMatchというコマンドが何を意味しているのかわからないけど、とりあえず、これにあたりを付けてみる。
/Library/WebServer/CGI-Executables/

$ echo -e '#!/usr/bin/perl \nprint "Content-Type: text/plain\\n"; \nprint "\\n";\nprint "Hello World!"' > /Library/WebServer/CGI-Executables/hello.cgi
$ chmod u+x /Library/WebServer/CGI-Executables/hello.cgi   # ←補足:これだと動きません

echo の -e は改行を改行文字で出よくしてもらうため。で、http://localhost/hello.cgiにブラウザでアクセスしてみる

$ curl http://localhost/cgi-bin/hello.cgi
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>500 Internal Server Error</title>
</head><body>
<h1>Internal Server Error</h1>
<p>The server encountered an internal error or
misconfiguration and was unable to complete
your request.</p>
<p>Please contact the server administrator,
 you@example.com and inform them of the time the error occurred,
and anything you might have done that may have
caused the error.</p>
<p>More information about this error may be available
in the server error log.</p>
</body></html>

アスセスはできたもののinternal errorだとさ。internalErrorってことは、さっき作ったhello.cgiが実行されているはず。
ってことで、エラーメッセージをみるために、Apacheのログを確認。

$ tail -n 4 /var/log/apache2/error_log 
[Thu May 24 00:07:09 2012] [error] [client 127.0.0.1] File does not exist: /Library/WebServer/Documents/favicon.ico
[Thu May 24 00:07:10 2012] [error] [client 127.0.0.1] File does not exist: /Library/WebServer/Documents/favicon.ico
[Thu May 24 00:32:42 2012] [error] [client 127.0.0.1] (13)Permission denied: exec of '/Library/WebServer/CGI-Executables/hello.cgi' failed
[Thu May 24 00:32:42 2012] [error] [client 127.0.0.1] Premature end of script headers: hello.cgi

ふむ。パーミッションがおかしいと。
Apacheのプロセスからcgiって起動させられるんだっけ(要確認)
ってことは

$ chmod g+x /Library/WebServer/CGI-Executables/hello.cgi

でいけるのではないでしょうか。

$  curl http://localhost/cgi-bin/hello.cgi
Hello World!

いけた。

MySQL

DMGからパッケージをインストールしてしまいます。下記の記事を参考に実行しました。
Mac OSX で MySQLを動かすまでのメモ

その他寄り道メモ

スクリプトを読んでみる

/usr/sbin/apachectl 内で

    if [ $UID != 0 ]; then
        echo This operation requires root.
        exit 1
    fi

とあったのだけど、こうやってroot userを判別するのが一般的なのね。

あと、スクリプトの引数ってこうやって分けるやり方も、サクッと軽量のスクリプトを作る場合は楽なのかな。
一般的なコマンドのような引数機能を持たせたい場合は、getoptsが使えるみたい。

case $ARGV in
start)
    run_launchctl load -w $LAUNCHD_JOB
    ERROR=$?
    ;;
stop|graceful-stop)
    run_launchctl unload -w $LAUNCHD_JOB
    ERROR=$?
    ;;
restart|graceful)
    run_launchctl unload -w $LAUNCHD_JOB 2> /dev/null
    run_launchctl load -w $LAUNCHD_JOB
    ERROR=$?
    ;;
startssl|sslstart|start-SSL)
    echo The startssl option is no longer supported.
    echo Please edit httpd.conf to include the SSL configuration settings
    echo and then use "apachectl start".
    ERROR=2
    ;;
configtest)
    $HTTPD -t
    ERROR=$?
    ;;
status|fullstatus)
    echo Go to $STATUSURL in the web browser of your choice.
    echo Note that mod_status must be enabled for this to work.
    ;;
*)
    $HTTPD $ARGV
    ERROR=$?
esac