LoginSignup
1
4

More than 3 years have passed since last update.

Apache2.4でバーチャルホストの設定(超初級)

Posted at

前提条件

  • OS:CentOS7系
  • Apache:2.4系

コンテンツの準備

サーバー側でコンテンツの準備をする。

サイト構成

var/www/html
         ├ index.html
         ├ site_a
         │  └ index.html
         └ site_b
            └ index.html

↑このようにする。以下具体的な手順。

/var/www/html

内に「index.html」を作成。

<meta charset="UTF-8">
<title>テスト</title>
<body>
<h1>This is other SITE!</h1>
</body>

と書く。これは例外のドメインで来たときに表示されるページ。

次に

/var/www/html

内に

  • site_a
  • site_b

を作り、その中にそれぞれ「index.html」を作成。

site_aのindex.htmlに

<meta charset="UTF-8">
<title>テスト</title>
<body>
<h1>This is SITE A!</h1>
</body>

site_bのindex.htmlに

<meta charset="UTF-8">
<title>テスト</title>
<body>
<h1>This is SITE B!</h1>
</body>

と記述。

テスト用のローカル設定

hostsの設定

サーバーのIPが、192.168.33.1だとする。(もちろんグローバルIPでも可)
ローカルのhostsに

      192.168.33.1     a.test
      192.168.33.1     b.test
      192.168.33.1     hoge.test

と記述。

ローカルのブラウザで
http://a.test
を見てみる。

This is other SITE!

になっている。まだバーチャルホストの設定をしていないので当然。
サーバー側の操作に戻る。

/etc/httpd/conf.d/

に「vhost.conf」などという名前のファイル(拡張子だけconfになっていればファイル名は何でもよい)を作成して、以下のように記述。

<VirtualHost *:80>
  ServerName any
  DocumentRoot /var/www/html
</VirtualHost>
<VirtualHost *:80>
  ServerName a.test
  DocumentRoot /var/www/html/site_a
</VirtualHost>
<VirtualHost *:80>
  ServerName b.test
  DocumentRoot /var/www/html/site_b
</VirtualHost>

文法チェック

apachectl configtest

httpd の再起動

sudo systemctl restart httpd

ローカルのブラウザで
http://a.test
を見てみる。

This is SITE A!

http://b.test
を見てみる。

This is SITE B!

http://hoge.test
を見てみる。

This is other SITE!

成功!

1
4
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
1
4