Raspberry PiのApache2にドメイン名指定の仮想ホスト
サブドメインを用いて、Apache2のVirtualHostを設定します。
Raspberry Piで運用している「aaa.com」がApacheのメインサイトに設定されている環境で、「bbb.aaa.com」のサブドメインに新規WEBサーバーバーチャルホストを設定します。WEBリクエストのURLの違いにより、表示する/利用するWEBサーバーの切替が行われます。
前提条件など
VirtualHost向け設定ファイル
テンプレートファイル「000-default.conf」をコピーして、今回作成するVirtualHost設定ファイルの雛形を作成し、設定ファイルを作成します。
@raspberrypi:/etc/apache2/sites-available $ pwd
/etc/apache2/sites-available
@raspberrypi:/etc/apache2/sites-available $ sudo cp 000-default.conf bbb.aaa.com.conf
コピーして作成したbbb.aaa.com.confファイルを編集。
<VirtualHost *:80>
# The ServerName directive sets the request scheme, hostname and port that
# the server uses to identify itself. This is used when creating
# redirection URLs. In the context of virtual hosts, the ServerName
# specifies what hostname must appear in the request's Host: header to
# match this virtual host. For the default virtual host (this file) this
# value is not decisive as it is used as a last resort host regardless.
# However, you must set it for any further virtual host explicitly.
ServerName bbb.aaa.com
ServerAdmin webmaster@bbb.aaa.com
DocumentRoot /var/www/bbb.aaa.com
# Available loglevels: trace8, ..., trace1, debug, info, notice, warn,
# error, crit, alert, emerg.
# It is also possible to configure the loglevel for particular
# modules, e.g.
#LogLevel info ssl:warn
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
# For most configuration files from conf-available/, which are
# enabled or disabled at a global level, it is possible to
# include a line for only one particular virtual host. For example the
# following line enables the CGI configuration for this host only
# after it has been globally disabled with "a2disconf".
#Include conf-available/serve-cgi-bin.conf
</VirtualHost>
VirtualHost設定の有効化とApache2への反映
a2ensite コマンドを用いて、作成した設定ファイルをAvailable からEnabelへ。
sudo a2ensite bbb.aaa.com.conf
Apacheを再起動して、VirtualHostは利用可能となります。
sudo service apache2 restart
seeed studio
¥11,750 (2025/05/18 12:36:37時点 Amazon調べ-詳細)
アイ・オー・データ
¥8,480 (2025/05/17 17:49:05時点 Amazon調べ-詳細)