Tahoo!!

自分の勉強していること(コンピュータ関連 / ネットワーク / セキュリティ / サーバ)や趣味について書いていきます

CentOSでWebアプリケーションサーバ構築

結構前の話になるが、研究室でPHPで書いたWebアプリケーションを走らせるサーバ構築する機会があったのでやったこと等メモ。

環境

OS

その他ソフトウェア

Web Server

CentOS 6.5には、標準でApache HTTP Server 2.215(httpd)がインストールされていた。そのためパッケージのインストール作業の必要はなかった。

httpd.conf

httpdの設定ファイルであるhttpd.confは/etc/httpd/conf/httpd.confにある。
特に変更しなくても動いたが、セキュリティのために以下の部分のみ変更した。

ServerTokens OS # OS名とApacheのバージョンを表示
↓
ServerTokens Prod # Apacheが動いていることのみ表示

iptables

Cent OSは標準では、HTTPが標準で利用するTCPの80番ポートがフィルタされているようになっているため、iptablesの設定(/etc/sysconfig/iptabels)を編集する必要がある。
以下のように追記することにより、TCPポート80番のパケットが通るようになる。(変更しないとlocalhostではアクセス出来るが、外からはアクセスできない)

# Firewall configuration written by system-config-firewall
# Manual customization of this file is not recommended.
*filter
:INPUT ACCEPT [0:0]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [0:0]
-A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
-A INPUT -p icmp -j ACCEPT
-A INPUT -i lo -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 22 -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT # 追記
-A INPUT -j REJECT --reject-with icmp-host-prohibited
-A FORWARD -j REJECT --reject-with icmp-host-prohibited
COMMIT

コンテンツの配置

/var/www/htmlがDocumentRootに設定されていてるので、ここにindex.htmlという名前のファイルを置くと、index.htmlに記述された内容が表示される。

Webサーバ関連はこの本を参考に構築した。

CentOS 6で作るネットワークサーバ構築ガイド (Network Server Construction Guide S)

CentOS 6で作るネットワークサーバ構築ガイド (Network Server Construction Guide S)

PHP

自分の環境では、デフォルトではPHPがインストールされていなかったので、yumを使ってインストール。

yum -y install php

次に、上記のhttpd.confの以下の部分を変更

DirectoryIndex index.html index.html.var
↓
DirectoryIndex index.html index.html.var index.php

AddType application/x-httpd-php .php # AddTypeがまとめて書かれている付近に追記

あとは、以下の参考サイトのように/etc/php.iniファイルを編集

Apacheを以下のコマンドで再起動

[root@localhost ~]# service httpd restart
httpd を停止中:                                            [  OK  ]
httpd を起動中: httpd: Could not reliably determine the server's fully qualified domain name, using localhost.localdomain for ServerName
                                                           [  OK  ]

以下のようなPHPファイルをDocumentRootに作成。

[root@localhost ~]# echo "<?php phpinfo(); ?>" > /var/www/html/info.php

ブラウザでhttp://サーバのIP(ドメイン)/info.phpにアクセスして、無事動作を確認。
f:id:takahoyo:20140507161654p:plain

PHP関連は以下のサイトを参考にした。
Apache2 PHP5 インストール | CentOSサーバー構築マニュアル

認証

認証が必要になるページもあったので、ダイジェスト認証も設定した。

まず、httpd.confに以下の内容を追記

<Directory "/var/www/html/hoge">
        AuthType Digest
        AuthName "Secret"
        AuthDigestDomain /hoge/
        AuthUserFile /etc/httpd/.htdigest
        require user hoge # hogeはユーザ名
</Directory>

次にパスワードファイル.htdigestを、/etc/httpd/にhtdigestコマンドで作成(場所はここでなくてもよい)

[root@localhost httpd]# htdigest -h
Usage: htdigest [-c] passwordfile realm username
The -c flag creates a new file.
[root@localhost httpd]# htdigest -c .htdigest hoge hoge
Adding password for hoge in realm hoge.
New password:
Re-type new password:

そして、上記の方法で再起動を行い設定完了。

試しにアクセスすると、以下のようなウインドウが表示され、認証を求められる。
f:id:takahoyo:20140507163046p:plain

ダイジェスト認証に関しては、以下のサイトを参考にした。
@IT:Apacheでユーザー認証を行うには(Digest認証編)