apachectl

Mac OS XはBSDですので、当然、Webサーバーとしても使えるわけです。Mac OS XにはApacheというWebサーバーソフトが入っていて、それを「パーソナルWeb共有」機能に利用しています。Apacheの本体は「httpd」という名前なのですが、httpdをコマンドとして打っても起動できません。Webサーバーの起動や終了には、専用のコマンド「apachectl」を使います(ひょっとしたら、httpdでも何かやればできるのかもしれませんが、私は知りません)。

まず、Apacheの起動は、「apachectl start」とします。

しかし、設定ファイルのアクセス権の問題があり、普通に「apachectl start」としても実行できません。

PowerBookG4:~ hiro$ apachectl start
Processing config directory: /private/etc/httpd/users/*.conf
fopen: Permission denied
httpd: could not open error log file /private/var/log/httpd/error_log.
/usr/sbin/apachectl start: httpd could not be started

そのため、suでrootになるかsudoを使って実行します(以下の例ではsuでrootに)。

Apacheの起動が、Mac OS XのパーソナルWeb共有にどう反映されるか見てみましょう。
Apacheの起動前に「システム環境設定」の「共有」パネルを表示した状態です。
開始前はパーソナルWeb共有のチェックは入っていません

PowerBookG4:~ hiro$ su
Password:
PowerBookG4:/Users/hiro root# apachectl start
Processing config directory: /private/etc/httpd/users/*.conf
/usr/sbin/apachectl start: httpd started

suでrootになってもう一度「apachectl start」すると、httpdが起動した旨のメッセージが表示されます。
この状態で、「システム環境設定」の「共有」パネルを表示してみると、このようにパーソナルWeb共有が開始されています。
開始後はパーソナルWeb共有のチェックが入っています

Apacheを停止する場合は「apachectl stop」とします。

PowerBookG4:/Users/hiro root# apachectl stop
/usr/sbin/apachectl stop: httpd stopped

また、Apacheを再起動するには「apachectl restart」とします。

PowerBookG4:/Users/hiro root# apachectl restart
/usr/sbin/apachectl restart: httpd restarted

ApacheはWebサーバーソフトですので、サーバーソフトならではの機能もあります。その時点で利用しているユーザーがいた場合は、そのユーザーのための処理は継続しつつ、段階的に再起動できます。この機能を使うには、「apachectl graceful」とします。

PowerBookG4:/Users/hiro root# apachectl graceful
/usr/sbin/apachectl graceful: httpd gracefully restarted

Apacheは複雑なソフトですので、設定が間違っていると起動しないことがあります。再起動失敗でサービス休止、ということにならないよう、設定ファイルをチェックする機能があります。「apachectl configtest」とすると、設定ファイルを検査し、文法上の間違いがなければ「Syntax OK」、構文エラーがあればエラーを表示します。

PowerBookG4:/Users/hiro root# apachectl configtest
Processing config directory: /private/etc/httpd/users/*.conf
Syntax OK

今回は、特に設定を変えずに、初期設定のままで使っていますので、特に問題は発見されませんでした。

Apacheの状態を表示させる機能もあるのですが、それにはテキストベースのWebブラウザlynxが必要ですので、特にいじっていないMac OS Xの場合、以下のようにlynxがないぞボケ、と言われます。

PowerBookG4:/Users/hiro root# apachectl status
/usr/sbin/apachectl: line 157: lynx: command not found
PowerBookG4:/Users/hiro root# apachectl fullstatus
/usr/sbin/apachectl: line 163: lynx: command not found

apachectlのヘルプ画面は、apachectl helpで表示できます。

PowerBookG4:/Users/hiro root# apachectl help
usage: /usr/sbin/apachectl (start|stop|restart|fullstatus|status|graceful|configtest|help)

start      - start httpd
stop       - stop httpd
restart    - restart httpd if running by sending a SIGHUP or start if 
             not running
fullstatus - dump a full status screen; requires lynx and mod_status enabled
status     - dump a short status screen; requires lynx and mod_status enabled
graceful   - do a graceful restart by sending a SIGUSR1 or start if not running
configtest - do a configuration syntax test
help       - this screen