前回は誤って編集途中で公開してしまいました・・・
そんなワケで仕方なく完結させてしまったので、続きで書く予定だった分も備忘録っておく。
今回は複数サイトをXAMPPで使う場合。
要するに前回同様Virtual Hostの設定をしてhostsファイルで強制的に名前解決をやっちゃうワケです。
以前投稿した記事のローカル版みたいなもので、ネット上でもよく紹介されている方法ですな。
例として
| URL | ドキュメントルート |
|---|---|
| http://localhost | C:\XAMPP\htdocs |
| http://myhogehoge.com | C:\XAMPP\public_html\myhogehoge.com |
| http://myfugafuga.com | C:\XAMPP\public_html\myfugafuga.com |
として設定する場合のXAMPPの設定。
前回の設定はされていないものとして、インストールしたてホヤホヤの「C:\XAMPP」を前提に、
「C:\XAMPP\apache\conf\extra\httpd-vhosts.conf」を編集。
# Use name-based virtual hosting. # ##NameVirtualHost *:80
今回はポート8080は使用しないので変更はコメントアウト部分を外すのみ。
# Use name-based virtual hosting. # NameVirtualHost *:80
同じくhttpd-vhosts.confでhost毎の設定を変更。
##<VirtualHost *:80>
##ServerAdmin postmaster@dummy-host.localhost
##DocumentRoot "/xampp/htdocs/dummy-host.localhost"
##ServerName dummy-host.localhost
##ServerAlias www.dummy-host.localhost
##ErrorLog "logs/dummy-host.localhost-error.log"
##CustomLog "logs/dummy-host.localhost-access.log" combined
##</VirtualHost>
##<VirtualHost *:80>
##ServerAdmin postmaster@dummy-host2.localhost
##DocumentRoot "/xampp/htdocs/dummy-host2.localhost"
##ServerName dummy-host2.localhost
##ServerAlias www.dummy-host2.localhost
##ErrorLog "logs/dummy-host2.localhost-error.log"
##CustomLog "logs/dummy-host2.localhost-access.log" combined
##</VirtualHost>
を下記のように。
<VirtualHost *:80>
DocumentRoot "/xampp/htdocs"
ServerName localhost
</VirtualHost>
<VirtualHost *:80>
DocumentRoot "/xampp/public_html/myhogehoge.com"
ServerName myhogehoge.com
</VirtualHost>
<VirtualHost *:80>
DocumentRoot "/xampp/public_html/myfugafuga.com"
ServerName myfugafuga.com
</VirtualHost>
頭に www をつけた場合も同じにしたい場合は ServerAlias を使いましょう。
<VirtualHost *:80>
DocumentRoot "/xampp/htdocs"
ServerName localhost
</VirtualHost>
<VirtualHost *:80>
DocumentRoot "/xampp/public_html/myhogehoge.com"
ServerName myhogehoge.com
ServerAlias www.myhogehoge.com
</VirtualHost>
<VirtualHost *:80>
DocumentRoot "/xampp/public_html/myfugafuga.com"
ServerName myfugafuga.com
ServerAlias www.myfugafuga.com
</VirtualHost>
当然今回もこの状態では動作しないw
ひとつ上の階層の「C:\XAMPP\apache\conf\httpd.conf」を編集。
# # Listen: Allows you to bind Apache to specific IP addresses and/or # ports, instead of the default. See also the <VirtualHost> # directive. # # Change this to Listen on specific IP addresses as shown below to # prevent Apache from glomming onto all bound IP addresses. # #Listen 0.0.0.0:80 #Listen [::]:80 Listen 80
今回は上記部分の変更は不要なのでスルー。
<Directory "/xampp/htdocs">
#
# Possible values for the Options directive are "None", "All",
# or any combination of:
# Indexes Includes FollowSymLinks SymLinksifOwnerMatch ExecCGI MultiViews
#
# Note that "MultiViews" must be named *explicitly* --- "Options All"
# doesn't give it to you.
#
# The Options directive is both complicated and important. Please see
# http://httpd.apache.org/docs/2.2/mod/core.html#options
# for more information.
#
Options Indexes FollowSymLinks Includes ExecCGI
#
# AllowOverride controls what directives may be placed in .htaccess files.
# It can be "All", "None", or any combination of the keywords:
# Options FileInfo AuthConfig Limit
#
AllowOverride All
#
# Controls who can get stuff from this server.
#
Order allow,deny
Allow from all
</Directory>
この部分を変更するワケですが、コメントアウトが多いので除去した。
<Directory "/xampp/htdocs">
Options Indexes FollowSymLinks Includes ExecCGI
AllowOverride All
Order allow,deny
Allow from all
</Directory>
<Directory "/xampp/public_html/myhogehoge.com">
Options Indexes FollowSymLinks Includes ExecCGI
AllowOverride All
Order allow,deny
Allow from all
</Directory>
<Directory "/xampp/public_html/myfugafuga.com">
Options Indexes FollowSymLinks Includes ExecCGI
AllowOverride All
Order allow,deny
Allow from all
</Directory>
下2つはまとめても動くかも。
<Directory "/xampp/public_html/">
Options Indexes FollowSymLinks Includes ExecCGI
AllowOverride All
Order allow,deny
Allow from all
</Directory>
ただ使用しない「/xampp/public_html/」にもアクセス権が残るので若干気持ち悪い。
ローカルテスト程度なら問題ないけど。
最後にhostsファイルの設定です。
通常なら「C:\WINDOWS\system32\drivers\etc」のやつです。
127.0.0.1 localhost 127.0.0.1 myhogehoge.com 127.0.0.1 myfugafuga.com
の2行を追加。
もし上でwwwのエイリアス設定を追加していた場合はwwwも。
127.0.0.1 localhost 127.0.0.1 myhogehoge.com 127.0.0.1 myfugafuga.com 127.0.0.1 www.myhogehoge.com 127.0.0.1 www.myfugafuga.com
これで準備はOK。
前回同様
- C:\XAMPP\public_html\myhogehoge.com
- C:\XAMPP\public_html\myfugauga.com
のフォルダを作成した上でApacheを再起動すればアクセスできる筈です。
うまくいかない場合は焦らずに
- 記述ミスがないか確認
- スーパーリロード(Ctrl+F5)を試す
- ブラウザ再起動
等をしてみましょう。
