Javaな日々

NO JAVA, NO LIFE.

さくら VPS (CentOS 6.3) に LAMP を構築する - Apache インストール

  1. Apache のインストール
    今回は Web サーバーとして一般的な Apache をインストールする.アカウントを root に切り替えて yum から Apache のパッケージをインストール,起動の確認,スタートアップの設定をする.

    [admin@www****** ~]$ su -
    [root@www****** ~]# yum -y install httpd
    [root@www****** ~]# /etc/rc.d/init.d/httpd start
    [root@www****** ~]# chkconfig httpd on
    Starting httpd:                                            [  OK  ]
    [root@www****** ~]# chkconfig --list httpd
    httpd          	0:off	1:off	2:on	3:on	4:on	5:on	6:off

  2. Apache の設定
    オリジナルの設定ファイルをバックアップしてからエディタで以下の項目を編集する.

    [root@ ~]# cp /etc/httpd/conf/httpd.conf /etc/httpd/conf/httpd.conf.org
    [root@ ~]# vi /etc/httpd/conf/httpd.conf

    • レスポンスヘッダの OS のバージョンを非表示

      ServerTokens OS
      ↓
      ServerTokens Prod

    • クライアントとの接続を保持

      KeepAlive Off
      ↓
      KeepAlive On

    • サーバー管理者を (自分のメールアドレスに) 変更

      ServerAdmin root@localhost
      ↓
      ServerAdmin admin@example.com

    • サーバー名を (自分のドメインに) 変更

      #ServerName www.example.com:80
      ↓
      ServerName www.example.com:80

    • ドキュメントルートを確認 (ここにおいたコンテンツが Web に公開される)

      DocumentRoot "/var/www/html" ←ドキュメントルートを確認

    • 以下の部分を探し,各所書き換える.

      <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.2/mod/core.html#options
      # for more information.
      #
          # Options Indexes FollowSymLinks
          Options -Indexes ExecCGI FollowSymLinks # 変更: CGI許可,ファイル一覧表示禁止
      
      #
      # 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を許可
      
      #
      # Controls who can get stuff from this server.
      #
          Order allow,deny
          Allow from all
      
      </Directory>

    • ユーザーディレクティブを有効にしたい場合は次のように書き換える
      (ユーザーディレクティブを利用する場合は /home/user_name のディレクトリを chmod でパーミッションを 755 などにしないといけない.また,ユーザーのホームディレクトリには public_html というディレクトリが必要でこれがドキュメントルートになる.)

      <IfModule mod_userdir.c>
          #
          # UserDir is disabled by default since it can confirm the presence
          # of a username on the system (depending on home directory
          # permissions).
          #
          #UserDir disabled # コメントアウトして有効化
      
          #
          # To enable requests to /~user/ to serve the user's public_html
          # directory, remove the "UserDir disabled" line above, and uncomment
          # the following line instead:
          # 
          UserDir public_html # コメントアウトを解除して設定を有効化
      
      </IfModule>
      #<Directory /home/*/public_html>
      #    AllowOverride FileInfo AuthConfig Limit
      #    Options MultiViews Indexes SymLinksIfOwnerMatch IncludesNoExec
      #    <Limit GET POST OPTIONS>
      #        Order allow,deny
      #        Allow from all
      #    </Limit>
      #    <LimitExcept GET POST OPTIONS>
      #        Order deny,allow
      #        Deny from all
      #    </LimitExcept>
      #</Directory>
      
      ↓
      
      <Directory /home/*/public_html>
          AllowOverride All # .htacess許可
          Options -Indexes ExecCGI FollowSymLinks # CGI許可,ファイル一覧表示禁止
          <Limit GET POST OPTIONS>
              Order allow,deny
              Allow from all
          </Limit>
          <LimitExcept GET POST OPTIONS>
              Order deny,allow
              Deny from all
          </LimitExcept>
      </Directory>

    • index の設定 (CGI を許可する)

      DirectoryIndex index.html index.html.var
      ↓
      DirectoryIndex index.html index.cgi index.html.var

    • ログの設定 (414 ERROR は記録しない)

      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

    • カスタムログの設定 (worm と画像のアクセスをログに記録しない)

      CustomLog logs/access_log combined
      ↓
      SetEnvIf Request_URI "cmd\.exe" nolog
      SetEnvIf Request_URI "root\.exe" nolog
      SetEnvIf Request_URI "Admin\.dll" nolog
      SetEnvIf Request_URI "NULL\.IDA" nolog
      SetEnvIf Request_URI "^/_mem_bin/" nolog
      SetEnvIf Request_URI "^/_vti_bin/" nolog
      SetEnvIf Request_URI "^/c/" nolog
      SetEnvIf Request_URI "^/d/" nolog
      SetEnvIf Request_URI "^/msadc/" nolog
      SetEnvIf Request_URI "^/MSADC/" nolog
      SetEnvIf Request_URI "^/scripts/" nolog
      SetEnvIf Request_URI "^/default.ida" nolog
      SetEnvIf Request_URI "\.(gif)|(jpg)|(png)|(ico)|(css)$" nolog
      SetEnvIf Remote_Addr 192.168. no_log
      CustomLog logs/access_log combined env=!no_log

    • エラー画面でApacheのバージョン非表示

      ServerSignature On
      ↓
      ServerSignature Off

    • Indexes が OFF なのでファイル一覧表示のアイコンの設定はすべてコメントアウト

      Alias /icons/ "/var/www/icons/"
      <Directory "/var/www/icons">
          Options Indexes MultiViews
          AllowOverride None
          Order allow,deny
          Allow from all
      </Directory>
      ↓
      #Alias /icons/ "/var/www/icons/"
      #<Directory "/var/www/icons">
      #    Options Indexes MultiViews FollowSymLinks
      #    AllowOverride None
      #    Order allow,deny
      #    Allow from all
      #</Directory>

    • ScriptAlias は使わないためすべてコメントアウト

      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>

    • autoindexに関係する設定をすべてコメントアウト

      #
      # IndexOptions: Controls the appearance of server-generated directory
      # listings.
      #
      #IndexOptions FancyIndexing VersionSort NameWidth=* HTMLTable Charset=UTF-8
      
      #
      # AddIcon* directives tell the server which icon to show for different
      # files or filename extensions.  These are only displayed for
      # FancyIndexed directories.
      #
      #AddIconByEncoding (CMP,/icons/compressed.gif) x-compress x-gzip
      
      #AddIconByType (TXT,/icons/text.gif) text/*
      #AddIconByType (IMG,/icons/image2.gif) image/*
      #AddIconByType (SND,/icons/sound2.gif) audio/*
      #AddIconByType (VID,/icons/movie.gif) video/*
      
      #AddIcon /icons/binary.gif .bin .exe
      #AddIcon /icons/binhex.gif .hqx
      #AddIcon /icons/tar.gif .tar
      #AddIcon /icons/world2.gif .wrl .wrl.gz .vrml .vrm .iv
      #AddIcon /icons/compressed.gif .Z .z .tgz .gz .zip
      #AddIcon /icons/a.gif .ps .ai .eps
      #AddIcon /icons/layout.gif .html .shtml .htm .pdf
      #AddIcon /icons/text.gif .txt
      #AddIcon /icons/c.gif .c
      #AddIcon /icons/p.gif .pl .py
      #AddIcon /icons/f.gif .for
      #AddIcon /icons/dvi.gif .dvi
      #AddIcon /icons/uuencoded.gif .uu
      #AddIcon /icons/script.gif .conf .sh .shar .csh .ksh .tcl
      #AddIcon /icons/tex.gif .tex
      #AddIcon /icons/bomb.gif core
      
      #AddIcon /icons/back.gif ..
      #AddIcon /icons/hand.right.gif README
      #AddIcon /icons/folder.gif ^^DIRECTORY^^
      #AddIcon /icons/blank.gif ^^BLANKICON^^
      
      #
      # DefaultIcon is which icon to show for files which do not have an icon
      # explicitly set.
      #
      #DefaultIcon /icons/unknown.gif
      
      #
      # AddDescription allows you to place a short description after a file in
      # server-generated indexes.  These are only displayed for FancyIndexed
      # directories.
      # Format: AddDescription "description" filename
      #
      #AddDescription "GZIP compressed document" .gz
      #AddDescription "tar archive" .tar
      #AddDescription "GZIP compressed tar archive" .tgz
      
      #
      # ReadmeName is the name of the README file the server will look for by
      # default, and append to directory listings.
      #
      # HeaderName is the name of a file which should be prepended to
      # directory indexes.
      #ReadmeName README.html
      #HeaderName HEADER.html
      
      #
      # IndexIgnore is a set of filenames which directory indexing should ignore
      # and not include in the listing.  Shell-style wildcarding is permitted.
      #
      #IndexIgnore .??* *~ *# HEADER* README* RCS CVS *,v *,t

    • AddLanguage ja .ja の行を最上位に移動して日本語を優先

      AddLanguage ja .ja
      AddLanguage ca .ca
      AddLanguage cs .cz .cs
      AddLanguage da .dk
      AddLanguage de .de
      AddLanguage el .el
      AddLanguage en .en
      AddLanguage eo .eo
      AddLanguage es .es
      AddLanguage et .et
      AddLanguage fr .fr
      AddLanguage he .he
      AddLanguage hr .hr
      AddLanguage it .it
      AddLanguage ko .ko
      AddLanguage ltz .ltz
      AddLanguage nl .nl
      AddLanguage nn .nn
      AddLanguage no .no
      AddLanguage pl .po
      AddLanguage pt .pt
      AddLanguage pt-BR .pt-br
      AddLanguage ru .ru
      AddLanguage sv .sv
      AddLanguage zh-CN .zh-cn
      AddLanguage zh-TW .zh-tw

    • MIME 言語タイプの適用優先順位で日本語を優先

      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

    • CGI を許可

      # AddHandler cgi-script .cgi
      ↓
      AddHandler cgi-script .cgi .pl

    ここまで終わったら保存して終了.
    設定ファイルの文法チェックを行い,正常であれば Apache を再起動して設定を適用させる.

    [root@www****** ~]# apachectl configtest
    Syntax OK
    [root@www****** ~]# /etc/rc.d/init.d/httpd restart
    Stopping httpd:                                            [  OK  ]
    Starting httpd:                                            [  OK  ]

  3. HTTP 接続用のポートを開放.

    [admin@www****** ~]$ sudo vi /etc/sysconfig/iptables

    以下を追記します.

    -A FIREWALL -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT

  4. 以下のコマンドでドキュメントルートにテスト用の簡易 HTML を配置,ブラウザで XXX.XXX.XXX.XXX を開いて確認してみる.

    [root@www****** ~]# echo '<!DOCTYPE html><html><head><title>Hello</title></head><body>Hello</body></html>' > /var/www/html/index.html

    一旦ここまででサーバーを再起動しておく.

    [root@www****** ~]# reboot