apache2のIPベースのバーチャルホストで複数ドメインの共存

■apache2のIPベースのバーチャルホストで複数ドメインの共存
 vmplayerのゲストOS(Squeeze)上で行った。

 参考:Apache2のバーチャルホストの設定(IPベース編)
 http://www.net-phantom.jp/uturo1/2011/10/apache2ip.html

■仮想NICの設定

$ cat /etc/network/interfaces
# This file describes the network interfaces available on your system
# and how to activate them. For more information, see interfaces(5).

# The loopback network interface
auto lo
iface lo inet loopback

# The primary network interface
allow-hotplug eth0
#iface eth0 inet dhcp
iface eth0 inet static
address 192.168.164.177
netmask 255.255.255.0
gateway 192.168.164.2
broadcast 192.168.164.255

auto eth0:0
iface eth0:0 inet static
address 192.168.164.178
netmask 255.255.255.0

auto eth0:1
iface eth0:1 inet static
address 192.168.164.179
netmask 255.255.255.0

$ env LANG=C /sbin/ifconfig | grep "inet "
          inet addr:192.168.164.177  Bcast:192.168.164.255  Mask:255.255.255.0
          inet addr:192.168.164.178  Bcast:192.168.164.255  Mask:255.255.255.0
          inet addr:192.168.164.179  Bcast:192.168.164.255  Mask:255.255.255.0
          inet addr:127.0.0.1  Mask:255.0.0.0


■/etc/hostsででたらめな3つのドメインを追加

$ sudo grep ^192 /etc/hosts
192.168.164.177 vmsqueeze-nclean.localdomain    vmsqueeze-nclean
192.168.164.177 www.squeeze.net
192.168.164.178 www.labunix.vm
192.168.164.178 www.vm.test

■apache2の導入

$ sudo apt-get install -y apache2

■IPベース用のファイルを作成
 ※あまり複雑にする気は無いので、以下のホスト名とIPを書き換えて三回実行

$ cat virtualhost.sh
#!/bin/bash

if [ `id -u` -ne 0 ];then
  echo "Not Permit User"
  exit 1
fi
IPIP=192.168.164.179
IPHOST=www.vm.test
IPMAIL=root@localdomain
IPROOT="/var/www/${IPHOST}"
test -d ${IPROOT} || mkdir ${IPROOT}
test -d ${IPROOT} || exit 1
test -d "/var/log/apache2/${IPHOST}" || mkdir "/var/log/apache2/${IPHOST}"
test -d "/var/log/apache2/${IPHOST}" || exit 1

echo '
<VirtualHost '${IPIP}'>
 Servername '${IPHOST}'
 ServerAdmin '${IPMAIL}'
 DocumentRoot '${IPROOT}'
 <Directory '${IPROOT}'>
  Options FollowSymLinks MultiViews
  AllowOverride None
  Order allow,deny
  allow from all
 </Directory>
 ErrorLog /var/log/apache2/'${IPHOST}'/error.log
 LogLevel warn
 CustomLog /var/log/apache2/'${IPHOST}'/access.log combined
</VirtualHost>' > "/etc/apache2/sites-available/${IPHOST}"


■コンテンツの作成

$ sudo find /var/www/* -type d -exec cp -p /var/www/index.html {} \;
$ sudo find /var/www/* -type f
/var/www/index.html
/var/www/www.labunix.vm/index.html
/var/www/www.squeeze.net/index.html
/var/www/www.vm.test/index.html

■ユニークなコンテンツに変更

# for list in www.labunix.vm www.squeeze.net www.vm.test ;do
    echo "[ $list ]"
    cp -pi /var/www/"$list"/index.html /var/www/"$list"/index.tmp
    sed -e s%'</body>'%"\n"'<p>'"$list"'</p>'"\n&"%g /var/www/"$list"/index.tmp > /var/www/"$list"/index.html 
  done
[ www.labunix.vm ]
[ www.squeeze.net ]
[ www.vm.test ]

■コンテンツの確認

# grep . /var/www/*/index.html | grep '<p>www.*\.'
/var/www/www.labunix.vm/index.html:<p>www.labunix.vm</p>
/var/www/www.squeeze.net/index.html:<p>www.squeeze.net</p>
/var/www/www.vm.test/index.html:<p>www.vm.test</p>

■権限の変更

$ sudo chown -R www-data:www-data /var/www
$ find /var/www -type f -exec ls -l {} \; | awk '{print $3 ":" $4 " " $8}'
www-data:www-data /var/www/www.labunix.vm/index.html
www-data:www-data /var/www/www.labunix.vm/index.tmp
www-data:www-data /var/www/index.html
www-data:www-data /var/www/www.squeeze.net/index.html
www-data:www-data /var/www/www.squeeze.net/index.tmp
www-data:www-data /var/www/www.vm.test/index.html
www-data:www-data /var/www/www.vm.test/index.tmp

■IPベースの仮想ホストの有効化

# a2ensite www.labunix.vm
Enabling site www.labunix.vm.
Run '/etc/init.d/apache2 reload' to activate new configuration!
# a2ensite www.squeeze.net
Enabling site www.squeeze.net.
Run '/etc/init.d/apache2 reload' to activate new configuration!
# a2ensite www.vm.test
Enabling site www.vm.test.
Run '/etc/init.d/apache2 reload' to activate new configuration!

■apache2設定の再読み込み

$ sudo /etc/init.d/apache2 reload
Reloading web server config: apache2.

■ローカルのブラウザからの確認
 ※1行目か7行目なら表示は、localhostの場合に7行目が無いから。

# for list in `hostname` www.labunix.vm www.squeeze.net www.vm.test ;do
    echo "[ $list ]"
    ping -c 1 $list 2>&1 | grep "$list"
    w3m -no-proxy -dump http://${list} | awk '(NR%7==0) || (NR==1) {print}'
  done
[ vmsqueeze-nclean ]
PING vmsqueeze-nclean.localdomain (127.0.1.1) 56(84) bytes of data.
64 bytes from vmsqueeze-nclean.localdomain (127.0.1.1): icmp_req=1 ttl=64 time=0.028 ms
--- vmsqueeze-nclean.localdomain ping statistics ---
It works!
[ www.labunix.vm ]
PING www.labunix.vm (192.168.164.178) 56(84) bytes of data.
64 bytes from www.labunix.vm (192.168.164.178): icmp_req=1 ttl=64 time=0.020 ms
--- www.labunix.vm ping statistics ---
It works!
www.labunix.vm
[ www.squeeze.net ]
PING www.squeeze.net (192.168.164.177) 56(84) bytes of data.
--- www.squeeze.net ping statistics ---
It works!
www.squeeze.net
[ www.vm.test ]
PING www.vm.test (192.168.164.179) 56(84) bytes of data.
64 bytes from www.vm.test (192.168.164.179): icmp_req=1 ttl=64 time=0.022 ms
--- www.vm.test ping statistics ---
It works!
www.vm.test