概要:JakartaプロジェクトのApacheをインストールしWebサーバーを構築する。

1. ApacheとPHPのインストール

# yum -y install httpd ← httpdインストール
# yum -y install php php-mbstring ← php、php-mbstringインストール

2. Webサーバー設定

  1. ) httpd.confの編集
    # vi /etc/httpd/conf/httpd.conf ← httpd設定ファイル編集
    
    ServerTokens OS
      
    ServerTokens Prod ← エラーページ等でOS名を表示しないようにする
    
    #ServerName www.example.com:80
      
    ServerName centos54.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を追加
    
    <Directory "/var/www/icons">
        Options Indexes MultiViews
          
        Options MultiViews ← iconsディレクトリのファイル一覧を表示しないようにする
        AllowOverride None
        Order allow,deny
        Allow from all
    </Directory>
    
  2. ) httpdの編集
    PHPからメールが送信できない場合の対応。
    PHPのメール送信関数mail()を実行したときに、以下の理由により/var/log/httpd/error_logに次のようなメッセージが表示され、メールが送信できない場合がある。
    qmail-inject "read error"
    理由:
    Qmail sets and uses UNIX environment variables to modify its behavior. 
    QMAILMFTFILE is one of them, which specifies the file containing mailing list addresses. 
    When called within your web server, qmail-inject attempts to read /root/.lists by default, 
    a file it doesn't have permissions to read. Try unsetting the environment variable 
    if your web server log shows the following error:
    
         qmail-inject: fatal: read error
    
    You can unset $QMAILMFTFILE by issuing the following command before 
    starting (or restarting) your web server:
    
        #export -n QMAILMFTFILE
    
    # vi /etc/rc.d/init.d/httpd
    
    start() {
            echo -n $"Starting $prog: "
            check13 || exit 1
    # Add export yyyy/mm/dd        ← 追加する
            export -n QMAILMFTFILE ← 追加する
            LANG=$HTTPD_LANG daemon --pidfile=${pidfile} $httpd $OPTIONS
            RETVAL=$?
            echo
            [ $RETVAL = 0 ] && touch ${lockfile}
            return $RETVAL
    }
    
    また、/etc/php.iniのsendmail_pathパラメータに以下のような設定がある。
    sendmail_path   =       /usr/sbin/sendmail -t -i
    
    これはこのままでよい。これはqmailを使用した時のsendmailのwrapperである /var/qmail/bin/sendmailで、メールを送信する時はこれからqmail-injectを呼び出している。
  3. ) 不要ファイルの削除
    # rm -f /etc/httpd/conf.d/welcome.conf ← テストページ削除
    # rm -f /var/www/error/noindex.html ← テストページ削除
    
  4. ) 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が表示されることを確認
    
  5. ) ドキュメントルート所有者変更 ここでは、例としてドキュメントルート所有者を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サーバー起動

# /etc/rc.d/init.d/httpd start ← httpd起動
httpd を起動中:                                            [  OK  ]

# chkconfig httpd on ← httpd自動起動設定
# chkconfig --list httpd ← httpd自動起動設定確認
httpd           0:off   1:off   2:on    3:on    4:on    5:on    6:off
  ← ランレベル2~5のonを確認

4. ポート80番のOPEN
ネットワーク上でTCP/IPの80番ポートが開いていること。(サーバー、クライアント、ルーターなどのファイヤーウォールやセキュリティー設定で OPENされていること。)

5. 外部からのWebサーバーアクセス確認
外部からWebサーバーにアクセスできるか確認する。

# echo test > /var/www/html/index.html ← テストページ作成

ブラウザーから http://ホスト名/ を入力してアクセスする。

test

と表示されればOK。

# rm -f /var/www/html/index.html ← テストページ削除

6. Webサーバー確認

  1. ) Webページ表示確認
    # set | grep LANG ← システムの文字コードの確認
    LANG=ja_JP.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
  2. ) 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
  3. ) 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
  4. ) .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
  5. ) PHP確認 PHPで簡単なテストページを表示してみる。
    # vi /var/www/html/test.php ← PHPテスト用ページ作成
    
    <?php
      phpinfo();
    ?>
    
    http://サーバー名/test.phpにアクセスしてphpinfoページが表示されればOK
  6. ) メール送信テスト
    # mkdir /var/log/apllog ← ログファイル用のディレクトリーを作成。
    # chmod 777 /var/log/apllog
    # vi /var/www/html/test.php ← テスト用PHPファイルを作成。
    
    PHP Mail Test
    <?php
    $val = "PHP Mail Test";
    error_log($val."\n", 3, "/var/log/apllog/apl_error_log");
    error_log($val."\n", 1, "centos@ホスト名");
      ← サーバーに「ホスト名」を合わせる(以下同じ)。
    $message = $val."\n";
    $rt = 0;
    $rt = @mail('centos@ホスト名', 'My Subject', $message, "From: root@ホスト名");
    system('echo -e "to: centos@ホスト名\nFrom: centos@ホスト名\nSubject:Test Mail\n\nTest\n" | /var/qmail/bin/qmail-inject');
      ← qmailを使用する場合のみ。
    ?>
    
    Clientのブラウザーから http://ホスト名/test.phpでアクセスしてcentos@ホスト名にメールが送信されているか確認する。

7. Webサーバー確認後始末
上記の確認で作成したテスト用ページ等を全て削除する。

# rm -f /var/www/html/* ← 作成したテスト用ページを全て削除
# rm -f /var/www/html/.htaccess ← 作成した.htaccessを削除

8. 設定変更後の操作
設定変更後に以下の操作が必要。

  1. ) 設定の確認 次の設定ファイルのテストコマンドを実行し問題のないことを確認する。
    # /etc/rc.d/init.d/httpd configtest
    
  2. ) 設定の反映 次のコマンドを実行し設定ファイルを読み直す。
    # /etc/rc.d/init.d/httpd reload
    
  3. ) ブラウザーのリセット サーバー側の設定が更新されても、ブラウザー側では以前の情報(Cache)が残っていて、最新の情報が使用されず以前の情報が表示される場合が多い。その場合は
    1. )ブラウザーの履歴を削除する。
    2. )ブラウザーを再起動する。
    3. )再見込みする。 などをする。

最終更新のRSS
Last-modified: 2014-03-11 (火) 01:59:57 (3699d)