El Capitanに標準でインストールされているApacheについて調べる

$ httpd -v
Server version: Apache/2.4.16 (Unix)
Server built:   Jul 31 2015 15:53:26

起動と停止

  • 起動

      $ sudo apachectl start
    

    http://localhostにアクセスしてIt works!と表示されることを確認
    これは/Library/WebServer/Documentsディレクトリ配下の内容が表示される

  • 停止

      $ sudo apachectl stop
    
  • 自動起動させる設定

      $ sudo launchctl load -w /System/Library/LaunchDaemons/org.apache.httpd.plist
    

設定

http://localhostの表示対象の/Library/WebServer/Documents配下はシステムで共通で利用されるので、 ユーザ毎に利用するユーザディレクトリを利用できるように設定する。
Apacheの各種設定ファイルは/etc/apache2ディレクトリ配下にある

httpd.confの編集

/etc/apache2/httpd.confを編集する

#LoadModule userdir_module libexec/apache2/mod_userdir.so
↓
LoadModule userdir_module libexec/apache2/mod_userdir.so

#Include /private/etc/apache2/extra/httpd-userdir.conf
↓
Include /private/etc/apache2/extra/httpd-userdir.conf

httpd-userdir.confの編集

/etc/apache2/extra/httpd-userdir.confを編集する

#Include /private/etc/apache2/users/*.conf
↓
Include /private/etc/apache2/users/*.conf

/etc/apache2/usersディレクトリ配下に自分のユーザディレクトリ用の設定ファイルを作成する。
なお、/etc/apache2/usersディレクトリ配下に参考となるGuest.confというファイルがあるのでコピー

$ sudo cp /etc/apache2/users/Guest.conf /etc/apache2/users/{ユーザ名}.conf

{ユーザ名}.confの編集

<Directory "/Users/{ユーザ名}/Sites/">
    Options Indexes MultiViews
    Options +FollowSymLinks
    AllowOverride All
    Require all granted
</Directory>

動作確認

設定ファイルの変更をチェックしてリスタート

$ apachectl configtest
$ sudo apachectl restart

追記(2017/05/12)

Sierraでこれらの設定をしたら、configtest実行時にエラーが表示された。

$ apachectl configtest
AH00557: httpd: apr_sockaddr_info_get() failed for <xxxxxx>
AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using 127.0.0.1. Set the 'ServerName' directive globally to suppress this message

どうも<xxxxxx>(伏せ字)のサーバの名前が解決できないという内容のようだ。
調べて見ると/etc/apache2/httpd.conf

# If your host doesn't have a registered DNS name, enter its IP address here.

というコメントがあるので、以下のように設定したところエラーが消えた。

#ServerName www.example.com:80
↓
ServerName 127.0.0.1:80

index.htmlの作成

ユーザディレクトリを作成

$ mkdir ~/Sites

~/Sitesディレクトリ配下に適当にindex.htmlを作成

http://localhost/~{ユーザ名}/にアクセスして、 今作成したindex.htmlが表示されることを確認

参考