WWWサーバーの構築
このページではWWWサーバー「Apache」の構築方法を紹介します。
名前ベースのバーチャルホストの設定もします。
(IPアドレスはひとつだが複数のドメインを扱う設定)
Apache公式マニュアル 名前ベースのバーチャルホスト
ドメインごとに「/home/ユーザー名/public_html」というディレクトリを作りドキュメントはそこに保存してます。
ログは「/home/ユーザー名/httpd_logs」というディレクトリを作りそこに保存してます。
よって1ドメインに1Linuxユーザーという設定です。
Apacheのインストール
[root@jintaro ~]# yum -y install httpd
(中略)
Installed: httpd.i386 0:2.2.8-1.fc8
Dependency Installed: httpd-tools.i386 0:2.2.8-1.fc8
Complete!
[root@jintaro ~]# yum -y install php php-mbstring
(中略)
Installed: php.i386 0:5.2.4-3 php-mbstring.i386 0:5.2.4-3
Dependency Installed: php-cli.i386 0:5.2.4-3 php-common.i386 0:5.2.4-3
Complete! |
httpdをインストール
phpとphp-mbstring
をインストール |
Apacheの設定
[root@jintaro ~]# vi /etc/httpd/conf/httpd.conf
(中略)
AddDefaultCharset UTF-8
↓
#AddDefaultCharset UTF-8
(中略)
#AddHandler cgi-script .cgi
↓
AddHandler cgi-script .cgi .pl
(中略)
---最終行に追加 ここから---
NameVirtualHost *:80
<VirtualHost *:80>
ServerAdmin hoge@hoge.com
DocumentRoot /home/testuser/public_html
ServerName jintaro.com
ServerAlias jintaro.com *.jintaro.com
DirectoryIndex index.shtml index.php index.cgi index.html
<Directory /home/testuser/public_html>
Options FollowSymLinks ExecCGI Includes
AllowOverride AuthConfig
Order allow,deny
Allow from all
</Directory>
ErrorLog /home/testuser/httpd_logs/error.log
SetEnvIf Remote_Addr 192.168. nolog
CustomLog /home/testuser/httpd_logs/access.log combined env=!nolog
</VirtualHost>
<VirtualHost *:80>
ServerAdmin hoge@hoge.com
DocumentRoot /home/testuser2/public_html
ServerName test.com
ServerAlias test.com *.test.com
DirectoryIndex index.shtml index.php index.cgi index.html
<Directory /home/testuser2/public_html>
Options FollowSymLinks ExecCGI Includes
AllowOverride AuthConfig
Order allow,deny
Allow from all
</Directory>
ErrorLog /home/testuser2/httpd_logs/error.log
SetEnvIf Remote_Addr 192.168. nolog
CustomLog /home/testuser2/httpd_logs/access.log combined env=!nolog
</VirtualHost>
---最終行に追加 ここまで---
[root@jintaro ~]# su - testuser
[testuser@jintaro ~]$ mkdir public_html
[testuser@jintaro ~]$ mkdir httpd_logs
[testuser@jintaro ~]$ exit
[root@jintaro ~]# su - testuser2
[testuser2@jintaro ~]$ mkdir public_html
[testuser2@jintaro ~]$ mkdir httpd_logs
[testuser2@jintaro ~]$ exit
[root@jintaro ~]# service httpd start
httpd を起動中: [ OK ]
[root@jintaro ~]# chkconfig httpd on
[root@jintaro ~]# chkconfig --list httpd
httpd 0:off 1:off 2:on 3:on 4:on 5:on 6:off
|
Apacheの設定ファイルを開く
#でコメントアウト
変更(.cgiと.plでCGIが実行される)
1つ目のホスト
サーバー管理者
ドキュメントのルート
サーバーのドメイン名
ドメイン別名
indexファイルの種類
ファイル名指定しない場合に
この順番で検索される
CGI,SSIを許可
htaccessのユーザー認証許可
エラーログを記録
ローカルからのアクセスを記録しない
アクセスログ
2つ目のホスト
rootからtestuserになる
ディレクトリpublic_htmlを作成
ディレクトリhttpd_logsを作成
rootに戻る
rootからtestuser2になる
ディレクトリpublic_htmlを作成
ディレクトリhttpd_logsを作成
rootに戻る
httpd(Apache)を起動
httpd自動起動on
httpd自動起動確認
2〜5でon
|
ためしにDocumentRoot (この場合/home/testuser/public_html)にindex.html等を置いてクライアントからアクセスしてみましょう。
WAN側からアクセスする場合はルータの80番ポートをオープンします。
(注) 見れないときはDocumentRootのディレクトリのパーミッションを確認しましょう。
ログのローテーション
上記のようにログを指定した場合は自動でローテーションされずにログが大きくなってしまうので
loglotateによって自動でログがローテーションされるようにします。
(上記のようにログファイルを指定しない場合はログは /var/log/httpd に作られます。
その場合はすでにローテーションされる様になってるのでローテーションの設定は不要です。)
[root@jintaro ~]# vi /etc/logrotate.d/httpd_access_log
/home/testuser/httpd_logs/access.log /home/testuser2/httpd_logs/access.log
{
missingok
notifempty
sharedscripts
postrotate
/bin/kill -HUP `cat /var/run/httpd.pid 2>/dev/null` 2> /dev/null || true
endscript
}
[root@jintaro ~]# vi /etc/logrotate.d/httpd_error_log
/home/testuser/httpd_logs/error.log /home/testuser2/httpd_logs/error.log
{
missingok
notifempty
sharedscripts
postrotate
/bin/kill -HUP `cat /var/run/httpd.pid 2>/dev/null` 2> /dev/null || true
endscript
}
|
access.log用のローテーション設定
ファイル作成
ローテーションしたいログファイルを
スペースで区切って指定
error.log用のローテーション設定
ファイル作成
|
これで1週間ごとにログがローテーションされる様になります。
(ローテーションの条件は /etc/logrotate.conf に設定されています。)
|