公開日:2025/8/5 0:00:00

AlmaLinux 8.10

AlmaLinux 8.10 セットアップメモ

Section 4.1 Webサーバー(Apache)構築編 Apache

About

Webアプリケーションを作るために……という事でWebサーバー構築を行っていきます。 ここでは、Apacheを用いたWebサーバーになっていますが、Section 5.1で nginx を用いた場合の方法もあります。

環境は、Section 2.1Section 2.3で作成したvirtualboxの環境で実行しています。

AppStreamのバージョン等を調べる

Section 3.1でも触れたとおり、モジュール管理されているのでバージョン情報等を確認します。

[root@localhost ~]# dnf module list httpd
AlmaLinux 8 - AppStream
Name                  Stream                 Profiles                                  Summary
httpd                 2.4 [d]                common [d], devel, minimal                Apache HTTP Server

ヒント: [d]efault, [e]nabled, [x]disabled, [i]nstalled
[root@localhost ~]#

2.4からバージョンが進んでないような気もしていますが、デフォルトなのでそのまま使えそうです。

バージョンの切り替え

勝手に変わってしまうのも問題になりそうなので、有効化しておきましょう。

[root@localhost ~]# dnf -y module enable httpd:2.4
========================================================================================================================
 パッケージ                  アーキテクチャー           バージョン                    リポジトリー                サイズ
========================================================================================================================
モジュールストリームの有効化中:
 httpd                                                  2.4

トランザクションの概要
========================================================================================================================

完了しました!
[root@localhost ~]# dnf module list httpd
AlmaLinux 8 - AppStream
Name       Stream          Profiles                       Summary
httpd      2.4 [d][e]      common [d], devel, minimal     Apache HTTP Server

ヒント: [d]efault, [e]nabled, [x]disabled, [i]nstalled
[root@localhost ~]# 

インストール

[root@localhost ~]# dnf -y install httpd mod_ssl mod_http2
依存関係が解決しました。
========================================================================================================================
 パッケージ                   Arch          バージョン                                           リポジトリー     サイズ
========================================================================================================================
インストール:
 httpd                        x86_64        2.4.37-65.module_el8.10.0+3997+13648b59.4            appstream        1.4 M
 mod_http2                    x86_64        1.15.7-10.module_el8.10.0+3966+b9483c84.3            appstream        155 k
 mod_ssl                      x86_64        1:2.4.37-65.module_el8.10.0+3997+13648b59.4          appstream        141 k
依存関係のインストール:
 almalinux-logos-httpd        noarch        84.5-1.el8                                           appstream         29 k
 apr                          x86_64        1.6.3-12.el8                                         appstream        128 k
 apr-util                     x86_64        1.6.1-9.el8                                          appstream        105 k
 httpd-filesystem             noarch        2.4.37-65.module_el8.10.0+3997+13648b59.4            appstream         44 k
 httpd-tools                  x86_64        2.4.37-65.module_el8.10.0+3997+13648b59.4            appstream        112 k
 mailcap                      noarch        2.1.48-3.el8                                         baseos            39 k
 sscg                         x86_64        3.0.0-7.el8                                          appstream         51 k
弱い依存関係のインストール:
 apr-util-bdb                 x86_64        1.6.1-9.el8                                          appstream         24 k
 apr-util-openssl             x86_64        1.6.1-9.el8                                          appstream         26 k

トランザクションの概要
========================================================================================================================
インストール  12 パッケージ

(中略)

インストール済み:
  almalinux-logos-httpd-84.5-1.el8.noarch
  apr-1.6.3-12.el8.x86_64
  apr-util-1.6.1-9.el8.x86_64
  apr-util-bdb-1.6.1-9.el8.x86_64
  apr-util-openssl-1.6.1-9.el8.x86_64
  httpd-2.4.37-65.module_el8.10.0+3997+13648b59.4.x86_64
  httpd-filesystem-2.4.37-65.module_el8.10.0+3997+13648b59.4.noarch
  httpd-tools-2.4.37-65.module_el8.10.0+3997+13648b59.4.x86_64
  mailcap-2.1.48-3.el8.noarch
  mod_http2-1.15.7-10.module_el8.10.0+3966+b9483c84.3.x86_64
  mod_ssl-1:2.4.37-65.module_el8.10.0+3997+13648b59.4.x86_64
  sscg-3.0.0-7.el8.x86_64

完了しました!
[root@localhost ~]#

まずはインストールまで。

サービスの登録 と firewall 設定

まずは、サービスを有効化して起動します。

