このページで分かること
前提
CentOS/RHEL系(Amazon Linux含む)を想定している。Debian/Ubuntu系の場合はパッケージ名が apache2 になり、SELinuxも通常無効なので読み替えること。
いろんなapache構築関連のサイトを見ても載ってなかった躓き。
どんだけ頑張ってもパーミッションを見直しても"500 Internal Server Error"が出る。
SELINUX=disabledに。
# This file controls the state of SELinux on the system.
# SELINUX= can take one of these three values:
# enforcing - SELinux security policy is enforced.
# permissive - SELinux prints warnings instead of enforcing.
# disabled - No SELinux policy is loaded.
#SELINUX=enforcing
SELINUX=disabled
# SELINUXTYPE= can take one of these three values:
# targeted - Targeted processes are protected,
# minimum - Modification of targeted policy. Only selected processes are pr$
# mls - Multi Level Security protection.
SELINUXTYPE=targeted
SELinuxがenforcingのままだと、httpdがドキュメントルート外のファイルを読めなかったり、DBやリバースプロキシ先への通信を拒否されたりして500になる。setenforce 0 で一時的にpermissiveに落とし、直るならSELinuxが原因と断定できる。
/etc/sysconfig/selinux の SELINUX=disabled(要再起動)だ。setsebool -P httpd_can_network_connect 1 や semanage fcontext + restorecon で必要な権限だけ開放できる。リバースプロキシやDB連携で使うならこちらの方が安全だ。インストールはシンプルに。
# yum -y install httpd httpd-tools httpd-devel httpd-manual
最近はchkconfigやserviceを使わずsystemctlらしい。
systemctl enable httpd
systemctl disable httpd
systemctl start httpd
systemctl stop httpd
systemctl status httpd
これもiptablesを使わずにfirewalldらしい。
# firewall-cmd --add-service=http --permanent
success
設定の反映
# firewall-cmd --reload
success
Apacheをリバースプロキシにするのも定番だ。yum -y install mod_ssl でSSLモジュールを入れ、/etc/httpd/conf.d/ に以下のような設定を置く。バックエンド(例: 8080番で動くDockerのWordPressやJenkins)への振り分けになる。
<VirtualHost *:443>
ServerName blog.example.com
ProxyPreserveHost On
ProxyPass / http://127.0.0.1:8080/
ProxyPassReverse / http://127.0.0.1:8080/
</VirtualHost>
-p 8080:80 のように「外側:コンテナ内」の順だ。外側のポートだけ変えて、Apacheからは127.0.0.1:8080へ向ける。setsebool -P httpd_can_network_connect 1 が必要で、これを忘れるとリバースプロキシが「503 Service Unavailable」になる。SSLProxyEngine On も追加する。Q. 500 Internal Server Errorが消えない。
A. まず tail -f /var/log/httpd/error_log でエラー内容を確認する。パーミッションに見えてもSELinuxが原因のことが多いので、setenforce 0 での切り分けが早い。
Q. Debian/Ubuntuで同じことをするには。
A. パッケージは apache2、設定は /etc/apache2/、再起動は systemctl restart apache2 だ。SELinuxは通常無効なので、このページのSELinux手順は読み飛ばしてよい。
Q. Dockerのhttpd公式イメージを使う場合の注意は。
A. httpd:2.4 公式イメージはDebianベースで、設定は /usr/local/apache2/conf/httpd.conf、ドキュメントルートは /usr/local/apache2/htdocs だ。-v でホストの設定・コンテンツをマウントして使う。
関連ページ
※ 本ページのAmazonリンクにはアフィリエイトタグ(toobf6-22)が付いている。
🤖 このページにはAIエージェント(ノア)による編集・加筆が含まれます。内容の正確性は保証できないため、重要な判断は一次情報でご確認ください。