Webサーバー構築(Apache)

最終更新日: 2014.02.19

<<トップページ <<新着情報 <<サイト内検索 <<CentOSで自宅サーバー構築 <<Scientific Linuxで自宅サーバー構築

■概要

Webサーバー(Apache)はInternet Explorer等のブラウザからWebページをみれるようにするためのサーバー。
ここでは、ホームページスペース提供サービスを行っている一般的なWebサーバーと同様に以下のことができるようにする。

 ・CGIは任意のディレクトリで実行できるようにする
 ・SSIは拡張子がshtmlのもののみ実行できるようにする
 ・.htaccessを使用できるようにする
 ・PHPを使用できるようにする


■Apacheインストール

[root@fedora ~]# yum -y install httpd ← Apacheインストール

[root@fedora ~]# yum -y install php php-mbstring ← php、php-mbstringインストール

■Apache設定

(1)Apache設定
[root@fedora ~]# vi /etc/httpd/conf/httpd.conf ← Apache設定ファイル編集
ServerTokens OS
↓
ServerTokens Prod ← エラーページ等でOS名を表示しないようにする

#ServerName www.example.com:80
↓
ServerName fedorasrv.com: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の許可

#
# 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 ← 〃
SetEnvIf Remote_Addr 192.168.1 no_log ← 追加(内部からのアクセスをログに記録しない)
SetEnvIf Remote_Addr 127.0.0.1 no_log ← 追加(自ホストからのアクセスをログに記録しない)
CustomLog logs/access_log combined env=!no_log ← 上記以外のアクセスをログに記録する

ServerSignature On
↓
ServerSignature Off ← エラーページ等でApacheのバージョンを表示しないようにする

AddDefaultCharset UTF-8
↓
#AddDefaultCharset UTF-8 ← コメントアウト

#AddHandler cgi-script .cgi
↓
AddHandler cgi-script .cgi .pl ← CGIスクリプトに.plを追加

[root@fedora ~]# rm -f /etc/httpd/conf.d/welcome.conf ← テストページ削除

[root@fedora ~]# rm -f /var/www/error/noindex.html ← テストページ削除

(2)Perlコマンドへ/usr/local/bin/perlでもアクセスできるようにする。
[root@fedora ~]# ln -s /usr/bin/perl /usr/local/bin/perl ← /usr/local/bin/perlから/usr/bin/perlへリンクをはる

[root@fedora ~]# whereis perl ← Perlのパスを確認
perl: /usr/bin/perl /usr/local/bin/perl /usr/share/man/man1/perl.1.gz
 ← Perlのパスに/usr/local/bin/perlが表示されることを確認

(3)ドキュメントルート所有者変更
ここでは、例としてドキュメントルート所有者をfedoraに変更する。
[root@fedora ~]# chown fedora. /var/www/html/ ← ドキュメントルート所有者変更

[root@fedora ~]# 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 fedora    fedora    4096  9月 27 17:43 html
drwxr-xr-x  3 root      root      4096  9月 27 09:29 icons

■Apache起動

(1)Apache起動
[root@fedora ~]# /etc/rc.d/init.d/httpd start ← Apache起動
Starting httpd (via systemctl):                            [  OK  ]

[root@fedora ~]# chkconfig httpd on ← Apache自動起動設定

(2)ポート80番のOPEN
ルーター側の設定でポート80番をOPENする。
※ルーターの設定は各ルーターのマニュアルまたはメーカー別ルーターポート開放手順を参照

ポートチェック【外部からポート開放確認】で「host名」にサーバー名(例:fedorasrv.com)、「port番号」に80と入力して「ポートチェック」ボタン押下し、「ホスト=fedorasrv.com ポート=80 にアクセスできました。」と表示されることを確認。

(3)外部からのWebサーバーアクセス確認
外部からWebサーバーにアクセスできるか確認する。
[root@fedora ~]# echo test >> /var/www/html/index.html ← テストページ作成

Free Website Test tools by WebSitePulse


「Test Type」で「Website Test」を選択
「Test Target」でサーバー名(例:http://fedorasrv.com)を指定
「Perform Test」ボタンを押下

以下のような結果が表示されればOK
Website test results

URL tested: http://fedorasrv.com
Test performed from: Seattle, WA
Test performed at: 2007-06-12 07:30:13 (GMT -07:00)
Status: OK
Response Time: 2.298 sec
DNS: 0.708 sec
Connect: 0.146 sec
First byte: 0.147 sec
Last byte: 1.296 sec
Size: 23294 bytes

[root@fedora ~]# rm -f /var/www/html/index.html ← テストページ削除


■Apache確認

(1)Webページ表示確認
[root@fedora ~]# vi /var/www/html/index.html ← テストページ作成
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">※システムの文字コードがUTF-8の場合
<meta http-equiv="Content-Type" content="text/html; charset=euc-jp">※システムの文字コードがEUCの場合
<title>テスト</title>
</head>
<body>
テスト
</body>
</html>
http://サーバーIPアドレス/にアクセスしてテストページが表示されればOK

(2)CGI確認
CGIで簡単なテストページを表示してみる。
[root@fedora ~]# 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";

[root@fedora ~]# chmod 755 /var/www/html/test.cgi ← テスト用CGIパーミッション変更
http://サーバーIPアドレス/test.cgiにアクセスしてCGIテストページが表示されればOK

(3)SSI確認
SSIで現在日時を表示してみる。
[root@fedora ~]# vi /var/www/html/test.shtml ← SSIテスト用ページ作成
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">※システムの文字コードがUTF-8の場合
<meta http-equiv="Content-Type" content="text/html; charset=euc-jp">※システムの文字コードがEUCの場合
<title>テスト</title>
</head>
<body>
SSIテスト
<!--#config timefmt="%Y/%m/%d %H:%M:%S" -->
<!--#echo var="DATE_LOCAL" -->
</body>
</html>
http://サーバーIPアドレス/test.shtmlにアクセスして現在日時を表示するSSIテストページが表示されればOK

(4).htaccess確認
.htaccessでDirectoryIndex(ファイル名を省略した場合に表示されるページ)をindex.htmlからindex.shtmlに変更してみる。
[root@fedora ~]# vi /var/www/html/.htaccess ← .htaccessファイル作成
DirectoryIndex index.shtml

[root@fedora ~]# vi /var/www/html/index.shtml ← .htaccessテスト用ページ作成
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">※システムの文字コードがUTF-8の場合
<meta http-equiv="Content-Type" content="text/html; charset=euc-jp">※システムの文字コードがEUCの場合
<title>テスト</title>
</head>
<body>
<p>.htaccessによるWebサーバ設定(例としてDirectoryIndex)の変更テスト</p>
このページのファイル名は<!--#echo var="DOCUMENT_NAME" -->
</body>
</html>
http://サーバーIPアドレス/にアクセスしてindex.shtmlが表示されればOK

(5)PHP確認
PHPで簡単なテストページを表示してみる。
[root@fedora ~]# vi /var/www/html/test.php ← PHPテスト用ページ作成
<?php
  phpinfo();
?>
http://サーバーIPアドレス/test.phpにアクセスしてphpinfoページが表示されればOK

(6)Webサーバー確認後始末
上記の確認で作成したテスト用ページ等を全て削除する。
[root@fedora ~]# rm -f /var/www/html/* ← 作成したテスト用ページを全て削除

[root@fedora ~]# rm -f /var/www/html/.htaccess ← 作成した.htaccessを削除


■関連コンテンツ




▲このページのトップへ戻る

プライバシーポリシー