じゃあ、おうちで学べる

本能を呼び覚ますこのコードに、君は抗えるか

夜は短い だが乙女がいないので Nginx Unit で静的ファイルを取得するまでのログ

これはNginx のお話ではなく普通のCentOSNGINX Unit というミドルウェアをインストールして静的ファイルをいくつかのhosts で設定していくのを寝る前にやっていく今日は書き終えて寝たい。何としても床を得たい。

環境構築

現環境

# cat /etc/redhat-release
CentOS Linux release 8.2.2004 (Core)

インストール

nginx Unit のCentOS用の手順 を参考にしながら設定を行う

/etc/yum.repos.d/unit.repo の設定

/etc/yum.repos.d/unit.repo に下記の設定を入れる

[unit]
name=unit repo
baseurl=https://packages.nginx.org/unit/centos/$releasever/$basearch/
gpgcheck=0
enabled=1

パッケージインストール

Unit パッケージと使用するその他のパッケージをインストールします

# yum install unit -y
# yum install unit-devel unit-go unit-jsc8 unit-jsc11 \
      unit-perl unit-php unit-python27 unit-python36 -y

確認 及び起動と有効化

インストールされているか各種確認を行い、設定の諸々の有効化を行います。あと、起動コマンドがあるのであれば -h もしくはそれらに準ずるオプションは絶対に入力をしましょう。人は救われることが大いにあります。

# which unitd
/usr/sbin/unitd
# unitd --version
unit version: 1.19.0
configured as ./configure --prefix=/usr --state=/var/lib/unit --control=unix:/var/run/unit/control.sock --pid=/var/run/unit/unit.pid --log=/var/log/unit/unit.log --tmp=/var/tmp --tests --openssl --modules=/usr/lib64/unit/modules --libdir=/usr/lib64 --cc-opt='-O2 -g -pipe -Wall -Werror=format-security -Wp,-D_FORTIFY_SOURCE=2 -Wp,-D_GLIBCXX_ASSERTIONS -fexceptions -fstack-protector-strong -grecord-gcc-switches -specs=/usr/lib/rpm/redhat/redhat-hardened-cc1 -specs=/usr/lib/rpm/redhat/redhat-annobin-cc1 -m64 -mtune=generic -fasynchronous-unwind-tables -fstack-clash-protection -fcf-protection -fPIC' --ld-opt='-Wl,-z,relro -Wl,-z,now -pie'

人を救う-h での確認

#  unitd -h

unit options:

  --version            print unit version and configure options

  --no-daemon          run unit in non-daemon mode

  --control ADDRESS    set address of control API socket
                       default: "unix:/var/run/unit/control.sock"

  --pid FILE           set pid filename
                       default: "/var/run/unit/unit.pid"

  --log FILE           set log filename
                       default: "/var/log/unit/unit.log"

  --modules DIRECTORY  set modules directory name
                       default: "/usr/lib64/unit/modules"

  --state DIRECTORY    set state directory name
                       default: "/var/lib/unit"

  --tmp DIRECTORY      set tmp directory name
                       default: "/var/tmp"

  --user USER          set non-privileged processes to run as specified user
                       default: "nobody"

  --group GROUP        set non-privileged processes to run as specified group
                       default: user's primary group

有効化及びスタートしていきます

# systemctl enable unit
Created symlink /etc/systemd/system/multi-user.target.wants/unit.service → /usr/lib/systemd/system/unit.service.
# systemctl start unit
# ps auxf | grep [u]nit
root       27237  0.0  0.3  65968  5620 ?        Ss   23:02   0:00 unit: main v1.19.0 [/usr/sbin/unitd --log /var/log/unit/unit.log --pid /var/run/unit/unit.pid --no-daemon]
nobody     27239  0.0  0.0  65968   664 ?        S    23:02   0:00  \_ unit: controller
nobody     27240  0.0  0.1  66108  2636 ?        S    23:02   0:00  \_ unit: router
ll /var/run/unit/control.sock
srw------- 1 root root 0  916 23:02 /var/run/unit/control.sock

ログの確認

