公開日:2022/8/22 0:00:00

AlmaLinux 8.6

AlmaLinux 8.6 セットアップメモ

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

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

バージョンの切り替え

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

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

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

これでよろしいですか? [y/N]: y
完了しました!
[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 install httpd mod_ssl mod_http2
依存関係が解決しました。
================================================================================
 パッケージ       Arch   バージョン                             Repo      サイズ
================================================================================
インストール:
 httpd            x86_64 2.4.37-43.module_el8.5.0+2631+6f259f31.3.alma
                                                                appstream 1.4 M
 mod_http2        x86_64 1.15.7-3.module_el8.5.0+2609+b30d9eec  appstream 153 k
 mod_ssl          x86_64 1:2.4.37-43.module_el8.5.0+2631+6f259f31.3.alma
                                                                appstream 136 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-6.el8                            appstream 105 k
 httpd-filesystem noarch 2.4.37-43.module_el8.5.0+2631+6f259f31.3.alma
                                                                appstream  39 k
 httpd-tools      x86_64 2.4.37-43.module_el8.5.0+2631+6f259f31.3.alma
                                                                appstream 106 k
 mailcap          noarch 2.1.48-3.el8                           baseos     39 k
 sscg             x86_64 2.3.3-14.el8                           appstream  49 k
弱い依存関係のインストール:
 apr-util-bdb     x86_64 1.6.1-6.el8                            appstream  24 k
 apr-util-openssl x86_64 1.6.1-6.el8                            appstream  27 k

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

ダウンロードサイズの合計: 2.2 M
インストール後のサイズ: 5.8 M
これでよろしいですか? [y/N]: y
(中略)
完了しました!
[root@localhost ~]#

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

サービスの登録 と firewall 設定

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

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

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

[root@localhost ~]# firewall-cmd --zone=public --add-service=http --permanent
[root@localhost ~]# firewall-cmd --zone=public --add-service=https --permanent

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

設定変更

.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
*** /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

        #
        # 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

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

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