Skip to content

Instantly share code, notes, and snippets.

@mapk0y
Last active August 29, 2015 14:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mapk0y/59f021829c4264191805 to your computer and use it in GitHub Desktop.
Save mapk0y/59f021829c4264191805 to your computer and use it in GitHub Desktop.
Mavericks についてくる apache で vhost 設定

デフォルトの設定を確認

# apachectl -V
Server version: Apache/2.2.26 (Unix)
Server built:   Dec 10 2013 22:09:38
Server's Module Magic Number: 20051115:33
Server loaded:  APR 1.4.5, APR-Util 1.3.12
Compiled using: APR 1.4.5, APR-Util 1.3.12
Architecture:   64-bit
Server MPM:     Prefork
  threaded:     no
    forked:     yes (variable process count)
Server compiled with....
 -D APACHE_MPM_DIR="server/mpm/prefork"
 -D APR_HAS_SENDFILE
 -D APR_HAS_MMAP
 -D APR_HAVE_IPV6 (IPv4-mapped addresses enabled)
 -D APR_USE_FLOCK_SERIALIZE
 -D APR_USE_PTHREAD_SERIALIZE
 -D SINGLE_LISTEN_UNSERIALIZED_ACCEPT
 -D APR_HAS_OTHER_CHILD
 -D AP_HAVE_RELIABLE_PIPED_LOGS
 -D DYNAMIC_MODULE_LIMIT=128
 -D HTTPD_ROOT="/usr"
 -D SUEXEC_BIN="/usr/bin/suexec"
 -D DEFAULT_PIDLOG="/private/var/run/httpd.pid"
 -D DEFAULT_SCOREBOARD="logs/apache_runtime_status"
 -D DEFAULT_LOCKFILE="/private/var/run/accept.lock"
 -D DEFAULT_ERRORLOG="logs/error_log"
 -D AP_TYPES_CONFIG_FILE="/private/etc/apache2/mime.types"
 -D SERVER_CONFIG_FILE="/private/etc/apache2/httpd.conf"

インストールされている apache は v2.2.26 で設定ファイルは /private/etc/apache2/httpd.conf にあることがわかる。
ちなみに、/etc/private/etc の symlink なので、 /private/etc/apache2/httpd.conf /etc/apache2/httpd.conf と同一ファイル。

動作確認

確認がしやすいように、デフォルトの httpd-vhosts.conf に合わせて以下のようなファイルを作成

# head /usr/docs/dummy-host*/index.html
==> /usr/docs/dummy-host.example.com/index.html <==
dummy-host.example.com

==> /usr/docs/dummy-host2.example.com/index.html <==
dummy-host2.example.com

vhost を有効にしただけの状態でテスト

/private/etc/apache2/httpd.conf の差分

 # Virtual hosts
+Include /private/etc/apache2/extra/httpd-vhosts.conf
-#Include /private/etc/apache2/extra/httpd-vhosts.conf

設定ファイルのテスト

# apachectl -t
Syntax OK

apache を起動

# apachectl start

httpd-vhosts.conf に初めから設定されている dummy-host.example.com でテスト

# curl -LH 'Host: dummy-host.example.com' http://127.0.0.1:80/
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>403 Forbidden</title>
</head><body>
<h1>Forbidden</h1>
<p>You don't have permission to access /
on this server.</p>
</body></html>

apache を停止

# apachectl stop

vhost を修正して DocumentRoot にアクセス許可を設定してテスト

/private/etc/apache2/extra/httpd-vhosts.conf の差分

 <VirtualHost *:80>
     ServerAdmin webmaster@dummy-host.example.com
     DocumentRoot "/usr/docs/dummy-host.example.com"
     ServerName dummy-host.example.com
     ServerAlias www.dummy-host.example.com
+    <Directory "/usr/docs/dummy-host.example.com">
+        Order allow,deny
+        Allow from all
+    </Directory>
     ErrorLog "/private/var/log/apache2/dummy-host.example.com-error_log"
     CustomLog "/private/var/log/apache2/dummy-host.example.com-access_log" common
 </VirtualHost>

 <VirtualHost *:80>
     ServerAdmin webmaster@dummy-host2.example.com
     DocumentRoot "/usr/docs/dummy-host2.example.com"
     ServerName dummy-host2.example.com
+    <Directory "/usr/docs/dummy-host2.example.com">
+        Order allow,deny
+        Allow from all
+    </Directory>
     ErrorLog "/private/var/log/apache2/dummy-host2.example.com-error_log"
     CustomLog "/private/var/log/apache2/dummy-host2.example.com-access_log" common
 </VirtualHost>

設定ファイルのテスト

# apachectl -t
Syntax OK

apache を起動

# apachectl start

httpd-vhosts.conf に初めから設定されている dummy-host.example.com でテスト

# curl -LH 'Host: dummy-host.example.com' http://127.0.0.1:80/
dummy-host.example.com
# curl -LH 'Host: dummy-host2.example.com' http://127.0.0.1:80/
dummy-host2.example.com

おまけ

vhosts を有効にしただけの設定でなぜアクセス出来ないのか?

/etc/apache2/httpd.conf に以下の設定があるため

180 <Directory />
181     Options FollowSymLinks
182     AllowOverride None
183     Order deny,allow
184     Deny from all
185 </Directory>

vhost の設定をしなかったらなぜアクセスできたのか?

/etc/apache2/httpd.conf に以下の設定があるため

170 DocumentRoot "/Library/WebServer/Documents"
~~snip~~
197 <Directory "/Library/WebServer/Documents">
~~snip~~
222     Order allow,deny
223     Allow from all
224
225 </Directory>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment