2025-11-25

AlmaLinux 10 で httpd を設定

Apache HTTP Server (httpd) は、Apache License 2.0 の条件でリリースされるフリーでオープンソースのクロスプラットフォームの Web サーバソフトウェアです。Apache は Apache ソフトウェア財団の支援のもと、開発者のオープンコミュニティによって開発・保守されています。

Wikipedia より抜粋、修正

家庭内 LAN で稼働している AlmaLinux 10 のファイルサーバーに Apache の httpd をインストールしてローカル Web サーバーに設定したメモです。

httpd のインストールと設定

Apache HTTP Server (httpd) をインストールおよび設定をする OS は下記のとおりです。

OS AlmaLinux 10.0 (Purple Lion) x86_64v2

パッケージのインストール

Apache HTTP Server httpdhttpd-devel パッケージをインストールします。

Apache HTTP Server のインストール
$ sudo dnf install httpd httpd-devel

サービスの起動

Apache HTTP Server のサービス、httpd を起動 (start) します。また、OS 起動後にサービスが使えるように有効化 (enable) します。

httpd の起動 
$ sudo systemctl start httpd
$ sudo systemctl enable httpd

ファイアーウォールの設定

HTTP で通信できるようにサービスを追加した後、ファイアーウォールの設定を再ロードします。

ファイアーウォールの設定 
$ sudo firewall-cmd --zone=public --permanent --add-service=http
success
$ sudo firewall-cmd --reload
success

ユーザ毎のディレクトリの設定

ユーザアカウント内の public_html 内をウェブサイトとして利用できるように /etc/httpd/conf.d/userdir.conf を編集します。下記に変更した箇所を赤字で示しました。

/etc/httpd/conf.d/userdir.conf の設定 
#
# UserDir: The name of the directory that is appended onto a user's home
# directory if a ~user request is received.
#
# The path to the end user account 'public_html' directory must be
# accessible to the webserver userid.  This usually means that ~userid
# must have permissions of 711, ~userid/public_html must have permissions
# of 755, and documents contained therein must be world-readable.
# Otherwise, the client will only receive a "403 Forbidden" message.
#
<IfModule mod_userdir.c>
    #
    # UserDir is disabled by default since it can confirm the presence
    # of a username on the system (depending on home directory
    # permissions).
    #
    #UserDir disabled

    #
    # To enable requests to /~user/ to serve the user's public_html
    # directory, remove the "UserDir disabled" line above, and uncomment
    # the following line instead:
    # 
    UserDir public_html
</IfModule>

#
# Control access to UserDir directories.  The following is an example
# for a site where these directories are restricted to read-only.
#
<Directory "/home/*/public_html">
    AllowOverride FileInfo AuthConfig Limit Indexes
    Options MultiViews Indexes SymLinksIfOwnerMatch IncludesNoExec
    Require method GET POST OPTIONS
</Directory>

ユーザアカウント内に public_html ディレクトリを作成します。この例ではユーザアカウントは bitwalk のみです。パーミッションは下記のようになっています。

ユーザアカウントのディレクトリとパーミッション 
$ cd ~
$ mkdir public_html
$ chmod 755 /home/bitwalk
$ ll -d /home/bitwalk
drwxr-xr-x. 23 bitwalk bitwalk 4096 11月 25 07:25 /home/bitwalk
$ ll -d /home/bitwalk/public_html
drwxr-xr-x. 2 bitwalk bitwalk 6 11月 25 07:25 /home/bitwalk/public_html
$

httpd を再起動します。

httpd サービスの再起動 
$ sudo systemctl restart httpd

SELinux の設定

最後に SELinux 下で httpd がユーザーディレクトリをアクセスできるように httpd_enable_homedirs を有効にします。

SELinux の httpd_enable_homedirs を有効化
$ sudo setsebool -P httpd_enable_homedirs 1

mod_ssl のインストール

SSL/TLS による HTTPS 通信に対応させるため、下記のように mod_ssl をインストールしておきます。

mod_ssl のインストール 
$ sudo dnf -y install mod_ssl

暗号化された HTTP 通信を行なう標準のポート 443 を利用できるようにファイアウォールを設定します。

ファイアーウォールの設定 (2) 
$ sudo firewall-cmd --permanent --add-port=443/tcp
success
$ sudo firewall-cmd --reload
success

httpd を再起動します。

httpd サービスの再起動 
$ sudo systemctl restart httpd

シンボリックリンクに関して

ユーザーアカウント内で作成した ~/public_html 内にシンボリックリンクを作成して、他の PC のブラウザから見ようとしたところアクセスを拒否されてしまいました。

SELinux のログを確認すると、

SELinux により、/usr/sbin/httpd による read アクセスが、ディレクトリー /home/bitwalk/MyProjects で拒否されました。

というエラーが出ていました。

SELinux の Solutions に従って下記のように httpd_read_user_content を実行したところ、シンボリックリンクしたディレクトリ内をブラウザから閲覧できるようになりました。

SELinux の httpd_read_user_content を有効化 
$ sudo setsebool -P httpd_read_user_content 1

ローカルで動かしているなら SELinux は面倒であれば無効化してしまう、という意見を見かけます。SELinux が普及し始めた古い記事だから、と思いたいのですが…。

いざ SELinux が必要な局面になったら途端に狼狽えてしまうことが判っているので、普段から SELinux アリで設定をしようと心がけています。

せっかく、SELinux でセキュリティの高い OS を実現しているというのに、ローカルだからとか VM で利用しているからという理由を言い訳にして無効にするのはもったいないことです。

参考サイト

  1. Apache HTTP Server Version 2.4 Documentation - Apache HTTP Server Version 2.4

 

ブログランキング・にほんブログ村へ bitWalk's - にほんブログ村 にほんブログ村 IT技術ブログ オープンソースへ
にほんブログ村

オープンソース - ブログ村ハッシュタグ
#オープンソース



このエントリーをはてなブックマークに追加

0 件のコメント: