Skip to content

Instantly share code, notes, and snippets.

@shokuto
Last active December 13, 2015 17:48
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shokuto/4950143 to your computer and use it in GitHub Desktop.
Save shokuto/4950143 to your computer and use it in GitHub Desktop.
OSX10.8_virtual-host

MacOS X ローカル環境(バーチャルホスト)構築メモ

MacOS X 10.8.2 環境でローカルにWeb制作環境を構築する際の手順メモ。まずはバーチャルホストを構築する。ホームディレクトリの直下に「Sites」フォルダを作り、ここをローカル環境のルートにする。その中に「test.local」というフォルダを作り、これをバーチャルホストの一つにする、という設定。詳しい手順は「gaspanik / mt-lion-amp.md」にあるんだけれど、より詳しく自分用メモ。長くなるので、とりあえずApacheの設定まで。

注意

OS Xのバージョンによって設定ファイルが微妙に違っているようだし、マシンによってうまくいかないことも多々あったりするのですが、ここは作業メモなんで詳しいことはよくわかりません。

手順

Apacheの起動

sudo apachectl start

バーチャルホストの設定

バーチャルホストを設定するには /etc/apache2/httpd.conf に設定を書き込むことが必要。 httpd.conf を開くと、いちばん最後に

Include /private/etc/apache2/other/*.conf

と書いてある。otherフォルダ内の XXXX.conf ファイルの内容を読み込んでくれるっぽい。OS X 10.7だと書いてないみたいなので付け足しておくといい。

同一階層の extra フォルダに XXXX.conf のサンプルがあるので、この中の httpd-vhosts.conf をデスクトップにコピーして開く。書き方としてこれを参考にする。ただ、これ(VirtualHost *:80 の2つの記述)はそのままにしておくと、存在しないディレクトリをしているせいかエラーになるので、全部コメントアウトしておく。直前の記述と同じく先頭に「#」をつける。

# <VirtualHost *:80>
#     ServerAdmin webmaster@dummy-host2.example.com
#     DocumentRoot "/usr/docs/dummy-host2.example.com"
....

設定を追加する。内容は「gaspanik / httpd-vhosts.conf」を参考に。username の欄は自分のマシンのユーザー名(ホームディレクトリの名前)。test.local はSites 直下に作ったフォルダ名。

NameVirtualHost *:80

<Directory "/Users/username/Sites">
    Options Indexes FollowSymLinks MultiViews
    AllowOverride All
    Order allow,deny
    Allow from all
</Directory>

<VirtualHost _default_:80>
    ServerName localhost
    DocumentRoot /Library/WebServer/Documents
</VirtualHost>

<VirtualHost *:80>
  ServerName test.local
  DocumentRoot "/Users/username/Sites/test.local"
  DirectoryIndex index.html index.php
  CustomLog "/Users/username/Sites/logs/test.local-access_log" combined
  ErrorLog "/Users/username/Sites/logs/test.local-error_log"
</VirtualHost>

なお、このへんの記述に誤りがあるとApacheが起動しなくなるので

apachectl configtest

で設定ファイルに問題がないか確認するといい。ただし、エラーの箇所までは教えてくれないし、エラーが出ていても問題なく起動したりする。

編集した httpd-vhosts.conf を Sites/ に置くが、ここに置いても読みに行ってくれないので、 /etc/apache2/other の中にシンボリックリンクを置いて読ませる。

sudo ln -s ~/Sites/httpd-vhosts.conf /etc/apache2/other

Finderでシンボリックリンクをダブルクリックして開けるか(正しくリンクされているか)確認しておく。

続いて /etc/hosts に、今回のバーチャルホストの設定を加える。viで開く。

sudo vi /etc/hosts

127.0.0.1 の欄に半角スペースで区切って今回のを追加。

127.0.0.1 localhost test.local

viの操作は viの使い方/基本操作 など参照。

これでtest.localのアドレスで中味を読んでくれるはず。ここまででいったん確認するため、Apacheを再起動

sudo apachectl stop
sudo apachectl start

ここは

sudo apachectl restart

でもいいのかもしれない。で、

http://test.local

にアクセスすると、いけてるっぽい。試しに index.html ファイルとか作って置いてみても表示はOK。ほっ。

PHP+MySQL編につづく。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment