Baccho Log

No Image

Sponsored Link

Virtual Host(サブドメイン)を使用する

  • 投稿日:
  • 更新日:
Tags:
vagrant Virtual Host VirtualBox XAMPP
Categories:
プログラミング

Virtual Hostとは?

ひとつのサーバーで複数のドメインを運用する技術のこと。
例えば、

  • localhost/vocaloid/index.html
  • localhost/wordpress/index.html
  • localhost/other/index.html

のそれぞれ違う三つのWEBサイトを制作しているとして、

  • local.vocalod
  • local.wordpress
  • local.other

というように、ドメインを変更することが出来ます。

複数の案件を管理するときなどに、わざわざ階層先を入力しなくてもよくなり、
分かりやすいドメインを設定出来るので、管理がとてもやりやすくなります。

XAMPP でVirtual Hostを設定する

XAMPPでのバーチャルホストの設定はとても簡単です。

  • httpd-vhosts.conf
  • hosts

この2つのファイルを編集するだけです。

httpd-vhosts.confの編集

C:/xampp/apache/conf/extra/httpd-vhosts.confをテキストエディタで開きます。

まず、下記の##NameVirtualHost *:80##を外します。

# Virtual Hosts
#
# Required modules: mod_log_config

# If you want to maintain multiple domains/hostnames on your
# machine you can setup VirtualHost containers for them. Most configurations
# use only name-based virtual hosts so the server doesn't need to worry about
# IP addresses. This is indicated by the asterisks in the directives below.
#
# Please see the documentation at 
# <URL:http://httpd.apache.org/docs/2.4/vhosts/>
# for further details before you try to setup virtual hosts.
#
# You may use the command line option '-S' to verify your virtual host
# configuration.

#
# Use name-based virtual hosting.
#
##NameVirtualHost *:80

次に末尾に下記を追記して下さい。

<VirtualHost *:80>
    DocumentRoot "C:/xampp/htdocs/vocaloid"
    ServerName local.vocaloid
</VirtualHost>
<Directory "C:/xampp/htdocs/wordpress">
    order deny,allow
    allow from ALL
</Directory>

<VirtualHost *:80>
    DocumentRoot C:/xampp/htdocs/wordpress
    ServerName local.wordpress
</VirtualHost>
<Directory "C:/xampp/htdocs/wordpress">
    order deny,allow
    allow from ALL
</Directory>

<VirtualHost *:80>
    DocumentRoot C:/xampp/htdocs/other
    ServerName local.other
</VirtualHost>
<Directory "C:/xampp/htdocs/other">
    order deny,allow
    allow from ALL
</Directory>

httpd-vhosts.confの編集は以上です。

hostsの編集

C:\Windows\System32\drivers\etc\hostsをテキストエディタで開きます。

127.0.0.1 localhostとなっている箇所があるので、その下に下記のように追記します。

127.0.0.1       localhost
127.0.0.1       local.vocaloid
127.0.0.1       local.wordpress
127.0.0.1       local.other

hostsの編集は以上です。

XAMPPの再起動

Apacheを再起動させなければなりませんので、
XAMPPのApacheが起動しているのであれば、一度Stopを押して再度Startをして下さい。

その後、
http://local.vocaloid/
http://local.wordpress/
http://local.other/
をそれぞれ開いてみて下さい。

無事ページが表示されればOKです。

Vagrant上のCentOS 7 でVirtual Hostを設定する

XAMPPに比べると少し手間ですが、そこまで難しいわけではないです。

  • vagrant-hostsupdater をインストール
  • vagrantfile の編集
  • httpd.conf の編集

を行えば出来ます。
次に一通りの流れを確認します。

vagrant-hostsepdater のインストール

下記コマンドを入力して vagrant-hostsupdaterをインストールします。
vagrant plugin install vagrant-hostsupdater

無事にインストールが完了したかどうかは
vagrant plugin list
で確認が出来ます。

vagrantfile の編集

該当のvagrantfile に下記を追加します。

# 好きなホスト名を入れます。
config.vm.hostname = "localhost"
# 好きなバーチャルホスト名を入れます。
config.hostsupdater.aliases = ["local.vocaloid", "local.wordpress", "local.other"]

これで vagrant up実行時に
C:/Windows/System32/drivers/etc/hosts
に自動的にバーチャルホストが追記されます。

また、vagrant suspendvagrant halt実行時には、
自動でバーチャルホストを破棄してくれます。

httpd.confの編集

vagrant sshでCentOSにアクセスし、
sudo vi /etc/httpd/conf/httpd.conf
を開きます。

httpd.confに下記を追記してください。

# 好きなサーバーネームを入れます。
ServerName vagrant

# 以下のServerNameは、vagrantfileで記述したバーチャルホスト名に合わせます。
<VirtualHost *:80>
DocumentRoot "/var/www/html/vocaloid"
ServerName local.vocaloid
ErrorLog /var/www/html/vocaloid/log/error_log
TransferLog /var/www/html/vocaloid/log/access_log
</VirtualHost>

<VirtualHost *:80>
DocumentRoot "/var/www/html/wordpress"
ServerName local.wordpress
ErrorLog /var/www/html/wordpress/log/error_log
TransferLog /var/www/html/wordpress/log/access_log
</VirtualHost>

<VirtualHost *:80>
DocumentRoot "/var/www/html/other"
ServerName local.other
ErrorLog /var/www/html/other/log/error_log
TransferLog /var/www/html/other/log/access_log
</VirtualHost>

ErrorLogTransferLogについては、記載しなくても大丈夫です。
記載しない場合は、デフォルトの場所が設定されます。
記載する場合についても、先にフォルダを作成しないとエラーが出ますので注意して下さい。

httpdの再起動

XAMPPの時と同様、Apacheの再起動が必要です。
systemctl restart restart
で再起動後、

http://local.vocaloid/
http://local.wordpress/
http://local.other/

をそれぞれ開いてみて下さい。
無事ページが表示されればOKです。

CentOS7 手動でホスト名変更をする

http://www.server-memo.net/centos-settings/centos7/hostname.html

« mPDFがChromeで表示されないJavaScript イベントの停止とバブリングについて »

Sponsored Link

コメントする

記事の感想や修正依頼等ありましたら、コメントをお願いいたします