LoginSignup
42
44

More than 5 years have passed since last update.

CentOSにapacheをインストールして、DocumentRootを変更したらあれこれ躓いた

Last updated at Posted at 2014-07-21

前提

  • CentOS 6.5
  • apache 2.2.15

手順

apacheのインストール

sudo yum -y install httpd

DocumentRootの変更

sudo vim /etc/httpd/conf/httpd.conf
httpd.conf
# DocumentRoot /var/www/html デフォルト
DocumentRoot /home/user/www/html

userでwww/htmlを作成する

cd ~
mkdir -p www/html

このあと、httpdを再起動したら

Syntax error on line 293 of /etc/httpd/conf/httpd.conf:DocumentRoot must be a directory

が出てhttpdを起動できず.

SELinux

ここ(ApacheのDocumentRootを変更すると、Apacheが起動しない。)を見ると、SELinuxというセキュリティ管理機能が原因で、エラーになっているらしい。

けど上記サイトに記載されている方法では解決せず、stackoverflowと、https://www.centos.org/forums/viewtopic.php?t=1742 を参考に

sudo yum -y install policycoreutils-python # semanageなどSELinuxを制御するツール一式がインストールされる
sudo setsebool -P httpd_enable_homedirs on
sudo chcon -R -t httpd_sys_content_t /home/user/www

sudo service httpd restart

でSyntax errorの問題は解決して起動した。

ターゲットポリシーのドキュメントを読んだ限りだと、httpdなどのLISTENするプロセスは、制限されたドメインで実行されるようで、制限されたプロセスが、ファイルにアクセスするためには、アクセス対象のファイルにドメインに対応したラベル(上の場合httpd_sys_content_t)が設定されている必要があるらしい。

iptables

外部からアクセスするために、ファイアウォールの設定

-A INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT

を追加して、80番ポートを有効にする。
有効にしたらiptablesを再起動

service iptables restart

index.htmlがForbidden

次の問題は、http://xxx.xxx.xxx.xxx/index.html
にアクセスしてもForbiddenが表示されてアクセスできないこと(事前に/home/user/www/html/index.htmlを作成している)。

ディレクトリのパーミッション

DocumentRootに設定したディレクトリの各階層に対してアクセス権o+xを設定をする必要があるとのこと。

自分の環境では、これでひとまず解決.

おまけ SELinuxの状態確認と設定

有効無効の確認

getenforce # Enforcingなら有効, Permissiveなら無効

有効無効の設定

set setenforce 0 # 0なら無効, 1なら有効

各種フラグ確認

getsebool -a | grep httpd # httpdに関して調べるなら

各種フラグ設定

setsebool -P flagname 0 or 1 # -P は永続性オプション. flagnameはgetseboolで確認できる.

ファイルやディレクトリのラベルの確認方法

ls -Z

参考

SELinuxを理解するために、下のドキュメントは一読した方がよいかも.
https://access.redhat.com/documentation/ja-JP/Red_Hat_Enterprise_Linux/6/html/Security-Enhanced_Linux/chap-Security-Enhanced_Linux-Working_with_SELinux.html

42
44
1

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
42
44