LoginSignup
0
3

More than 5 years have passed since last update.

CentOS7 - Apacheでポートを解放してるのにブラウザからアクセスできない

Last updated at Posted at 2017-01-18

時系列になってるので先に解決策を書く

下記のコマンドをセットで実行したら治った。無事、アパッチにブラウザからアクセスできた。CentOS7では従来までのCentOS6のときのiptableではポートは開かず、firewallコマンドを実行しないとポートが開かない。調べてみたらiptableは起動すらしてなかった

firewall-cmd --permanent --zone=public --add-service=http 
firewall-cmd --permanent --zone=public --add-service=https
firewall-cmd --reload

参考

Firewall-cmdというファイアウォールが自動的に有効になっている場合があり、見落としがちです。
ポート80 (http) と 443 (https)を開けるには、下記のようにします。

/
/
/
/

wgetしたら接続を拒否される

wget http://160.16.120.119/
--2017-01-18 10:35:01--  http://160.16.120.119/
160.16.120.119:80 に接続しています... 失敗しました: 接続を拒否されました.

Apacheの実行ユーザー周りを当たってみる

やること

・httpdの設定に入る
・httpd.confを書き換える /etc/httpd/conf

ssh_config実行ユーザー周りの状況

#
# If you wish httpd to run as a different user or group, you must run
# httpd as root initially and it will switch.
#
# User/Group: The name (or #number) of the user/group to run httpd as.
# It is usually good practice to create a dedicated user and group for
# running httpd, as with most system services.
#
User apache
Group apache

apacheの実行ユーザーを作ってみる

# groupadd httpd
# useradd -g httpd -d /var/empty/httpd -s /sbin/nologin httpd

-gオプションはyーざーの所属するイニシャライズグループを指定する
-dはユーザーのログインディレクトリを指定する
-sはユーザーのログインシェルを指定する

自作した実行ユーザーにapacheの実行ユーザーを変更した

#
# If you wish httpd to run as a different user or group, you must run
# httpd as root initially and it will switch.
#
# User/Group: The name (or #number) of the user/group to run httpd as.
# It is usually good practice to create a dedicated user and group for
# running httpd, as with most system services.
#
User httpd
Group httpd

実行ユーザーがhttpdか確認してみる

[root@tk2-236-27615 conf]#ps auxw | grep httpd
root      1421  0.0  0.1 112664   980 pts/0    R+   11:13   0:00 grep --color=auto httpd

ちょっとgrepコマンドがよくわからなかったので調べてた

grepコマンドはファイルや標準入力から正規表現でマッチする行を探し出すコマンド。正規表現でマッチする行を探し出すコマンド。

grepコマンドの使用例

grep -Gi -e httpd httpd.conf

大小関係なくhttpd.confからhttpdという文字を抽出する


[root@tk2-236-27615 conf]# grep -Gi -e httpd httpd.conf
# See <URL:http://httpd.apache.org/docs/2.4/> for detailed information.
# <URL:http://httpd.apache.org/docs/2.4/mod/directives.html>
# same ServerRoot for multiple httpd daemons, you will need to change at
ServerRoot "/etc/httpd"
# Statically compiled modules (those listed by `httpd -l') do not need
# If you wish httpd to run as a different user or group, you must run
# httpd as root initially and it will switch.
# User/Group: The name (or #number) of the user/group to run httpd as.
# running httpd, as with most system services.
User httpd
Group httpd
    # http://httpd.apache.org/docs/2.4/mod/core.html#options
# Load config files in the "/etc/httpd/conf.d" directory, if any.```

色々いじってたらバグってしまった

apacheを一度再起動できない

/etc/rc.d/init.d/httpd restart

だめ

別のコマンドで試すけどダメ

[root@tk2-236-27615 conf]# service httpd restart
Redirecting to /bin/systemctl restart  httpd.service
Job for httpd.service failed because the control process exited with error code. See "systemctl status httpd.service" and "journalctl -xe" for details.

停止すらできない

[root@tk2-236-27615 ~]# service httpd stop
Redirecting to /bin/systemctl stop  httpd.service

httpd.conf.orgをhttpd.confにmvして時を巻き戻す

mv httpd.conf httpd.conf.bu

mv conf.org httpd.conf

とりあえず動くようになったけど、原因を解明したい

原因を解明する

diffで差分とか見てservice httpd configtestとかで不具合が起こってる場所を探して治す

[root@tk2-236-27615 conf]# service httpd configtest
httpd: Syntax error on line 99 of /etc/httpd/conf/httpd.conf: /etc/httpd/conf/httpd.conf:99: <Directory> was not closed.

なんとなくもう一度wgetする

なぜか拒否はされなくなってる(なぞ)

wget 160.16.120.119
--2017-01-18 14:41:42--  http://160.16.120.119/
160.16.120.119:80 に接続しています... 接続しました。
HTTP による接続要求を送信しました、応答を待っています... 200 OK
長さ: 135 [text/html]
`index.html' に保存中

100%[======================================>] 135         --.-K/s 時間 0s

2017-01-18 14:41:42 (31.7 MB/s) - `index.html' へ保存完了 [135/135]

最終手段、手助けを借りる

アクセスが拒否されてるということはポートが開いてないことが原因だ
でも、前にもポートを開く作業はしてる。でも、ポートが開かない

よく調べてみると、CentOS7では従来までのCentOS6のときのiptableではポートは開かず
調べてみたらiptableは起動すらしてなかった

解決策

下記のコマンドをセットで実行したら治った
無事、アパッチにブラウザからアクセスできた


firewall-cmd --permanent --zone=public --add-service=http 
firewall-cmd --permanent --zone=public --add-service=https
firewall-cmd --reload

参考

引用

Firewall-cmdというファイアウォールが自動的に有効になっている場合があり、見落としがちです。
ポート80 (http) と 443 (https)を開けるには、下記のようにします。
0
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
3