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

AlmaLinux 9.2 セットアップメモ

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

About

Webアプリケーションを作るために……という事でWebサーバー構築を行っていきます。 ここでは、nginx(https://nginx.org/)を用いたWebサーバーになっています。

環境は、Section 2.1Section 2.3で作成したvirtualboxの環境で実行しています。 また、Section 3.1 でインストールしたPostgreSQLが入った状態とします。

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

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

[root@localhost ~]# dnf module list nginx
AlmaLinux 9 - AppStream
Name                       Stream                     Profiles                       Summary
nginx                      1.22                       common [d]                     nginx webserver

Hint: [d]efault, [e]nabled, [x]disabled, [i]nstalled

一つしかないので、それを使います。

バージョンの切り替え

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

[root@localhost ~]# dnf module enable nginx:1.22
Dependencies resolved.
========================================================================================================================
 Package                     Architecture               Version                       Repository                   Size
========================================================================================================================
Enabling module streams:
 nginx                                                  1.22

Transaction Summary
========================================================================================================================

Is this ok [y/N]: y
Complete!
[root@localhost ~]# dnf module list nginx
AlmaLinux 9 - AppStream
Name                      Stream                       Profiles                       Summary
nginx                     1.22 [e]                     common [d]                     nginx webserver

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

インストール

[root@localhost ~]# dnf install nginx
Dependencies resolved.
========================================================================================================================
 Package                      Architecture  Version                                              Repository        Size
========================================================================================================================
Installing:
 nginx                        x86_64        1:1.22.1-3.module_el9.2.0+20+2a475c1b.alma           appstream         39 k
Installing dependencies:
 almalinux-logos-httpd        noarch        90.5.1-1.1.el9                                       appstream         18 k
 nginx-core                   x86_64        1:1.22.1-3.module_el9.2.0+20+2a475c1b.alma           appstream        573 k
 nginx-filesystem             noarch        1:1.22.1-3.module_el9.2.0+20+2a475c1b.alma           appstream         11 k

Transaction Summary
========================================================================================================================
Install  4 Packages

Total download size: 640 k
Installed size: 1.8 M
Is this ok [y/N]: y
Downloading Packages:
(1/4): almalinux-logos-httpd-90.5.1-1.1.el9.noarch.rpm                                  3.5 kB/s |  18 kB     00:05
(2/4): nginx-1.22.1-3.module_el9.2.0+20+2a475c1b.alma.x86_64.rpm                        7.5 kB/s |  39 kB     00:05
(3/4): nginx-core-1.22.1-3.module_el9.2.0+20+2a475c1b.alma.x86_64.rpm                   111 kB/s | 573 kB     00:05
(4/4): nginx-filesystem-1.22.1-3.module_el9.2.0+20+2a475c1b.alma.noarch.rpm              53 kB/s |  11 kB     00:00
------------------------------------------------------------------------------------------------------------------------
Total                                                                                    98 kB/s | 640 kB     00:06
Running transaction check
Transaction check succeeded.
Running transaction test
Transaction test succeeded.
Running transaction
  Preparing        :                                                                                                1/1
  Running scriptlet: nginx-filesystem-1:1.22.1-3.module_el9.2.0+20+2a475c1b.alma.noarch                             1/4
  Installing       : nginx-filesystem-1:1.22.1-3.module_el9.2.0+20+2a475c1b.alma.noarch                             1/4
  Installing       : nginx-core-1:1.22.1-3.module_el9.2.0+20+2a475c1b.alma.x86_64                                   2/4
  Installing       : almalinux-logos-httpd-90.5.1-1.1.el9.noarch                                                    3/4
  Installing       : nginx-1:1.22.1-3.module_el9.2.0+20+2a475c1b.alma.x86_64                                        4/4
  Running scriptlet: nginx-1:1.22.1-3.module_el9.2.0+20+2a475c1b.alma.x86_64                                        4/4
  Verifying        : almalinux-logos-httpd-90.5.1-1.1.el9.noarch                                                    1/4
  Verifying        : nginx-1:1.22.1-3.module_el9.2.0+20+2a475c1b.alma.x86_64                                        2/4
  Verifying        : nginx-core-1:1.22.1-3.module_el9.2.0+20+2a475c1b.alma.x86_64                                   3/4
  Verifying        : nginx-filesystem-1:1.22.1-3.module_el9.2.0+20+2a475c1b.alma.noarch                             4/4

Installed:
  almalinux-logos-httpd-90.5.1-1.1.el9.noarch
  nginx-1:1.22.1-3.module_el9.2.0+20+2a475c1b.alma.x86_64
  nginx-core-1:1.22.1-3.module_el9.2.0+20+2a475c1b.alma.x86_64
  nginx-filesystem-1:1.22.1-3.module_el9.2.0+20+2a475c1b.alma.noarch

Complete!
[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
success
[root@localhost ~]# firewall-cmd --zone=public --add-service=https --permanent
success
[root@localhost ~]#

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

document root を変更する

document root は /usr/share/nginx/html になっていますが、FHSを知っていたりすると、いろいろ思うところがあるので、apache を使う場合と同様に /var/www/html にファイルを置くように変更します。

FHS (Filesystem Hierarchy Standar) とは

Filesystem Hierarchy Standar(FHS)は、Linuxを含むUnix系OSでの主なディレクトリとその内容を定めたものです。

Filesystem Hierarchy Standard - Wikipedia

RHEL(Redhat Enterprise Linux)も、おおむねそれに準拠しているのでそれに沿ってファイルを配置したほうが良いということになります。 FHS的には /usr 下はあまり変わらないもの。内容がちょいちょい変わるようなら /var 下という感じになっています。Webページが固定されることはないということを考えれば、/var 下だろうということで、そうしたいということになります。

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

[root@localhost ~]# cp --preserve=context /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
***************
*** 38,55 ****
                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 = /404.html {
!               }

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

        # Settings for a TLS enabled server.
--- 38,55 ----
                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 = /404.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

このままだと、SELinuxコンテキストが異なり、パーミッションエラーになってしまうので、それも変更します。

コピーやディレクトリ作成しただけだと、var_t になってしまいます。これを Webサービスから変えようと思うと、httpd_sys_content_t にする必要があります。変更はいろいろな手段がありますが、もともとOS設定で /var/www のコンテキストは登録済みなので、restorecon を使用して復元するという方法で変更します。ついでに、他とユーザー部分が異なるのも気持ちが悪いので合わせます。

[root@localhost ~]# ls -laZ /var/www/
total 4
drwxr-xr-x.  3 root  root  unconfined_u:object_r:var_t:s0   18 Aug  9 15:13 .
drwxr-xr-x. 20 root  root  system_u:object_r:var_t:s0     4096 Aug  9 15:13 ..
drwxr-xr-x.  2 nginx nginx unconfined_u:object_r:var_t:s0    6 Aug  9 15:13 html
[root@localhost ~]# restorecon -v -r /var/www
Relabeled /var/www from unconfined_u:object_r:var_t:s0 to unconfined_u:object_r:httpd_sys_content_t:s0
Relabeled /var/www/html from unconfined_u:object_r:var_t:s0 to unconfined_u:object_r:httpd_sys_content_t:s0
[root@localhost ~]# chcon -R -v -u system_u /var/www
changing security context of '/var/www/html'
changing security context of '/var/www'
[root@localhost ~]# ls -laZ /var/www
total 4
drwxr-xr-x.  3 root  root  system_u:object_r:httpd_sys_content_t:s0   18 Aug  9 15:13 .
drwxr-xr-x. 20 root  root  system_u:object_r:var_t:s0               4096 Aug  9 15:13 ..
drwxr-xr-x.  2 nginx nginx system_u:object_r:httpd_sys_content_t:s0    6 Aug  9 15:13 html
[root@localhost ~]#

まずはフォルダ移動までで確認していきましょう。

サービス再起動

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

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

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

コンテンツ配置とSELinux

よくハマる事例を少し試してみましょう。

まずは、 /home/root に適当なファイルを二つ作成します。

[root@localhost ~]# echo 'COPY' > ~/copy_file.html
[root@localhost ~]# echo 'MOVE' > ~/move_file.html

次にファイル名通り、それぞれのファイルを copy と mv で /var/www/html に持っていきます。

[root@localhost ~]# cp ~/copy_file.html /var/www/html/
[root@localhost ~]# mv ~/move_file.html /var/www/html/

どうなっているか確認してみましょう。

[root@localhost ~]# ls -laZ /var/www/html/
total 8
drwxr-xr-x. 2 nginx nginx system_u:object_r:httpd_sys_content_t:s0     50 Aug  9 17:56 .
drwxr-xr-x. 3 root  root  system_u:object_r:httpd_sys_content_t:s0     18 Aug  9 15:13 ..
-rw-r--r--. 1 root  root  unconfined_u:object_r:httpd_sys_content_t:s0  5 Aug  9 17:55 copy_file.html
-rw-r--r--. 1 root  root  unconfined_u:object_r:admin_home_t:s0         5 Aug  9 17:54 move_file.html
[root@localhost ~]#

SELinuxコンテキストのタイプがそれぞれ異なっていることが分かると思います。それぞれのファイルに対してブラウザなどからアクセスしてみましょう。

おそらく、move_file.html のほうは、403 Forbidden になってしまうと思います。この辺りがSELinuxのパーミッション問題です。コピーならディレクトリに設定されているコンテキストが引き継がれますが、移動になると元のものが使われてしまうという問題がでます。

こうなってしまった場合は、restorecon を使用してコンテキストをOS標準の状態に復元するという方法で変更するのが簡単な方法で正しいやり方となります。

[root@localhost ~]# restorecon -v -r /var/www/html
Relabeled /var/www/html/move_file.html from unconfined_u:object_r:admin_home_t:s0 to unconfined_u:object_r:httpd_sys_content_t:s0
[root@localhost ~]#

tar等で圧縮したファイル群を、適当な src あたりで展開して、それを移動させる……という運用があると思いますが、こういう罠が待っているので、基本的には移動は、コピーして元のファイルを削除するという運用に切り替えると何かと幸せです。

なお、FTP/SFTPでアップロードする場合はコピーと同じ効果を発揮するので、気にしなくて済みます。