[root@localhost ~]# systemctl enable httpd
Created symlink /etc/systemd/system/multi-user.target.wants/httpd.service → /usr/lib/syst//system/httpd.service.
[root@localhost ~]# systemctl start httpd
[root@localhost ~]# 

このままでは、firewallによってアクセスできないようにされているので、そちらも開放します。

[root@localhost ~]# firewall-cmd --zone=public --add-service=http --permanent
success
[root@localhost ~]# firewall-cmd --zone=public --add-service=https --permanent
success
[root@localhost ~]# systemctl reload firewalld
[root@localhost ~]# firewall-cmd --list-all
public (active)
  target: default
  icmp-block-inversion: no
  interfaces: enp0s3
  sources:
  services: cockpit dhcpv6-client http https ssh
  ports:
  protocols:
  forward: no
  masquerade: no
  forward-ports:
  source-ports:
  icmp-blocks:
  rich rules:
[root@localhost ~]# 

これで、ネットワーク的に問題が無ければブラウザでアクセスできると思います。

設定変更

.htaccess を使うWebアプリケーションが多いので、DocumentRoot(サーバー内の代表的なページを管理するフォルダの事)で使えるように許可します。

変更する設定は、ServerAdmin、ServerName、Direcrory Directive 内の AllowOverride となります。 ServerAdminは、Webサイトへの問い合わせ先となるメールアドレスを入れるのがお作法。ServerNameは公開するときに使うドメインを書いてください。

外部公開まで考えるともっと変えたほうが良い設定もありますが、開発環境レベルであればこれで十分でしょう。

エディタで直すのもあれなのでsedコマンドを使って一気に書き換えます。

[root@localhost ~]# cp /etc/httpd/conf/httpd.conf /etc/httpd/conf/httpd.conf.org
[root@localhost ~]# cat /etc/httpd/conf/httpd.conf.org \
| sed -E "s/^(ServerAdmin )(.*$)/\1webmaster@localhost.example.com/" \
| sed -E "s/#(ServerName .*$)/\1/" \
| sed -E "/# AllowOverride.*/,/AllowOverride None/ s/(AllowOverride) None/\1 All/" \
> /etc/httpd/conf/httpd.conf
[root@localhost ~]# diff /etc/httpd/conf/httpd.conf.org /etc/httpd/conf/httpd.conf

*** /etc/httpd/conf/httpd.conf.org      YYYY-mm-dd hh:ii:ss.000000000 +0900
--- /etc/httpd/conf/httpd.conf          YYYY-mm-dd hh:ii:ss.000000000 +0900
***************
*** 86,92 ****
        # e-mailed.  This address appears on some server-generated pages, such
        # as error documents.  e.g. admin@your-domain.com
        #
!       ServerAdmin root@localhost

        #
        # ServerName gives the name and port that the server uses to identify itself.
--- 86,92 ----
        # e-mailed.  This address appears on some server-generated pages, such
        # as error documents.  e.g. admin@your-domain.com
        #
!       ServerAdmin webmaster@localhost.example.com

        #
        # ServerName gives the name and port that the server uses to identify itself.
***************
*** 95,101 ****
        #
        # If your host doesn't have a registered DNS name, enter its IP address here.
        #
!       #ServerName www.example.com:80

        #
        # Deny access to the entirety of your server's filesystem. You must
--- 95,101 ----
        #
        # If your host doesn't have a registered DNS name, enter its IP address here.
        #
!       ServerName www.example.com:80

        #
        # Deny access to the entirety of your server's filesystem. You must
***************
*** 151,157 ****
            # It can be "All", "None", or any combination of the keywords:
            #   Options FileInfo AuthConfig Limit
            #
!           AllowOverride None

            #
            # Controls who can get stuff from this server.
--- 151,157 ----
            # It can be "All", "None", or any combination of the keywords:
            #   Options FileInfo AuthConfig Limit
            #
!           AllowOverride All

            #
            # Controls who can get stuff from this server.

設定を変えたらサービスを再起動しましょう。

オプションその1 mod_securty

WAF(Web Application Firewall)を、apacheのモジュールをインストールして有効にすることで導入することができます。

[root@localhost ~]# dnf install mod_security mod_security-mlogc mod_security_crs

設定については色々調べてみてください。

なお、インストール後は、自分でサービスを再起動しましょう。

オプションその2 mod_markdown

markdownテキストをアップロードすればhtmlに変換して表示してくれるモジュールがあります。開発では使わないだろう……と言わずに……

[root@localhost ~]# dnf install mod_markdown

設定については色々調べてみてください。

なお、インストール後は、自分でサービスを再起動しましょう。