Apache2 バーチャルホスト 設定
ヒント
現状の設定ではhttp://mail.domain.tldやhttp://ftp.domain.tldでアクセスできてしまうため
VirtualHostの設定でhttp://www.domain.tldのアクセスのみを許可する。
ServerAlias domain.tldを追加してhttp://domain.tldのアクセス許可も可能。
http.conf編集
[root@freebsd ~]# vi /usr/local/etc/apache22/httpd.conf
#Include etc/apache22/extra/httpd-vhosts.conf
↓
Include etc/apache22/extra/httpd-vhosts.conf ←コメント解除(バーチャルホスト設定)
VirtualHost設定
[root@freebsd ~]# rm /usr/local/etc/apache22/extra/httpd-vhosts.conf
[root@freebsd ~]# vi /usr/local/etc/apache22/extra/httpd-vhosts.conf
↓下記を記入
NameVirtualHost *:80
<VirtualHost *:80>
ServerName any
DocumentRoot /tmp
</VirtualHost>
<VirtualHost *:80>
ServerName www.freebsd.orz
DocumentRoot /usr/local/www/apache22/data
</VirtualHost>
wwwなしで接続を許可する場合
[root@freebsd ~]# vi /usr/local/etc/apache22/extra/httpd-vhosts.conf
<VirtualHost *:80>
ServerName www.freebsd.orz
ServerAlias freebsd.orz
DocumentRoot /usr/local/www/apache22/data
</VirtualHost>
wwwなしをwwwありにリダイレクト(wwwありに統一)する場合
[root@freebsd ~]# vi /usr/local/etc/apache22/extra/httpd-vhosts.conf
<VirtualHost *:80>
ServerName www.freebsd.orz
ServerAlias freebsd.orz
DocumentRoot /usr/local/www/apache22/data
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(freebsd.orz)(:80)? [NC]
RewriteRule ^(.*) http://www.freebsd.orz/$1 [R=301,L]
</VirtualHost>
サブドメインを追加する場合
[root@freebsd ~]# vi /usr/local/etc/apache22/extra/httpd-vhosts.conf
↓最終行に下記を記入
<VirtualHost *:80>
ServerName user_name.freebsd.orz
DocumentRoot /home/user_name/public_html
SuexecUserGroup user_name user_name
ErrorLog /var/log/httpd/user_name-error_log
CustomLog /var/log/httpd/user_name-access_log combined env=!nolog
</VirtualHost>
ヒント
ServerNameで指定したサーバー名はDNSの設定が必要です。
Apache再起動
[root@freebsd ~]# /usr/local/etc/rc.d/apache22 restart