cat /var/log/unit/unit.log
2020/09/16 23:02:40 [info] 27238#27238 discovery started
2020/09/16 23:02:40 [notice] 27238#27238 module: java 11.0.6 "/usr/lib64/unit/modules/java11.unit.so"
2020/09/16 23:02:40 [notice] 27238#27238 module: java 1.8.0_242 "/usr/lib64/unit/modules/java8.unit.so"
2020/09/16 23:02:40 [notice] 27238#27238 module: perl 5.26.3 "/usr/lib64/unit/modules/perl.unit.so"
2020/09/16 23:02:40 [notice] 27238#27238 module: php 7.2.11 "/usr/lib64/unit/modules/php.unit.so"
2020/09/16 23:02:40 [notice] 27238#27238 module: python 2.7.16 "/usr/lib64/unit/modules/python2.7.unit.so"
2020/09/16 23:02:40 [notice] 27238#27238 module: python 3.6.8 "/usr/lib64/unit/modules/python3.6.unit.so"
2020/09/16 23:02:40 [info] 27237#27237 controller started
2020/09/16 23:02:40 [notice] 27237#27237 process 27238 exited with code 0
2020/09/16 23:02:40 [info] 27240#27240 router started
2020/09/16 23:02:40 [info] 27240#27240 OpenSSL 1.1.1c FIPS  28 May 2019, 1010103f

Quick Start

Quick Start を見ながら最初のアプリケーションをデプロイしていく

# cat << EOF > config.json

    {
        "type": "php",
        "root": "/www/blogs/scripts"
    }
EOF
# curl -X PUT --data-binary @config.json --unix-socket /var/run/unit/control.sock http://localhost/config/applications/blogs
{
        "error": "Failed to apply new configuration."
}

かなCのでログを見る。

2020/09/16 23:22:03 [alert] 27309#27309 root realpath(/www/blogs/scripts) failed (2: No such file or directory)
2020/09/16 23:22:03 [notice] 27237#27237 process 27309 exited with code 1
2020/09/16 23:22:03 [warn] 27240#27240 failed to start application "blogs"
2020/09/16 23:22:03 [alert] 27240#27240 failed to apply new conf

あ、ディレクトリがありませんっておしゃってますね。作って再実行する。

# mkdir -p /www/blogs/scripts
# curl -X PUT --data-binary @config.json --unix-socket /var/run/unit/control.sock http://localhost/config/applications/blogs
{
        "success": "Reconfiguration done."
}

次は成功した。ドキュメントへの過度な信頼はいつなくなったんだろう...。眠いので感傷的になっているかもしれないですが下記のコマンドで確認できます。

# curl -XGET --unix-socket /var/run/unit/control.sock http://localhost/config/
{
        "listeners": {},
        "applications": {
                "blogs": {
                        "type": "php",
                        "root": "/www/blogs/scripts"
                }
        }
}
# curl -XDELETE --unix-socket /var/run/unit/control.sock http://localhost/config/
{
        "success": "Reconfiguration done."
}
# curl -XGET --unix-socket /var/run/unit/control.sock http://localhost/config/
{}

各種メゾットがちゃんと役割を果たしていて個人的には非常に嬉しいです。ほかに言うことはありません。 適当にアプリなど配置すれば確認できるのでしょうが今回はターゲットではないので設定の投入だけ確認できたので終わり

静的なファイルの配置

mkdir -p /www/data/static/ とかでファイルを作って index.html を直下に配置する。

<!doctype html>
<html>
  <head>
    <title>This is the title of the webpage!</title>
  </head>
  <body>
    <p>welcome to Nginx Unit </p>
  </body>
</html>
# cat << EOF > config.json

{
    "listeners": {
        "127.0.0.1:8300": {
            "pass": "routes"
        }
     },

    "routes": [
        {
            "action": {
                "share": "/www/data/static/"
             }
        }
    ]
}
EOF

設定ファイルの設定を行い確認する。

# curl -X PUT --data-binary @config.json --unix-socket /var/run/unit/control.sock http://localhost/config/
{
        "success": "Reconfiguration done."
}
]# curl -XGET --unix-socket /var/run/unit/control.sock http://localhost/config/
{
        "listeners": {
                "127.0.0.1:8300": {
                        "pass": "routes"
                }
        },

        "routes": [
                {
                        "action": {
                                "share": "/www/data/static/"
                        }
                }
        ]
}

curl を行う

ポートが解放されているか確認する

# netstat -nltp | grep [u]nit
tcp        0      0 127.0.0.1:8300          0.0.0.0:*               LISTEN      27240/unit: router

実際に公開したものを取得してみる...

# curl 127.0.0.1:8300/index.html
<!doctype html>
<html>
  <head>
    <title>This is the title of the webpage!</title>
  </head>
  <body>
    <p>welcome to Nginx Unit </p>
  </body>
</html>

一旦、検証のブログとしては完成しました(完成したんです)。本当は図などがあればいいのですがよいです。