LoginSignup
7
10

More than 5 years have passed since last update.

MacのXamppでのヴァーチャルホストの設定(xamppをインストールした場所以外にアクセス出来るようにする)

Last updated at Posted at 2014-09-21

MacでXamppを使っていて、Xamppをインストールした場所以外の場所でもlocalサーバーとしてアクセスさせたい場合の設定方法をまとめました。

httpd.confの編集

Apacheの設定ファイルを編集します。XAMPPのApacheは初期設定ではバーチャルホストを許可していません。
/Application/XAMPP/etc/httpd.conf を開いて、460行目あたりの下記文言先頭にある「#」を外します

# Virtual hosts
# Include /Applications/XAMPP/etc/extra/httpd-vhosts.conf


# Virtual hosts
Include /Applications/XAMPP/etc/extra/httpd-vhosts.conf

これでバーチャルホストの設定ファイルを読み込むようになります。

httpd-vhosts.confの編集

次は、さきほどのhttpd.confで読み込むように設定したバーチャルホストのファイルを編集します。
注意点としては、バーチャルホストを有効化したら今まで設定していた「http://localhost/」の設定ももう一度やり直さないと行けません。
/Applications/XAMPP/xamppfiles/etc/extra/httpd-vhosts.conf
このファイルを編集します。
元々このファイルに書かれている下の部分はサンプルなので消すなりコメントアウト(全部の行頭に「#」を追加)しても問題ないです。

こちらをすべてコメントアウト

httpd-vhosts.conf
<VirtualHost *:80>
    ServerAdmin webmaster@dummy-host.example.com
    DocumentRoot "/Applications/XAMPP/xamppfiles/docs/dummy-host.example.com"
    ServerName dummy-host.example.com
    ServerAlias www.dummy-host.example.com
    ErrorLog "logs/dummy-host.example.com-error_log"
    CustomLog "logs/dummy-host.example.com-access_log" common
</VirtualHost>

<VirtualHost *:80>
    ServerAdmin webmaster@dummy-host2.example.com
    DocumentRoot "/Applications/XAMPP/xamppfiles/docs/dummy-host2.example.com"
    ServerName dummy-host2.example.com
    ErrorLog "logs/dummy-host2.example.com-error_log"
    CustomLog "logs/dummy-host2.example.com-access_log" common
</VirtualHost>

今まで使ってたドキュメントルートを設定

次に、今までlocalhostでアクセスしていた箇所を記載します。

httpd-vhosts.conf
<VirtualHost *:80>
    DocumentRoot "/Applications/XAMPP/xamppfiles/htdocs"
    ServerName localhost
</VirtualHost>

これを追加すれば今まで通り「http://localhost/」でXAMPPの設定画面にアクセスできます。
新しいドキュメントルートの追加
やっと新しいドキュメントルートを追加できます。以下のように記述しましょう。

httpd-vhosts.conf
<VirtualHost *:80>
    DocumentRoot "/Users/ユーザ名/YourDirectory/"
    ServerName local.domain.com
</VirtualHost>

hostsファイルの編集

設定の最後はhostsファイルの編集です。手動でやってもいいですが、今回はHosterというアプリを使います。
http://www.redwinder.com/macapp/hoster/

hostsファイルとは「このマシンのブラウザでアクセスしたURLの飛び先を設定するファイル」です。
ダウンロードしたアプリを開いて、左上のプラスボタンで新規セットを作成します。
「ホスト名」にさっき入力した新しいドメイン(ServerNameに書いた文字)を入力して、「IP」には「127.0.0.1」を入力しましょう。

最後にApacheの再起動

これを忘れると設定が反映されません。
設定したバーチャルホストにアクセスしてみましょう。

「アクセス出来ません」と出たら

ターミナルから
chmod 755 設定したフォルダ
で権限を付与してあげましょう。 

7
10
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
7
10