🌐

Apache でバーチャルホストを作成する

2025/01/18に公開

この記事について

Apache でバーチャルホストを作成したときの作業メモです。

行いたいこと

  • このポスト の環境の node4 上で稼働している Apache にバーチャルホストの設定を追加する
  • http://node4.example.jp で node4 のページを表示する(現行のまま)
  • http://v1.node4.example.jp でバーチャルホスト v1 のページを表示する
  • http://v2.node4.example.jp でバーチャルホスト v2 のページを表示する

バーチャルホスト用の名前解決用情報を DNS に登録

このポストで作成した DNS サーバーに、バーチャルホスト用の名前解決用情報を登録します。

set system static-host-mapping host-name v1.node4.example.jp inet 192.168.100.104
set system static-host-mapping host-name v2.node4.example.jp inet 192.168.100.104

登録情報

ホスト名 DocumentRoot の path メモ
node4.example.jp /var/www/html/
v1.node4.example.jp /var/www/html1/ 今回作成するバーチャルホスト用その 1
v2.node4.example.jp /var/www/html2/ 今回作成するバーチャルホスト用その 2

テスト用ファイルの内容

/var/www/html1/index.html
virtualhost running on node 4 Part 1
/var/www/html2/index.html
virtualhost running on node 4 Part 2

設定ファイルの配置先ディレクトリー

/etc/httpd/conf.d/README ファイルの内容に従い /etc/httpd/conf.d/ ディレクトリーに配置します。設定ファイル名は virtualhost.conf にします。

/etc/httpd/conf.d/README
This directory holds configuration files for the Apache HTTP Server;
any files in this directory which have the ".conf" extension will be
processed as httpd configuration files.  The directory is used in
addition to the directory /etc/httpd/conf.modules.d/, which contains
configuration files necessary to load modules.

Files are processed in sorted order.  See httpd.conf(5) for more
information.

設定ファイル

/etc/httpd/conf.d/virtualhost.conf
<VirtualHost *:80>
  DocumentRoot /var/www/html
  ServerName any
</VirtualHost>

<VirtualHost *:80>
  DocumentRoot /var/www/html1
  ServerName v1.node4.example.jp
</VirtualHost>

<VirtualHost *:80>
  DocumentRoot /var/www/html2
  ServerName v2.node4.example.jp
</VirtualHost>

サービスの再起動と状態確認

sudo systemctl restart httpd.service
sudo systemctl status httpd.service

クライアント PC からの表示テスト

http://node4.example.jp/

2025-01-18_18h47_10.png

http://v1.node4.example.jp/

2025-01-18_18h48_19.png

http://v2.node4.example.jp/

2025-01-18_18h48_57.png

Discussion