Red Hat Training

A Red Hat training course is available for Red Hat Enterprise Linux

3.4. 設定の実例

SELinux でどのように Apache HTTP Server を補完するのか、 Apache HTTP Server の全機能をどのように管理するのかなど、 実践的な例を以下に示します。

3.4.1. 静的なサイトを実行する

静的な web サイトを作成する場合は、 その web サイトの .html ファイルに httpd_sys_content_t タイプのラベルを付けます。 デフォルトでは、httpd_sys_content_t タイプのラベルが付いたファイルには Apache HTTP Server による書き込みは行なえません。 次のように、読み取り専用 web サイト向けのファイルを格納する新しいディレクトリを作成します。
  1. root ユーザーで mkdir /mywebsite を実行し最上位にディレクトリを作成します。
  2. root ユーザーで /mywebsite/index.html ファイルを作成します。 以下のコンテンツを /mywebsite/index.html にコピーして貼り付けます。
    <html>
    <h2>index.html from /mywebsite/</h2>
    </html>
    
  3. /mywebsite/ および配下のファイルやサブディレクトリへの Apache HTTP Server 読み取り専用アクセスを許可するため、/mywebsite/httpd_sys_content_t タイプのラベルを付けます。root ユーザーで次のコマンドを実行してラベルの変更をファイルコンテキスト設定に追加します。
    # semanage fcontext -a -t httpd_sys_content_t "/mywebsite(/.*)?"
    
  4. root ユーザーで restorecon -R -v /mywebsite を実行してラベルの変更を行ないます。
    # restorecon -R -v /mywebsite
    restorecon reset /mywebsite context unconfined_u:object_r:default_t:s0->system_u:object_r:httpd_sys_content_t:s0
    restorecon reset /mywebsite/index.html context unconfined_u:object_r:default_t:s0->system_u:object_r:httpd_sys_content_t:s0
    
  5. この例の場合、 root ユーザーで /etc/httpd/conf/httpd.conf を編集します。 既存の DocumentRoot オプションをコメントアウトします。 DocumentRoot "/mywebsite" オプションを追加します。 編集結果は以下のようになるはずです。
    #DocumentRoot "/var/www/html"
    DocumentRoot "/mywebsite"
    
  6. root ユーザーで service httpd status を実行し Apache HTTP Server の状態を確認します。 サーバーが停止している場合は、 root ユーザーで service httpd start を実行してサーバーを起動します。 サーバーが実行中の場合は、 root ユーザーで service httpd restart を実行しサーバーの再起動を行ないます (これにより httpd.conf への変更がすべて適用されます)。
  7. web ブラウザで http://localhost/index.html に行きます。 次のように表示されます。
    index.html from /mywebsite/