Vagrant内でVirtualHostを設定する
やりたいこと
VirtualBox + Vagrantで作成したローカル開発環境上で、ひとつのサーバ内で複数のアプリやサイトの開発を行いたい。
そしてアプリケーションのルートディレクトリに対して、ホストを割り当てて本番環境と同じディレクトリ構成で確認ができるようにしたい。
ゲストOS内のディレクトリ (各アプリケーションが入っているルートディレクトリ) |
URL VirtualHost設定:なし |
URL VirtualHost設定:あり |
---|---|---|
/var/www/html | http://192.168.33.11/ | http://dev.root/ |
/var/www/html/app1 | http://192.168.33.11/app1 | http://dev.app1/ |
/var/www/html/app2/public | http://192.168.33.11/app2/public | http://dev.app2/ |
var/www/html/app1
をルートディレクトリとして公開するのであれば、virtualhostの設定がないと「192.168.33.11/app1」からでないとローカル上では確認できないため、app1というディレクトリが邪魔になる。 app2も同様。
環境
- ホストOS
- windows10
- VirtualBox 5.2.6
- Vagrant 2.0.2
- ゲストOS
手順
1. (ホストOS)hostsファイルにホスト名を書く
C:\Windows\System32\drivers\etc\hosts
を管理者権限で開き、各ホストの割り当てを追加する。
192.168.33.11 dev.root 192.168.33.11 dev.app1 192.168.33.11 dev.app2
※「vagrant-hostsupdater」というプラグインを使用すればこの工程を省くことができる。
しかしwindowsの場合、hostsのパーミッションを他のユーザーでも書き込めるように設定しないとvagrant up
で失敗する。
個人的にはこの権限を変更するのが憚れたので、手動でhostsを変更することにした。
2. (ゲストOS)httpd.confファイルにvirtualhostの設定を書く
vagrant ssh
でゲストOSに入ったら、管理者権限でhttpd.confファイルを編集する。
# 念のためバックアップを取っておく $ sudo cp /etc/httpd/conf/httpd.conf /etc/httpd/conf/httpd.conf.bk # viコマンドで編集 $ sudo vi /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> DocumentRoot "/var/www/html" ServerName dev.root </VirtualHost> # app1の設定 <VirtualHost *:80> DocumentRoot "/var/www/html/workspace/app1" ServerName dev.app1 </VirtualHost> # app2の設定 <VirtualHost *:80> DocumentRoot "/var/www/html/workspace/app2/public" ServerName dev.app2 </VirtualHost> # 追記ここまで # ------------------------------------- (略)...
※いろいろググってみると、このhttpd.confファイル内の# NameVirtualHost *:80
をコメントアウトする、という記事があるが、Apache2.4からはこの設定は不要になり、現在は記述が削除されているので注意。
基本的に、<VirtualHost *:80>
~ </VirtualHost>
に設定を書いていく。
DocumentRootにはその名の通りそのホストに接続したときのルートディレクトリを記述。
ServerNameには hostsファイルに加えたホスト名を記述する。
その他の設定は下記参考。
3. (ゲストOS)apache再起動して設定を反映
$ sudo systemctl restart httpd.service
これで各URLにアクセスして、DocumentRootのファイルが読み込まれて表示されればOK。
ハマったところ
- VirtualHostの設定でエラーログ、アクセスログの設定をする場合、ディレクトリを先に作っておかないとエラーとなる
- ホスト名にアンダースコアを含めるとApache 2.4.25 以降でステータスコード400のBad Requestとなる