公開日:2023/1/8 0:00:00

AlmaLinux 8.7

AlmaLinux 8.7 セットアップメモ

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

ヒント: [d]efault, [e]nabled, [x]disabled, [i]nstalled

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

バージョンの切り替え

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

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

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

これでよろしいですか? [y/N]: y
完了しました!
[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 [e]                     common [d]                     nginx webserver

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

インストール

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

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

ダウンロードサイズの合計: 646 k
インストール後のサイズ: 1.7 M
これでよろしいですか? [y/N]: y
パッケージのダウンロード:
(中略)

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

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

サービスの登録 と firewall 設定

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

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

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

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

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

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の変更と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」が表示されるだけだと思います。 まずは設定完了という事で。