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

AlmaLinux 8.10

AlmaLinux 8.10 セットアップメモ

Section 5.1 Webサーバー(nginx)構築編 nginx

About

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

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

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

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

[root@localhost ~]# dnf module list nginx
AlmaLinux 8 - AppStream
Name                      Stream                       Profiles                       Summary
nginx                     1.14 [d]                     common [d]                     nginx webserver
nginx                     1.16                         common [d]                     nginx webserver
nginx                     1.18                         common [d]                     nginx webserver
nginx                     1.20                         common [d]                     nginx webserver
nginx                     1.22                         common [d]                     nginx webserver
nginx                     1.24                         common [d]                     nginx webserver

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

最新を使う場合は mainline を選択することになりますが、現時点(2025年08月)ではstable versionがmodule内にないので、1.24 を選択しようと思います。

バージョンの切り替え

有効にするバージョンを切り替えます。

[root@localhost ~]# dnf -y module enable nginx:1.24
依存関係が解決しました。
========================================================================================================================
 パッケージ                  アーキテクチャー           バージョン                    リポジトリー                サイズ
========================================================================================================================
モジュールストリームの有効化中:
 nginx                                                  1.24

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

完了しました!
[root@localhost ~]# dnf module list nginx
AlmaLinux 8 - AppStream
Name                      Stream                       Profiles                       Summary
nginx                     1.14 [d]                     common [d]                     nginx webserver
nginx                     1.16                         common [d]                     nginx webserver
nginx                     1.18                         common [d]                     nginx webserver
nginx                     1.20                         common [d]                     nginx webserver
nginx                     1.22                         common [d]                     nginx webserver
nginx                     1.24 [e]                     common [d]                     nginx webserver

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

インストール

[root@localhost ~]# dnf -y install nginx
依存関係が解決しました。
依存関係が解決しました。
========================================================================================================================
 パッケージ                  Arch         バージョン                                              リポジトリー    サイズ
========================================================================================================================
インストール:
 nginx                       x86_64       1:1.24.0-1.module_el8.10.0+3795+fdb6ce5a.alma.1         appstream       599 k
依存関係のインストール:
 almalinux-logos-httpd       noarch       84.5-1.el8                                              appstream        29 k
 nginx-filesystem            noarch       1:1.24.0-1.module_el8.10.0+3795+fdb6ce5a.alma.1         appstream        25 k

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

(中略)

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

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

サービスの登録 と firewall 設定

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

[root@localhost ~]# systemctl enable nginx
Created symlink /etc/systemd/system/multi-user.target.wants/nginx.service → /usr/lib/syst//system/nginx.service.
[root@localhost ~]# systemctl start nginx
[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 ~]# 

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

document root を変更する

apache を使っていると、/var/www/html にファイルを置くことが基本設定となっているので、それに合わせておきたいと思います。

nginx.conf をsedで編集します。

[root@localhost ~]# cp /etc/nginx/nginx.conf /etc/nginx/nginx.conf.org
[root@localhost ~]# cat /etc/nginx/nginx.conf.org \
| sed -E "s/(^[ ]+root[ ]+)\/usr.*$/\1\/var\/www\/html;/" \
| sed -E "s/(^[ ]+error_page .*$)/#\1/" \
| sed -E "/(^[ ]+location =[^\{]+)/,/^[ ]+\}/s/(^[ ]+.*$)/#\1/" \
> /etc/nginx/nginx.conf
[root@localhost ~]#

rootの変更とerror_pageの設定をコメントアウトしたので、diff で確認します。

[root@localhost ~]# diff -cT /etc/nginx/nginx.conf.org /etc/nginx/nginx.conf
*** /etc/nginx/nginx.conf.org    YYYY-mm-dd hh:ii:ss.000000000 +0900
--- /etc/nginx/nginx.conf        YYYY-mm-dd hh:ii:ss.000000000 +0900
***************
*** 39,56 ****
                listen       80;
                listen       [::]:80;
                server_name  _;
!               root         /usr/share/nginx/html;

                # Load configuration files for the default server block.
                include /etc/nginx/default.d/*.conf;

!               error_page 404 /404.html;
!                   location = /40x.html {
!               }

!               error_page 500 502 503 504 /50x.html;
!                   location = /50x.html {
!               }
            }

        # Settings for a TLS enabled server.
--- 39,56 ----
                listen       80;
                listen       [::]:80;
                server_name  _;
!               root         /var/www/html;

                # Load configuration files for the default server block.
                include /etc/nginx/default.d/*.conf;

!       #        error_page 404 /404.html;
!       #            location = /40x.html {
!       #        }

!       #        error_page 500 502 503 504 /50x.html;
!       #            location = /50x.html {
!       #        }
            }

        # Settings for a TLS enabled server.

フォルダの作成

フォルダが存在していなければ作成します。ある場合は権限だけ変更しましょう。

[root@localhost ~]# mkdir -p /var/www/html
[root@localhost ~]# chown nginx:nginx /var/www/html

サービス再起動

サービスを再起動してブラウザで確認します。

[root@localhost ~]# systemctl stop nginx
[root@localhost ~]# systemctl start nginx

/var/www/html の中身が空っぽなので、よく見るかもしれない「403 Forbidden」が表示されるだけだと思います。 まずは設定完了という事で。