CentOS 7にNginx 1.16をインストール（SCL）

# はじめに

Software Collection(SCL)を利用してCentOS7にNginx1.16をインストール
親記事：[Nginxの各種インストール方法とEOLまとめ](https://qiita.com/witchcraze/items/df4a0baaaad9c932ea30)
参考：[Quick Start — Software Collections](https://www.softwarecollections.org/en/docs/)

## サポート

本手法で導入した場合、[Red Hat Software Collections Product Life Cycle - Red Hat Customer Portal](https://access.redhat.com/support/policy/updates/rhscl)より、2021-12がEOLだと思われる。
それ以降に報告された脆弱性や不具合への対応は実施されない可能性がある。

## LOG

### レポジトリ登録

```shell-session
# yum install -y centos-release-scl
```

### インストール

CentOS7.7だと起動しなかったので7.6を使用しています。
[Systemd error: 'Refusing to accept PID outside of service control group, acquired through unsafe symlink chain' \- Red Hat Customer Portal](https://access.redhat.com/solutions/4410831) が影響しているかもしれないけど未調査。

```shell-session
# cat /etc/redhat-release
CentOS Linux release 7.6.1810 (Core)

# yum install -y rh-nginx116 which
# scl enable rh-nginx116 bash
... 略
```

### nginx起動/停止

```shell-session
# systemctl start rh-nginx116-nginx
# systemctl status -l rh-nginx116-nginx
● rh-nginx116-nginx.service - The nginx HTTP and reverse proxy server
   Loaded: loaded (/usr/lib/systemd/system/rh-nginx116-nginx.service; disabled; vendor preset: disabled)
   Active: active (running) since Thu 2020-01-30 03:30:15 UTC; 5s ago
  Process: 1431 ExecStart=/opt/rh/rh-nginx116/root/usr/libexec/nginx-scl-helper enable $RH_NGINX116_SCLS_ENABLED -- /opt/rh/rh-nginx116/root/usr/sbin/nginx (code=exited, status=0/SUCCESS)
  Process: 1425 ExecStartPre=/opt/rh/rh-nginx116/root/usr/libexec/nginx-scl-helper enable $RH_NGINX116_SCLS_ENABLED -- /opt/rh/rh-nginx116/root/usr/sbin/nginx -t (code=exited, status=0/SUCCESS)
  Process: 1418 ExecStartPre=/opt/rh/rh-nginx116/root/usr/libexec/nginx-scl-helper enable $RH_NGINX116_SCLS_ENABLED -- /usr/bin/scl_enabled rh-nginx116 (code=exited, status=0/SUCCESS)
  Process: 1417 ExecStartPre=/usr/bin/rm -f /var/opt/rh/rh-nginx116/run/nginx/nginx.pid (code=exited, status=0/SUCCESS)
 Main PID: 1437 (nginx)
   CGroup: /docker/285f4a924c86a08e019904bd193ea56b61ec9cf87e8fa795e3855778d20f6f4d/system.slice/rh-nginx116-nginx.service
           tq1437 nginx: master process /opt/rh/rh-nginx116/root/usr/sbin/nginx
           mq1438 nginx: worker process
           ? 1437 nginx: master process /opt/rh/rh-nginx116/root/usr/sbin/nginx

Jan 30 03:30:15 285f4a924c86 systemd[1]: Starting The nginx HTTP and reverse proxy server...
Jan 30 03:30:15 285f4a924c86 nginx-scl-helper[1425]: nginx: the configuration file /etc/opt/rh/rh-nginx116/nginx/nginx.conf syntax is ok
Jan 30 03:30:15 285f4a924c86 nginx-scl-helper[1425]: nginx: configuration file /etc/opt/rh/rh-nginx116/nginx/nginx.conf test is successful
Jan 30 03:30:15 285f4a924c86 systemd[1]: Failed to read PID from file /var/opt/rh/rh-nginx116/run/nginx/nginx.pid: Invalid argument
Jan 30 03:30:15 285f4a924c86 systemd[1]: Started The nginx HTTP and reverse proxy server.
```

```shell-session
# systemctl stop rh-nginx116-nginx
# systemctl status -l rh-nginx116-nginx
● rh-nginx116-nginx.service - The nginx HTTP and reverse proxy server
   Loaded: loaded (/usr/lib/systemd/system/rh-nginx116-nginx.service; disabled; vendor preset: disabled)
   Active: inactive (dead)

Jan 30 03:30:15 285f4a924c86 systemd[1]: Starting The nginx HTTP and reverse proxy server...
Jan 30 03:30:15 285f4a924c86 nginx-scl-helper[1425]: nginx: the configuration file /etc/opt/rh/rh-nginx116/nginx/nginx.conf syntax is ok
Jan 30 03:30:15 285f4a924c86 nginx-scl-helper[1425]: nginx: configuration file /etc/opt/rh/rh-nginx116/nginx/nginx.conf test is successful
Jan 30 03:30:15 285f4a924c86 systemd[1]: Failed to read PID from file /var/opt/rh/rh-nginx116/run/nginx/nginx.pid: Invalid argument
Jan 30 03:30:15 285f4a924c86 systemd[1]: Started The nginx HTTP and reverse proxy server.
Jan 30 03:30:46 285f4a924c86 systemd[1]: Stopping The nginx HTTP and reverse proxy server...
Jan 30 03:30:46 285f4a924c86 systemd[1]: Stopped The nginx HTTP and reverse proxy server.
```

### CentOS7.7で発生したエラー

```shell-session
# systemctl start rh-nginx116-nginx
Job for rh-nginx116-nginx.service failed because a timeout was exceeded. See "systemctl status rh-nginx116-nginx.service" and "journalctl -xe" for details.
# systemctl status -l rh-nginx116-nginx.service
● rh-nginx116-nginx.service - The nginx HTTP and reverse proxy server
   Loaded: loaded (/usr/lib/systemd/system/rh-nginx116-nginx.service; disabled; vendor preset: disabled)
   Active: failed (Result: timeout) since Thu 2020-01-30 03:37:24 UTC; 31s ago
  Process: 1744 ExecStart=/opt/rh/rh-nginx116/root/usr/libexec/nginx-scl-helper enable $RH_NGINX116_SCLS_ENABLED -- /opt/rh/rh-nginx116/root/usr/sbin/nginx (code=exited, status=0/SUCCESS)
  Process: 1738 ExecStartPre=/opt/rh/rh-nginx116/root/usr/libexec/nginx-scl-helper enable $RH_NGINX116_SCLS_ENABLED -- /opt/rh/rh-nginx116/root/usr/sbin/nginx -t (code=exited, status=0/SUCCESS)
  Process: 1731 ExecStartPre=/opt/rh/rh-nginx116/root/usr/libexec/nginx-scl-helper enable $RH_NGINX116_SCLS_ENABLED -- /usr/bin/scl_enabled rh-nginx116 (code=exited, status=0/SUCCESS)
  Process: 1730 ExecStartPre=/usr/bin/rm -f /var/opt/rh/rh-nginx116/run/nginx/nginx.pid (code=exited, status=0/SUCCESS)
   CGroup: /docker/285f4a924c86a08e019904bd193ea56b61ec9cf87e8fa795e3855778d20f6f4d/docker/285f4a924c86a08e019904bd193ea56b61ec9cf87e8fa795e3855778d20f6f4d/system.slice/rh-nginx116-nginx.service
           tq1750 nginx: master process /opt/rh/rh-nginx116/root/usr/sbin/nginx
           mq1751 nginx: worker process

Jan 30 03:35:54 285f4a924c86 systemd[1]: Starting The nginx HTTP and reverse proxy server...
Jan 30 03:35:54 285f4a924c86 nginx-scl-helper[1738]: nginx: the configuration file /etc/opt/rh/rh-nginx116/nginx/nginx.conf syntax is ok
Jan 30 03:35:54 285f4a924c86 nginx-scl-helper[1738]: nginx: configuration file /etc/opt/rh/rh-nginx116/nginx/nginx.conf test is successful
Jan 30 03:35:54 285f4a924c86 systemd[1]: Failed to parse PID from file /var/opt/rh/rh-nginx116/run/nginx/nginx.pid: Invalid argument
Jan 30 03:35:54 285f4a924c86 systemd[1]: Refusing to accept PID outside of service control group, acquired through unsafe symlink chain: /var/opt/rh/rh-nginx116/run/nginx/nginx.pid
Jan 30 03:37:24 285f4a924c86 systemd[1]: rh-nginx116-nginx.service start operation timed out. Terminating.
Jan 30 03:37:24 285f4a924c86 systemd[1]: Failed to start The nginx HTTP and reverse proxy server.
Jan 30 03:37:24 285f4a924c86 systemd[1]: Unit rh-nginx116-nginx.service entered failed state.
Jan 30 03:37:24 285f4a924c86 systemd[1]: rh-nginx116-nginx.service failed.
```

### nginx自動起動設定/設定解除

```shell-session
# systemctl enable rh-nginx116-nginx
Created symlink from /etc/systemd/system/multi-user.target.wants/rh-nginx116-nginx.service to /usr/lib/systemd/system/rh-nginx116-nginx.service.

# systemctl list-unit-files --type=service |grep nginx
rh-nginx116-nginx.service              enabled
```

```shell-session
# systemctl disable rh-nginx116-nginx
Removed symlink /etc/systemd/system/multi-user.target.wants/rh-nginx116-nginx.service.

# systemctl list-unit-files --type=service |grep nginx
rh-nginx116-nginx.service              disabled
```

### 各種確認

```shell-session
# which nginx
/opt/rh/rh-nginx116/root/usr/sbin/nginx

# nginx -v
nginx version: nginx/1.16.1

# yum info rh-nginx116
Loaded plugins: fastestmirror, ovl
Loading mirror speeds from cached hostfile
 * base: ftp.riken.jp
 * centos-sclo-rh: ftp.riken.jp
 * centos-sclo-sclo: ftp.riken.jp
 * extras: ftp.riken.jp
 * updates: ftp.riken.jp
Installed Packages
Name        : rh-nginx116
Arch        : x86_64
Version     : 1.16
Release     : 1.el7
Size        : 0.0
Repo        : installed
From repo   : centos-sclo-rh
Summary     : Package that installs rh-nginx116
License     : GPLv2+
Description : This is the main package for rh-nginx116 Software Collection.
```