Webサーバー Apacheのインストール
概要:Webサーバーはhttpプロトコルにより、Firefox、Internet Explorer等のブラウザからWebページを見れるようにするためのサーバーです。ここでは一般的な
Apache Software FoundationのApacheを使用したWebサーバーを構築します。
ここでは、以下のことができるようにします。
- CGIは任意のディレクトリで実行できるようにする。
- SSIは拡張子がshtmlのもののみ実行できるようにする。
- .htaccessを使用できるようにする。
- PHPを使用できるようにする。
目次 †
1. Apacheインストール
2. SSL
以下の内容についてはCentos V5.4の例の下記を参照下さい。
1. ApacheとPHPのインストールの確認 †
ApccheとPHPは既にインストールされているので確認します。
# yum list installed | grep httpd
httpd.x86_64 2.2.15-15.el6.centos.1 @base ← これがあることを確認
# yum list installed | grep php
php.x86_64 5.3.3-14.el6_3 @updates
php-mbstring.x86_64 5.3.3-14.el6_3 @updates ← この2つがあることを確認
2. Webサーバー設定 †
- ) httpd.confの編集
# vi /etc/httpd/conf/httpd.conf ← httpd設定ファイル編集
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
| ServerTokens OS
↓
ServerTokens Prod ← エラーページ等でOS名を表示しないようにする
KeepAlive Off
↓
KeepAlive On ← クライアントとの接続を保持する
#ServerName www.example.com:80
↓
ServerName www.acompass.net:80 ← サーバー名を指定。但しこの時点では未だサーバー名が未定なので後で設定します。
<Directory "/var/www/html">
#
# Possible values for the Options directive are "None", "All",
# or any combination of:
# Indexes Includes FollowSymLinks SymLinksifOwnerMatch ExecCGI MultiViews
#
# Note that "MultiViews" must be named *explicitly* --- "Options All"
# doesn't give it to you.
#
# The Options directive is both complicated and important. Please see
# http://httpd.apache.org/docs-2.0/mod/core.html#options
# for more information.
#
Options Indexes FollowSymLinks
↓
Options Includes ExecCGI FollowSymLinks ← CGI,SSIの許可
#
# AllowOverride controls what directives may be placed in .htaccess files.
# It can be "All", "None", or any combination of the keywords:
# Options FileInfo AuthConfig Limit
#
AllowOverride None
↓
AllowOverride All ← .htaccessの許可
#
# ErrorLog: The location of the error log file.
# If you do not specify an ErrorLog directive within a <VirtualHost>
# container, error messages relating to that virtual host will be
# logged here. If you *do* define an error logfile for a <VirtualHost>
# container, that host's errors will be logged there and not here.
#
ErrorLog logs/error_log ← エラーログの場所を確認
#
# The following directives define some format nicknames for use with
# a CustomLog directive (see below).
#
LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined
↓
LogFormat "%h %l %u %t \"%!414r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined
← 長すぎるURI(414エラー)はログに記録しない
#
# For a single logfile with access, agent, and referer information
# (Combined Logfile Format), use the following directive:
#
SetEnvIf Request_URI "default\.ida" no_log ← 追加(wormからのアクセスをログに記録しない)
SetEnvIf Request_URI "cmd\.exe" no_log ← 〃
SetEnvIf Request_URI "root\.exe" no_log ← 〃
SetEnvIf Request_URI "Admin\.dll" no_log ← 〃
SetEnvIf Request_URI "NULL\.IDA" no_log ← 〃
CustomLog logs/access_log combined env=!no_log ← 上記以外のアクセスをログに記録する。ログの場所を確認
ServerSignature On
↓
ServerSignature Off ← エラーページ等でApacheのバージョンを表示しないようにする
AddLanguage ja .ja ← この行を最初に移動する
AddLanguage ca .ca
:
:
LanguagePriority ja en ca cs...... ← jaを最初に移動する
AddDefaultCharset UTF-8
↓
#AddDefaultCharset UTF-8 ← コメントアウト
#AddHandler cgi-script .cgi
↓
AddHandler cgi-script .cgi .pl ← CGIスクリプトに.plを追加
<Directory "/var/www/icons">
Options Indexes MultiViews
↓
Options MultiViews ← iconsディレクトリのファイル一覧を表示しないようにする
AllowOverride None
Order allow,deny
Allow from all
</Directory>
|
- ) ログの場所の確認
ログの場所は上記httpd.confの設定ではlogs/の設定になっています。これは絶対ルートでは/etc/httpd/logsです。調べてみると以下のように/var/log/httpdへシンボリックリンクが張られていました。/var/log/httpdのログを見てもいいことが分かります。
# ls -la /etc/httpd
drwxr-xr-x 4 root root 4096 Sep 20 00:44 .
drwxr-xr-x 84 root root 12288 Nov 4 04:33 ..
drwxr-xr-x 2 root root 4096 Nov 7 02:04 conf
drwxr-xr-x 2 root root 4096 Nov 7 01:49 conf.d
lrwxrwxrwx 1 root root 19 Sep 20 00:44 logs -> ../../var/log/httpd
lrwxrwxrwx 1 root root 29 Sep 20 00:44 modules -> ../../usr/lib64/httpd/modules
lrwxrwxrwx 1 root root 19 Sep 20 00:44 run -> ../../var/run/httpd
- ) 不要ファイルの削除
# rm -f /etc/httpd/conf.d/welcome.conf ← テストページ削除
# rm -f /var/www/error/noindex.html ← テストページ削除
- ) Perlコマンドへ/usr/local/bin/perlでもアクセスできるようにする。
以下は既に設定済みでした。
# ln -s /usr/bin/perl /usr/local/bin/perl
← /usr/local/bin/perlから/usr/bin/perlへリンクをはる
# whereis perl ← Perlのパスを確認
perl: /usr/bin/perl /usr/local/bin/perl /usr/share/man/man1/perl.1.gz
← Perlのパスに/usr/local/bin/perlが表示されることを確認
- ) ドキュメントルート所有者変更
ここでは、例としてドキュメントルート所有者をcentosに変更します。
# chown centos. /var/www/html/ ← ドキュメントルート所有者変更
# ll /var/www/ ← ドキュメントルート所有者変更確認
合計 24
drwxr-xr-x 2 root root 4096 10月 5 11:45 cgi-bin
drwxr-xr-x 3 root root 4096 10月 9 00:19 error
drwxr-xr-x 5 centos centos 4096 9月 27 17:43 html
drwxr-xr-x 3 root root 4096 9月 27 09:29 icons
3. Webサーバーの設定の反映 †
Apacheの設定を反映させるためにはApacheを停止・開始で可能ですが、以下の4つのオプションによる方法があります。
オプション | 説明 |
restart | 停止処理と開始処理を連続して行います。Webサービスを即終了して再起動します。 |
condrestart | Apacheが起動しているときだけ再起動を行います。停止している場合は起動しません。Webサービスを即終了して再起動します。 |
reload | 子プロセスのみを即終了(実行中のリクエストの処理を中止)して、設定変更を反映します。親プロセスは終了しません。 |
graceful | 子プロセスは実行中のリクエストの処理が完了してから終了します。すべての子プロセスが終了してから設定変更を反映します。親プロセスは終了しません。処理完了のメッセージは表示されません。 |
ここでは設定反映だけで、また実行中のリクエストの処理を中止しても問題ないので、オプションにreloadを指定します。
# service httpd reload ← リロード
Reloading httpd:
4. Webサーバーの自動起動設定の確認 †
# chkconfig --list httpd ← httpd自動起動設定確認
httpd 0:off 1:off 2:on 3:on 4:on 5:on 6:off
← ランレベル2~5のonを確認
※ service httpdの代わりに/etc/rc.d/init.d/httpdとしても同じです。
5. ポート80番のOPEN †
VPSの場合通常ネットワーク上でTCP/IPの80番ポートは開いています。
6. Webサーバー確認 †
- ) Webページ表示確認
# set | grep LANG ← システムの文字コードの確認
LANG=en_US.UTF-8
# vi /var/www/html/index.html ← テストページ作成
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> ← システムの文字コードがUTF-8の場合
<title>テスト</title>
<body>
テスト
</body>
</html>
ブラウザーから http://ホスト名/ を入力してアクセスします。
テスト
と表示されればOKです。
# rm -f /var/www/html/index.html ← テストページ削除
- ) CGI確認
CGIで簡単なテストページを表示してみます。
# vi /var/www/html/test.cgi ← テスト用CGI作成
#!/usr/local/bin/perl
print "Content-type: text/html\n\n";
print "<html>\n";
print "<head>\n";
print "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\">\n";
← システムの文字コードがUTF-8の場合
print "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=euc-jp\">\n";
← システムの文字コードがEUCの場合
print "<title>テスト</title>\n";
print "</head>\n";
print "<body>\n";
print "CGIテスト\n";
print "</body>\n";
print "</html>\n";
# chmod 755 /var/www/html/test.cgi ← テスト用CGIパーミッション変更
http://サーバー名/test.cgiにアクセスしてCGIテストページが表示されればOKです。
- ) SSI確認
SSIで現在日時を表示してみます。
# vi /var/www/html/test.shtml ← SSIテスト用ページ作成
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
← システムの文字コードがUTF-8の場合
<title>テスト</title>
<body>
SSIテスト
<!--#config timefmt="%Y/%m/%d %H:%M:%S" -->
<!--#echo var="DATE_LOCAL" -->
</body>
</html>
http://サーバー名/test.shtmlにアクセスして現在日時を表示するSSIテストページが表示されればOKです。
- ) .htaccess確認
.htaccessでDirectoryIndex(ファイル名を省略した場合に表示されるページ)をindex.htmlからindex.shtmlに変更してみます。
# vi /var/www/html/.htaccess ← .htaccessファイル作成
DirectoryIndex index.shtml
# vi /var/www/html/index.shtml ← .htaccessテスト用ページ作成
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
← システムの文字コードがUTF-8の場合
<title>テスト</title>
<body>
<p>.htaccessによるWebサーバ設定(例としてDirectoryIndex)の変更テスト</p>
このページのファイル名は<!--#echo var="DOCUMENT_NAME" -->
</body>
</html>
http://サーバー名/にアクセスしてindex.shtmlが表示されればOKです。
- ) PHP確認
PHPで簡単なテストページを表示してみます。
# vi /var/www/html/test.php ← PHPテスト用ページ作成
<?php
phpinfo();
?>
http://サーバー名/test.phpにアクセスしてphpinfoページが表示されればOKです。
7. Webサーバー確認後始末 †
上記の確認で作成したテスト用ページ等を全て削除します。
# rm -f /var/www/html/* ← 作成したテスト用ページを全て削除
# rm -f /var/www/html/.htaccess ← 作成した.htaccessを削除
8. 設定変更後の操作 †
Apacheの設定変更後に以下の操作が必要です。また、PHPを使用している場合/etc/php.iniの設定を変更した場合にもreloadが必要です。
- ) 設定の確認
次の設定ファイルのテストコマンドを実行し問題のないことを確認します。
# service httpd configtest
Syntax OK ← この様に表示されればOKです。
- ) 設定の反映
次のコマンドを実行し設定ファイルを読み直します。
# service httpd reload
httpd を再読み込み中:
- ) ブラウザーのリセット
サーバー側の設定が更新されても(ApacheをRestartさせても)、ブラウザー側では以前の情報(Cache)が残っていて、最新の情報が使用されず以前の情報が表示される場合が多いです。その場合は
- )ブラウザーの履歴を削除する。
- )ブラウザーを再起動する。
- )再見込みする。( Ctl + F5 の2つのキーを同時に押します。)
などをします。