CentOS 5.3 x64のApacheでバーチャルホストを設定
CentOS 5.3 x64のApacheを使った、バーチャルホストの設定をご紹介します。
- ユーザー管理
- ディレクトリ構成。
- FTPをどうするか。
- 想定される構成
- ユーザーの作成
- /etc/httpd/conf/httpd.confの編集
設定する前に、ある程度構成や仕様を考えなければいけません。
主に検討しなければならないのが、
だと思います。
当然、自分がいくつものドメインを管理して、自分一人しか使ってなければ、こんな悩む必要は無いんでしょうけど、ホスティングサービスをやりたいけど、どう設定すれば管理も楽なんだろうと思う人もいるはず。
そこで、簡単かつ明確なディレクトリ構成で、バーチャルドメインでのApache設定方法を考えてみたので、一例として参考にして下さい。
あくまで、簡単に設定できる方法です。
メインドメイン:foo.com
バーチャルドメイン:bar.com
foo.comのユーザーを作成
# useradd foo
# passwd foo
bar.comのユーザー作成
# useradd bar
# passwd bar
それぞれのドキュメントルートを作成
# mkdir /home/foo/public_html
# chown foo:foo /home/foo/public_html
# mkdir /home/bar/public_html
# chown bar:bar /home/bar/public_html
パーミッションの変更
# chmod 701 /home/*
セキュリティー上の観点から、プロダクトのみの表記
ServerTokens OS
↓
ServerTokens ProductOnly
メインドメインを記述
#ServerName www.example.com:80
↓
ServerName foo.com
バーチャルホストファイルに記述するため、コメントアウト
DocumentRoot "/var/www/html"
↓
#DocumentRoot "/var/www/html"
バーチャルホストファイルに記述するため、コメントアウト
<Directory "/var/www/html">
Options Indexes FollowSymLinks
AllowOverride None
Order allow,deny
Allow from all
</Directory>
↓
#<Directory "/var/www/html">
# Options Indexes FollowSymLinks
# AllowOverride None
# Order allow,deny
# Allow from all
#</Directory>
バーチャルホストファイルに記述するため、コメントアウト
ScriptAlias /cgi-bin/ "/var/www/cgi-bin/"
<Directory "/var/www/cgi-bin">
AllowOverride None
Options None
Order allow,deny
Allow from all
</Directory>
↓
#ScriptAlias /cgi-bin/ "/var/www/cgi-bin/"
#<Directory "/var/www/cgi-bin">
# AllowOverride None
# Options None
# Order allow,deny
# Allow from all
#</Directory>
index.htmとindex.cgiを追加
DirectoryIndex index.html index.html.var
↓
DirectoryIndex index.html index.htm index.cgi index.html.var
ログのIPアドレスを名前解決するかどうか。どっちでも良いです。好みで。
HostnameLookups Off
↓
HostnameLookups On
セキュリティー上の観点から、シグネチャ表示を不可
ServerSignature On
↓
ServerSignature Off
セキュリティ上の観点から、Indexesを削除
Options Indexes FollowSymLinks
↓
Options FollowSymLinks
日本語を最優先に設定
LanguagePriority en ca cs da de el eo es et fr he hr it ja ko ltz nl nn no pl pt pt-BR ru sv zh-CN zh-TW
↓
LanguagePriority ja en ca cs da de el eo es et fr he hr it ko ltz nl nn no pl pt pt-BR ru sv zh-CN zh-TW
文字化けを防ぐために、デフォルト文字コードの設定を回避。
AddDefaultCharset UTF-8
↓
#AddDefaultCharset UTF-8
CGIの動作を許可
#AddHandler cgi-script .cgi
↓
AddHandler cgi-script .cgi .pl
セキュリティ上の観点から、METHODのTRACEを不可にするために追記
# Trace Method
TraceEnable Off
バーチャルホスト化のために設定
#NameVirtualHost *:80
↓
NameVirtualHost *:80
/etc/httpd/conf.d/virtualhost-.conf
IPアドレスでアクセスしてきた場合、どのページも表示させないようにする。
#
# VirtualHost example:
# Almost any Apache directive may go into a VirtualHost container.
# The first VirtualHost section is used for requests without a known
# server name.
#
<VirtualHost *:80>
ServerName any
DocumentRoot /tmp
</VirtualHost>
※/tmpは適当に変更しても良い。
※他のサイトでは、httpd.confファイルに直接記述する例が掲載されていますが、私が検証してみると、どうしてもIPアドレスでアクセスしてもうまくいきませんでした。なので、思うような結果が得られた方法をご紹介しました。
/etc/httpd/conf.d/virtualhost-foo.com.conf
#
# VirtualHost example:
# Almost any Apache directive may go into a VirtualHost container.
# The first VirtualHost section is used for requests without a known
# server name.
#
<Directory "/home/foo/public_html">
Options Includes FollowSymLinks ExecCGI MultiViews
AllowOverride AuthConfig
Order allow,deny
Allow from all
</Directory>
<VirtualHost *:80>
ServerAdmin foo@foo.com
DocumentRoot /home/foo/public_html
ServerName www.foo.com
ServerAlias foo.com *.foo.com
ErrorLog logs/foo.com-error_log
CustomLog logs/foo.com-access_log combined env=!no_log
</VirtualHost>
※ 同じ要領でbar.comのファイルも作成。
# service httpd start
# chkconfig httpd on
基本的な設定は以上です。
もしも、運用中に新しいドメインを追加しなくてはならなくなった場合は、以下の手順で可能です。
# useradd hoge
# passwd hoge
# mkdir /home/hoge/public_html
# chown hoge:hoge /home/hoge/public_html
# chmod 701 /home/*
/etc/httpd/conf.d/virtualhost-hoge.com.confの編集
#
# VirtualHost example:
# Almost any Apache directive may go into a VirtualHost container.
# The first VirtualHost section is used for requests without a known
# server name.
#
<Directory "/home/hoge/public_html">
Options Includes FollowSymLinks ExecCGI MultiViews
AllowOverride AuthConfig
Order allow,deny
Allow from all
</Directory>
<VirtualHost *:80>
ServerAdmin hoge@hoge.com
DocumentRoot /home/hoge/public_html
ServerName www.hoge.com
ServerAlias hoge.com *.hoge.com
ErrorLog logs/hoge.com-error_log
CustomLog logs/hoge.com-access_log combined env=!no_log
</VirtualHost>
# service httpd reload
これで、バーチャルホストでの運用が可能ですし、FTPも普通にvsftpdを設定するだけで、ユーザーにアカウントを割り振ってコンテンツをアップしてもらうことが可能です。
是非、お試し下さい。
以下、参考リンクです。
Wikipedia(Linux)
Wikipedia(CentOS)
Wikipedia(Apache)
Wikipedia(バーチャルホスト)
- 関連記事