ubuntu版 Apache2.4でバーチャルホストの設定

開発環境

ApacheをApacheと呼んで何が悪い?

CentOSしか触ってこなかった私にしてみれば、悪いどころか、極悪人なのです。

あなたの会社の社長が山田太郎さんだったとして、あなたは社長のことを「山田」と呼ぶのか?

「社長」じゃないの?

同様に、Apacheは実際のオペレーションでは「httpd」と呼ぶのです。

山田太郎は仕事上は「社長」と呼ばれるべきなのです。

しかし、だ。

Ubuntuとかその仲間達はApacheをApacheと、いや、あろうことかApache「2」と呼ぶようなヤツらなのだ。

$ systemctl start apache2

ほら、社長に向かって「山田太郎!」って!


さて、本題。雑ですが、本題。

confファイル をごにょごにょする。

ubuntu20.04上のApache2.4で、"vagrant.test" をホストネームにして、
"http://hoge.vagrant.test"、"http://fuga.vagrant.test"
などでプロジェクトを作りやすくするためにはバーチャルホストを設定する必要があるが、その下準備みたいな。

ubuntu版Apacheのconf関連

まず、プログラム本体の設定ファイル。親玉。
CentOS版では /etc/httpd/に各種ファイルがあり、親玉は/etc/httpd/conf/httpd.confです。
Debian系では /etc/apache2/の中であり、親玉は/etc/apache2/apache2.conf。

/etc/apache2$ ll
total 88
drwxr-xr-x  8 root root  4096 Feb 18 12:08 ./
drwxr-xr-x 88 root root  4096 Feb 18 14:11 ../
-rw-r--r--  1 root root  7224 Aug 13  2020 apache2.conf
drwxr-xr-x  2 root root  4096 Feb 18 13:10 conf-available/
drwxr-xr-x  2 root root  4096 Feb 18 12:08 conf-enabled/
-rw-r--r--  1 root root  1782 Apr 14  2020 envvars
-rw-r--r--  1 root root 31063 Apr 14  2020 magic
drwxr-xr-x  2 root root 12288 Feb 18 12:13 mods-available/
drwxr-xr-x  2 root root  4096 Feb 18 12:13 mods-enabled/
-rw-r--r--  1 root root   320 Apr 14  2020 ports.conf
drwxr-xr-x  2 root root  4096 Feb 18 12:08 sites-available/
drwxr-xr-x  2 root root  4096 Feb 18 12:08 sites-enabled/

/etc/apache2/apache2.conf を開けてみる

145 # Include module configuration:
146 IncludeOptional mods-enabled/*.load
147 IncludeOptional mods-enabled/*.conf
148 
149 # Include list of ports to listen on
150 Include ports.conf

・・・中略・・・

221 # Include generic snippets of statements
222 IncludeOptional conf-enabled/*.conf
223 
224 # Include the virtual host configurations:
225 IncludeOptional sites-enabled/*.conf

modules、port、snippet、virtual hostについては、*.confというファイル を作って然るべき場所に置いておいたらロードするよ〜、ということですね。

conf-availableとconf-enabledなど、ペアになっているディレクトリについては、「*-available = 有効なconfファイル (読むとは言ってない)が入ってる」の中のconfファイルのうち、読み込みたいものだけを「*-enabled」の中にシンボリックリンクで置いておくということですね。

まあ、自分でconfファイルを作って使う場合は ***-enable の方で良いでしょう。

読み込んだり読み込まなかったりを切り替えたい場合は、***-availableの中に作って、読み込みたい時はちゃんとシンボリックリンクを張るということで解決。

vhost.confを作る

では、作業しましょう。

sites-enabledディレクトリに「000-default.conf」というファイルがあり、これがバーチャルホストの記述サンプルになっています。
このファイルをコピーして、vhost.conf(拡張子が.confであればファイル名は何でもいい)というのを作り、編集してます。

具体的な記述の仕方についてはいくらでも参考になる記事があるのでここでは省略します。
公式が一番いいんだろうけど、ちょっと情報が古いので英語版を参照した方がいい。

$ sudo cp sites-enabled/000-default.conf sites-enabled/vhost.conf && sudo vim sites-enabled/vhost.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 www.example.com

    ServerAdmin webmaster@localhost
    DocumentRoot /var/www/html

    # 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 *:80>
    ServerName vagrant.test
    ServerAlias www.vagrant.test
    ServerAdmin webmaster@localhost
    DocumentRoot /var/www/html
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

ちなみに、Apache2.4からは「NameVirtualHost」の記述は廃止されてます。
公式の日本語ページには残っちゃってたりするんだけどねww

hostsファイルにも追記

$ sudo vim /etc/hosts

127.0.0.1       localhost vagrant.test #←加筆
127.0.1.1       vagrant.vm      vagrant

# The following lines are desirable for IPv6 capable hosts
::1     localhost ip6-localhost ip6-loopback
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters

# Apacheを再起動
$ sudo systemctl restart apache2.service

これで、http://vagrant.test で「Apache2 Ubuntu Default Page」が表示されたらOK!!

サブドメインの追加

hoge.vagrant.test というサブドメインを作ります。
データを置くトップディレクトリを /var/www/html/hoge として進めます。

まずは、vagrant側の設定。

Vagrantfile側の設定いろいろ

いったんゲストマシンを出る

$ exit
ファイル最後部の end の上に以下を追記

  config.vm.hostname = "vagrant.test"
  config.hostsupdater.aliases = ["www.vagrant.test", "hoge.vagrant.test"]

ついでに、ホストとゲストのアレをアレしてやる

# config.vm.synced_folder "../data", "/vagrant_data"

のところを

  config.vm.synced_folder "./html", "/var/www/html",
    owner: 'www-data',
    group: 'www-data',
    create: true,
    mount_options: ['dmode=777', 'fmode=755']

ubuntu版のapacheではuser:group がいずれも"www-data"となっていることに注意。

仮想マシンを再起動

$ vagrant reload

# か〜ら〜の〜

$ vagrant ssh

ゲストマシンApache側の設定

# まずはhoge.vagrant.test のホームディレクトリを作る
$ mkdir /var/www/html/hoge
$ sudo touch /var/www/html/hoge/index.php

# 生成した index.phpに何か内容を書き込んでおく
# 非公開のローカルサーバーでは phpinfo()あたりをやる場合が多いかも

$ sudo vim sites-enabled/vhost.conf

<VirtualHost *:80>
    ServerName hoge.vagrant.test
    ServerAlias www.hoge.vagrant.test
    ServerAdmin webmaster@localhost
    DocumentRoot /var/www/html/hoge
    ErrorLog ${APACHE_LOG_DIR}/hoge.error.log
    CustomLog ${APACHE_LOG_DIR}/hoge.access.log combined
</VirtualHost>

$ sudo vim /etc/hosts
127.0.0.1       localhost vagrant.test hoge.vagrant.test #←加筆

$ sudo systemctl restart apache2

これで「http://hoge.vagrant.test」 が正常に表示されればOK!!

相変わらず雑な説明で申し訳ないですが、あくまでも備忘録ということで。
ただ、これでも必要以上に丁寧に説明してる部分もあったりなかったり・・・

今日はここまで!