Apache のインストールと初期設定をおこないます。
1)Apache のインストール
最初にApacheインストールされているか確認します。# rpm -qa httpd #無いですね。
Apacheが無いのでインストールを行ないます。
ソースから自分でコンパイルする方法があるらしいのですが、まだハードルが高そうなので、コマンド一発で済むので、yum でパッケージからインストールする方法にしたいと思います。
インストールコマンド
# sudo yum -y install httpd
一応、アンインストールの場合
# sudo yum remove httpd
インストールしたらバージョンの確認をします。
# rpm -qa httpd httpd-2.2.3-45.el5.centos.1 # httpd -v Server version: Apache/2.2.3 Server built: May 4 2011 06:51:15
2)Apache の初期設定(httpd.conf)
Apache の設定ファイルとなる httpd.conf を編集しますので、とりあえずhttpd.confのバックアップをとってから作業を進めていきます。# sudo cp /etc/httpd/conf/httpd.conf /etc/httpd/conf/httpd_110720.conf # sudo vi /etc/httpd/conf/httpd.conf
初期状態では OS 名が返される。OSのバージョンを表示させないように変更。Prod を指定するとレスポンスヘッダには Server: Apache という名前しか返されない。
# Don't give away too much information about all the subcomponents # we are running. Comment out this line if you don't mind remote sites # finding out what major optional modules you are running ServerTokens OS ↓ ServerTokens Prod
コメントアウトを解除してサーバーのホスト名を書きます。
DNS に登録していないのでIPを指定しときます。
# ServerName gives the name and port that the server uses to identify itself. # This can often be determined automatically, but we recommend you specify # it explicitly to prevent problems during startup. # # If this is not set to valid DNS name for your host, server-generated # redirections will not work. See also the UseCanonicalName directive. # # If your host doesn't have a registered DNS name, enter its IP address here. # You will have to access it by its address anyway, and this will make # redirections work in a sensible way. # #ServerName www.example.com:80 ↓ ServerName XXX.XXX.XXX.XXX
Web サーバーのルートディレクトリの設定ですが、特に変更はしないのでデフォルトのままでOK。
# DocumentRoot: The directory out of which you will serve your # documents. By default, all requests are taken from this directory, but # symbolic links and aliases may be used to point to other locations. # DocumentRoot "/var/www/html"
アクセスされたディレクトリ内に DirectoryIndex で設定されたファイル ( 初期状態では index.html index.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 FollowSymLinks
エラー画面などに出力されるサーバー情報(Apacheのバージョン)を非表示
# Optionally add a line containing the server version and virtual host # name to server-generated pages (internal error documents, FTP directory # listings, mod_status and mod_info output etc., but not CGI generated # documents or custom error documents). # Set to "EMail" to also include a mailto: link to the ServerAdmin. # Set to one of: On | Off | EMail # ServerSignature On ↓ ServerSignature Off
Esc でコマンドモードに戻り、:wq で設定を保存する。そして以下のコマンドを実行し、設定に問題がないことを確認する。
# sudo apachectl configtest Syntax OK
Apache を起動させてみる。
# sudo service httpd start Starting httpd: [ OK ]
起動できたら ServerName に指定したアドレスにアクセスしてみる。
今回は IP にしたので http://XXX.XXX.XXX.XXX/
テストページが表示されたら成功です。

最後に OS 起動時に Apache が自動的に起動されるように設定します。
まず、以下のコマンドを実行して自動起動が有効かを調べます。
# chkconfig --list httpd httpd 0:off 1:off 2:off 3:off 4:off 5:off 6:off無効です。
chkconfig コマンドで httpd ( Apache ) を登録する。
# sudo chkconfig httpd on # chkconfig --list httpd httpd 0:off 1:off 2:on 3:on 4:on 5:on 6:off有効になりました。
ちなみに chkconfig –list で表示される数値はランレベルと呼ばれるシステムの動作モードである。数値の表す内容は以下のようになる。
0 : シャットダウン ( システム停止中 )
1 : シングル ユーザー モード ( root のみ )
2 : マルチ ユーザー モード ( ネットワークなし )
3 : マルチ ユーザー モード ( テキスト )
4 : 未使用
5 : マルチ ユーザー モード ( グラフィカル )
6 : システム再起動
参考:Web制作 さくらのVPSに挑戦(駄文)
⇒ さくらのVPSを使ってみる【4】-MySQLをインストールする