CentOSにApache HTTP Server(httpd)をインストールしてテストページを表示する

スポンサーリンク

CentOSにWebサーバーのhttpdをインストールして、テストページを表示させるまでの方法です。設定ファイルは特に変更しません。バージョンが2.2系ですが最新版のインストールは別途書きます。

httpdのインストール

yumコマンドで httpd をインストールします。root で実行してください。

# yum -y install httpd
...
インストール:
  httpd.x86_64 0:2.2.15-39.el6.centos

依存性関連をインストールしました:
  apr.x86_64 0:1.3.9-5.el6_2     apr-util.x86_64 0:1.3.9-3.el6_0.1  apr-util-ldap.x86_64 0:1.3.9-3.el6_0.1  httpd-tools.x86_64 0:2.2.15-39.el6.centos
  mailcap.noarch 0:2.1.31-2.el6
...
# httpd -v
Server version: Apache/2.2.15 (Unix)
Server built:   Oct 16 2014 14:48:21

以下のようなディレクトリやファイルが作成されています。

/etc/httpd/
/etc/sysconfig/httpd
/etc/logrotate.d/httpd
/usr/lib64/httpd/
/var/www/
/var/run/httpd/
/var/log/httpd/

インストールが出来たら起動してみましょう。以下どちらのコマンドでも同じです。

# service httpd start
httpd を起動中: httpd: Could not reliably determine the server's fully qualified domain name, using 127.0.0.1 for ServerName
                                                           [  OK  ]
---
# /etc/init.d/httpd start
httpd を起動中: httpd: Could not reliably determine the server's fully qualified domain name, using 127.0.0.1 for ServerName
                                                           [  OK  ]

停止はstop、再起動はrestart、状態を確認するにはstatusを使用します。

起動した時にhttpd: Could not reliably determine the server's fully qualified domain name, using 127.0.0.1 for ServerNameというメッセージが表示されていますが、これは httpd.conf の ServerName を設定することで消えます。

# vi /etc/httpd/conf/httpd.conf
...
#
# 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
...
# service httpd restart
httpd を停止中:                                            [  OK  ]
httpd を起動中:                                            [  OK  ]

起動後にブラウザで確認すると以下の画面が表示されます。httpdをインストールしたマシンのブラウザならlocalhostやホスト名、仮想マシンやサーバーにインストールした場合はIPアドレスを入力するだけでOKです。

f:id:tasukujp:20150627153830p:plain

ウェルカムページについて

先程ブラウザで確認した画面ですが、これは/etc/httpd/conf.d/welcome.confという設定ファイルによって、デフォルト状態でアクセスした時に表示されるようになっています。ファイルの中身は以下の通りです。

# cat /etc/httpd/conf.d/welcome.conf
<LocationMatch "^/+$">
    Options -Indexes
    ErrorDocument 403 /error/noindex.html
</LocationMatch>

welcome.confを直接参照しているかというとそうではなく、/etc/httpd/conf/httpd.conf内にある以下の一文で読み込んでいます。

#
# Load config files from the config directory "/etc/httpd/conf.d".
#
Include conf.d/*.conf

処理内容としては以下の3つです。

LocationMatch

<LocationMatch "^/+$"></LocationMatch>内の処理は正規表現にマッチするURLのみに適用されます。今回の場合はIPアドレスのみ(http://192.168.33.100/)というURLでアクセスしているので、これにマッチしている事になります。

 LocationMatch - Apache HTTP Server Version 2.4

Options -Indexes

Indexesオプションは DirectoryIndex で指定したファイルが無ければディレクトリ内の一覧を表示します。デフォルトでは以下のようになっています。

#
# DirectoryIndex: sets the file that Apache will serve if a directory
# is requested.
#
# The index.html.var file (a type-map) is used to deliver content-
# negotiated documents.  The MultiViews Option can be used for the
# same purpose, but it is much slower.
#
DirectoryIndex index.html index.html.var

そのため、index.htmlが無い場合は次のような画面になりますが、ディレクトリの中身が丸見えになってしまうとあまりよろしくないです。

f:id:tasukujp:20150627172023p:plain

これを表示させないために-Indexesのようにマイナスを付けて指定します。

f:id:tasukujp:20150627182045p:plain

 DocumentRoot - Apache HTTP Server Version 2.4

ちなみに DirectoryIndex はファイル名しか書いてないですが DocumentRoot で指定したディレクトリ内を参照します。

#
# 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"

 DocumentRoot - Apache HTTP サーバ バージョン 2.4

ErrorDocument

エラーが発生した時の動作を指定します。index.htmlが無い場合は 403エラーが発生しますがデフォルト(上記のForbiddenページ)のページではなくて指定した画面を表示してくれます。

指定してるのは/error/noindex.htmlですが、こんなディレクトリどこにも無いと思います。それもそのはずで httpd.conf の中にAlias /error/ "/var/www/error/"という一文があります。これによって/var/www/error/noindex.htmlを表示している事がわかります。

それが次の画面です。最初に表示されたのはエラーページだった訳ですね。

f:id:tasukujp:20150627153830p:plain

 ErrorDocument - Apache HTTP サーバ バージョン 2.4

index.htmlの作成

という訳で index.html を作成しましょう。

# vi /var/www/html/index.html
<html>
<body>
<h1>Hello World!</h1>
</body>
</html>

http://192.168.33.100/で再度アクセスしてページが変わっていたら成功です。

httpdの自動起動

インストールした段階では自動起動がONになっていないため、再起動する度に手動で httpd を起動する必要があります。

# chkconfig --list | grep httpd
httpd           0:off   1:off   2:off   3:off   4:off   5:off   6:off

自動起動をONにしておくことで起動する手間がいらなくなるのでしておきましょう。

# chkconfig httpd on
# chkconfig --list | grep httpd
httpd           0:off   1:off   2:on    3:on    4:on    5:on    6:off