LoginSignup
35
37

More than 1 year has passed since last update.

【Apache】VirtualHostを使ってみよう

Last updated at Posted at 2020-06-16

はじめに

VirtualHostとは

Apache バーチャルホスト説明書
バーチャルホストという用語は、1 台のマシン上で (company1.com と company2.com のような) 二つ以上のウェブサイトを扱う運用方法のことを指します。

Aliasとは何が違うのか?

複数のAliasを設定した場合と似てるのでは?と最初思ったのですが、
Aliasが一つのウェブサイトの中で違うページのものを違うディレクトリに置いてるのに対し、
VirtualHostで別個のウェブサイトを一つのマシン上で扱っているということになります。

VirtualHostの種類

  • IP ベース:各ウェブサイトに違う IP アドレスがあるバーチャルホスト
  • 名前ベース:それぞれの IP アドレスに 複数の名前があるバーチャルホスト

今回は名前ベースのバーチャルホストを扱います。

前提

下記記事で構築した環境を前提とします。

  • Windows10にVagrantをを入れてCentOS7をインストールしよう(123456)
  • ローカルでLAMP環境を構築しよう(014

使用ツール

  • Tera Term

手順

1. 【サーバー側】コンテンツ準備
2. 【ローカル側】設定変更
3. 【サーバー側】バーチャルホストの設定
4. 【ローカル側】動作確認

やってみよう

1. 【サーバー側】コンテンツ準備

下記のサイト構成でcentos7のほうに表示用のコンテンツを用意します。
まんま参考サイトさんの通り。

var/www/html
         ├ index.html …➀
         ├ site_a
         │  └ index.html…➁
         └ site_b
            └ index.html…➂

上記の➀~➂のファイルを下記のように記述します。

➀のindex.html
<meta charset="UTF-8">
<title>テスト</title>
<body>
<h1>This is other SITE!</h1>
</body>
➁のindex.html
<meta charset="UTF-8">
<title>テスト</title>
<body>
<h1>This is SITE A!</h1>
</body>
➂のindex.html
<meta charset="UTF-8">
<title>テスト</title>
<body>
<h1>This is SITE B!</h1>
</body>

2. 【ローカル側】設定変更

C:\Windows\System32\drivers\etc\hostsの設定を下記のように変更します。
IPアドレスの192.168.33.1は各自環境で読み替えてください。
私はcentos7にも\etc\hostsがあるから
そっちを直してしまって「?????」ってなっていました。愚か!

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

これで、http://192.168.33.1http://a.testが同じ結果を表示します。
もちろんhttp://b.testhttp://hoge.testも同様。
今のところどれにアクセスしても➀のファイルが表示されます。
3. 【サーバー側】バーチャルホストの設定
http://a.testhttp://b.testhttp://hoge.testの表示結果が変わります。

3. 【サーバー側】バーチャルホストの設定

centos7に/etc/httpd/conf.d/vhost.confを作成し、以下のように記述します。
パスは/etc/httpd/conf.d/でないとダメですが、ファイル名は拡張子が.confであればなんでもよいです。

vhost.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>

下記のコマンドで、vhost.confの文法に問題がないかチェックします。
Syntax OKと表示されればOKです。

apachectl configtest

下記のコマンドで、httpdを再起動します。

systemctl restart httpd

4. 【ローカル側】動作確認

ローカルのブラウザでhttp://a.testhttp://b.test
http://hoge.testをそれぞれ表示してみましょう。
http://a.testで➁のファイル、http://b.testで➂のファイル、
http://hoge.testで➀のファイルが表示されれば成功です。

補足:3. 【サーバー側】バーチャルホストの設定について

3. 【サーバー側】バーチャルホストの設定では、
/etc/httpd/conf.d/vhost.confというファイルを作成しましたが、
ファイルを作成せず、下記のように/etc/httpd/conf/httpd.confを編集するやり方もあるようです。
詳しくはこちらのサイトで。

/etc/httpd/conf/httpd.conf
(略)...

# Further relax access to the default document root:
<Directory "/var/www/html">
    #
    # 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.4/mod/core.html#options
    # for more information.
    #
    Options Indexes FollowSymLinks

    #
    # 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 None

    #
    # Controls who can get stuff from this server.
    #
    Require all granted
</Directory>

# 適当なサーバ名を設定
serverName testserver

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

(略)...

どう使い分けるとかあるんでしょうか。
あと、↓はなくても大丈夫でした。

# 適当なサーバ名を設定
serverName testserver

参考サイト

Apache バーチャルホスト説明書
ApacheのVirtualHostってなんだ
Apache2.4でバーチャルホストの設定(超初級)
Vagrant内でVirtualHostを設定する

関連ページ

Windows10にVagrantをを入れてCentOS7をインストールしよう

1. VagrantインストールからVagrantfileを設置まで
2. 仮想マシンの操作
3. WinSCP、Tera Termに秘密鍵でログイン
4. WinSCP、Tera Termにrootユーザーでパスワードログイン
5. zip/unzipをインストール
6. Vagrantにて仮想環境を配布

ローカルでLAMP環境を構築しよう

0. 事前準備
1. Apacheをインストール
4. ファイアウォールとか停止する

Apache

DocumentRootを変更しよう
Aliasを使ってみよう
VirtualHostを使ってみよう

おすすめ参考書

サーバ構築の実際がわかる Apache[実践]運用/管理 (Software Design plus)

閑話休題

最近の悩みはエンジニアの職業病である肩こりです。。。
私はコリコランを友人から教えてもらって愛用していますが結構効いてます!
専用シールもあると便利なのでおすすめです。
みなさんでもぜひ!

35
37
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
35
37