====== Linux:CentOS6:Apacheに繋がらない ======
* Apache で色々試そうと、CentOS をインストールしました。ブラウザから Apache への接続がすんなりではなかったので、Apacheの起動からブラウザで接続できるまでを解説します。
===== 環境 =====
| OS | CentOS6.6 |
| インストールパッケージ | Web Server |
| IPアドレスの取得 | DHCPより取得(192.168.0.100) |
===== Apacheの起動 =====
==== Apacheの起動確認 ====
* CentOS インストール直後、Apacheが起動されているか確認します。どうやらそのままでは起動しないようです。
>/etc/init.d/httpd status
httpd は停止しています
==== Apacheを手動で起動 ====
* ブラウザからApacheへの接続確認のため、とりあえず手動で Apache を起動します。エラーが出ますが無視します。:-x
>/etc/init.d/httpd start
Could not reliably determine the server's fully qualified domain name, using localhost.localdomain for ServerName
[ OK ]
===== ブラウザから接続 =====
==== ブラウザから接続できない ====
* ブラウザから接続できるか確認します。ドメインの設定を行っていないので ネットワークに繋がっている別の PC のブラウザから以下のようにIPアドレスを指定して接続します。が、タイムアウトになり接続できません、
http://192.168.0.100
==== ping の応答あり ====
* 念のため 別の PC から ping を打ってみますが、応答があります。
C:\Users\hoge>ping 192.168.0.100
192.168.0.100 に ping を送信しています 32 バイトのデータ:
192.168.0.100 からの応答: バイト数 =32 時間 <1ms TTL=64
192.168.0.100 からの応答: バイト数 =32 時間 <1ms TTL=64
192.168.0.100 からの応答: バイト数 =32 時間 <1ms TTL=64
==== SELinux を無効にする ====
* SELinux の動作を確認します。SELinuxが有効になっているようです。
>getenforce
Enforcing ← 有効
* Apache への接続をとりあえず確認したいので、SELinux を一旦無効にします。 設定は/etc/sysconfig/selinux です。
SELINUX=enforceing
を
SELINUX=disebled
に変更してLinuxを**再起動**します。
===== ブラウザから再度接続 =====
==== Apacheを再び起動 ====
* 再起動したので再度 apache を立ち上げます。
>/etc/init.d/httpd start
Could not reliably determine the server's fully qualified domain name, using localhost.localdomain for ServerName
[ OK ]
==== やっぱりブラウザから接続できない ====
* 再度ブラウザから以下のURLを入力して接続できるか確認します。が、状況は変わりません。
http://192.168.0.100
==== ファイアーウォール(iptables)の確認 ====
* 次にファイアーウォール(iptables)の確認を行ってみます。見たところhttpプロトコル(port 80)を通過させていないようです。
>iptables -L
Chain INPUT (policy ACCEPT)
target prot opt source destination
ACCEPT all -- anywhere anywhere state RELATED,ESTABLISHED
ACCEPT icmp -- anywhere anywhere
ACCEPT all -- anywhere anywhere
ACCEPT tcp -- anywhere anywhere state NEW tcp dpt:ssh
REJECT all -- anywhere anywhere reject-with icmp-host-prohibited
Chain FORWARD (policy ACCEPT)
target prot opt source destination
REJECT all -- anywhere anywhere reject-with icmp-host-prohibited
Chain OUTPUT (policy ACCEPT)
target prot opt source destination
>
==== ファイアーウォール(iptables)でhttpプロトコル(port 80)を通過させる。 ====
* とりあえず、iptables の設定ファイルをバックアップ
>/etc/sysconfig/iptables iptables.ORG
* httpプロトコルを追加します。編集するファイルは /etc/sysconfig/iptables です。
1 # Firewall configuration written by system-config-firewall
2 # Manual customization of this file is not recommended.
3 *filter
4 :INPUT ACCEPT [0:0]
5 :FORWARD ACCEPT [0:0]
6 :OUTPUT ACCEPT [0:0]
7 -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
8 -A INPUT -p icmp -j ACCEPT
9 -A INPUT -i lo -j ACCEPT
10 -A INPUT -m state --state NEW -m tcp -p tcp --dport 22 -j ACCEPT
11 -A INPUT -j REJECT --reject-with icmp-host-prohibited
12 -A FORWARD -j REJECT --reject-with icmp-host-prohibited
13 COMMIT
* 10行目の ssh(port22)を通過させる設定の後にhttp(port80)を通過させるための1行を追加します。
-A INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT
* ファイアーウォール(iptables)を再起動します。
>/etc/rc.d/init.d/iptables restart
iptables: チェインをポリシー ACCEPT へ設定中filter [ OK ]
iptables: ファイアウォールルールを消去中: [ OK ]
iptables: モジュールを取り外し中: [ OK ]
iptables: ファイアウォールルールを適用中: [ OK ]
==== 繋がった!! ====
* 改めてブラウザから接続できるか確認します。やっと繋がり apche のテストページが表示されました。
http://192.168.0.100
===== サーバ起動時に Apache を起動 =====
* サーバを起動する度に Apache を起動するのも面倒なので、サーバ起動時に Apache も起動するように設定します。
==== サービスの確認 ====
* 念のため、Apache の起動設定を確認します。確認するには chkconfig コマンドを使用します。 Apache のサービス名は、httpd です。すべてのランレベルで off となっています。
>chkconfig --list | grep httpd
httpd 0:off 1:off 2:off 3:off 4:off 5:off 6:off
==== 起動設定 ====
* サーバ起動時に Apache も起動するよう設定を変更します。ランレベル2~5 で起動するように設定されました。
>chkconfig httpd on
>chkconfig --list | grep httpd
httpd 0:off 1:off 2:on 3:on 4:on 5:on 6:off