NGINXのセットアップ

1. 事前確認

ライセンスファイルが配置されていることを確認してください。 ファイルが配置されていない場合、トライアルを申請し証明書と鍵を取得してください

## cd ~/
ls ~/nginx-repo.*

NGINXは Ansible Playbookの設定サンプルをGitHubで公開しています。ファイルを取得します。

## cd ~/
git clone https://github.com/BeF5/f5j-nginx-ansible-lab.git
cd ~/f5j-nginx-ansible-lab

Inventoryの情報を以下コマンドで確認します

## cd ~/f5j-nginx-ansible-lab
ansible-inventory -i inventories/hosts --graph
実行結果サンプル
1@all:
2  |--@nginx1:
3  |  |--ansible_host:10.1.1.7
4  |--@nginx2:
5  |  |--ansible_host:10.1.1.6
6  |--@ungrouped:

nginx1nginx2 という2つのホストが登録されていることが確認できます

2. NGINX Plus、NAP WAF/DoSのインストール

Playbookの内容を確認します

## cd ~/f5j-nginx-ansible-lab
cat playbook/deploy-nginx-plus-app-protect-waf-dos.yaml
実行結果サンプル
 1---
 2- hosts: all
 3  collections:
 4    - nginxinc.nginx_core
 5  tasks:
 6    - name: Install NGINX Plus
 7      ansible.builtin.include_role:
 8        name: nginx
 9      vars:
10        nginx_type: plus
11        nginx_license:
12          certificate: ~/nginx-repo.crt
13          key: ~/nginx-repo.key
14        nginx_remove_license: false
15
16    - name: Install NGINX App Protect WAF/DoS
17      ansible.builtin.include_role:
18        name: nginx_app_protect
19      vars:
20        nginx_app_protect_waf_enable: true
21        nginx_app_protect_dos_enable: true
22        nginx_app_protect_install_signatures: true
23        nginx_app_protect_install_threat_campaigns: true
24        nginx_app_protect_setup_license: false
25        nginx_app_protect_remove_license: false
  • 4行目で nginxinc.nginx_core のコレクションを指定します

  • 8行目で nginx のロールを指定し、10-14行目でパラメータを指定します

    • 10行目 nginx_type : InstallするNGINXをOpenSourceかPlusか指定します

    • 11行目 nginx_license : NGINX Plusに必要となる証明書・鍵を指定します

    • 14行目 nginx_remove_license : インストール後ライセンスファイルの削除を指定します

    • その他パラメータは NGINX installation variables を参照してください

  • 18行目で nginx_app_protect のロールを指定し、20-25行目でパラメータを指定します

    • 20行目 nginx_app_protect_waf_enable : NGINX App Protect WAF をインストールします

    • 21行目 nginx_app_protect_dos_enable : NGINX App Protect DoS をインストールします

    • 22-23行目 nginx_app_protect_* : WAFのSignature、Threat Campaign Signatureをインストールします

    • 24-25 nginx_app_protect_*_license : ライセンスの利用、インストール後のライセンスファイルの削除を指定します

    • その他パラメータは NGINX App Protect installation and configuration variables を参照してください

NGINX Plus、NGINX App Protect WAF/DoS をインストール

## cd ~/f5j-nginx-ansible-lab
ansible-playbook -i inventories/hosts -l nginx1 playbook/deploy-nginx-plus-app-protect-waf-dos.yaml --private-key="~/.ssh/id_rsa"  --become
実行結果サンプル
 1PLAY [all] *************************************************************************************************************************************************************
 2
 3TASK [Gathering Facts] *************************************************************************************************************************************************
 4ok: [10.1.1.7]
 5
 6TASK [Install NGINX Plus] **********************************************************************************************************************************************
 7
 8** 省略 **
 9
10PLAY RECAP *************************************************************************************************************************************************************
1110.1.1.7                   : ok=49   changed=22   unreachable=0    failed=0    skipped=45   rescued=0    ignored=0

実行したコマンドのオプションの指定パラメータは以下です

ansible-playbook コマンドのサンプル
1ansible-playbook <option> <playbook file path>

option

用途・役割

-i

実行対象となるインベントリファイルを指定します

-l (–limit)

インベントリの対象となるホストをフィルタで指定します

–private-key

ホストに接続する際に利用する鍵ファイルのパスを指定します

–become (-b)

becomeで操作を実行します。権限昇格方法のデフォルトは sudo です

インストールしたパッケージの情報の確認します

参考となる記事はこちらです。
nginx -v

NGINX App Protect のVersion

cat /opt/app_protect/VERSION

NGINX App Protect DoS のVersion

admd -v

その他インストールしたパッケージの情報を確認いただけます。ラボ環境のホストはUbuntuとなります。

dpkg-query -l | grep nginx-plus
実行結果サンプル
1ii  nginx-plus                         25-1~focal                            amd64        NGINX Plus, provided by Nginx, Inc.
2ii  nginx-plus-module-appprotect       25+3.671.0-1~focal                    amd64        NGINX Plus app protect dynamic module version 3.671.0
3ii  nginx-plus-module-appprotectdos    25+2.0.1-1~focal                      amd64        NGINX Plus appprotectdos dynamic module
dpkg-query -l | grep app-protect
実行結果サンプル
1ii  app-protect                        25+3.671.0-1~focal                    amd64        App-Protect package for Nginx Plus, Includes all of the default files and examples. Nginx App Protect provides web application firewall (WAF) security protection for your web applications, including OWASP Top 10 attacks.
2ii  app-protect-attack-signatures      2021.11.16-1~focal                    amd64        Attack Signature Updates for App-Protect
3ii  app-protect-common                 8.12.1-1~focal                        amd64        NGINX App Protect
4ii  app-protect-compiler               8.12.1-1~focal                        amd64        Control-plane(aka CP) for waf-general debian
5ii  app-protect-dos                    25+2.0.1-1~focal                      amd64        Nginx DoS protection
6ii  app-protect-engine                 8.12.1-1~focal                        amd64        NGINX App Protect
7ii  app-protect-plugin                 3.671.0-1~focal                       amd64        NGINX App Protect plugin

3. NGINX Plus、NAP WAF/DoSのアンインストール

ライセンス、Playbookなど正しく配置していることを想定し説明を進めます。 確認が必要な場合 1. 事前確認 を参照してください。

Playbookの内容を確認します

## cd ~/f5j-nginx-ansible-lab
cat playbook/remove-nginx-plus-app-protect-waf-dos.yaml
実行結果サンプル
 1---
 2- hosts: all
 3  collections:
 4    - nginxinc.nginx_core
 5  tasks:
 6    - name: Uniinstall NGINX Plus
 7      ansible.builtin.include_role:
 8        name: nginx
 9      vars:
10        nginx_type: plus
11        nginx_setup: uninstall
12        nginx_license:
13          certificate: ~/nginx-repo.crt
14          key: ~/nginx-repo.key
15        nginx_remove_license: false
16        nginx_start: false
17
18    - name: Uninstall NGINX App Protect WAF/DoS
19      ansible.builtin.include_role:
20        name: nginx_app_protect
21      vars:
22        nginx_app_protect_waf_setup: uninstall
23        nginx_app_protect_dos_setup: uninstall
24        nginx_app_protect_setup_license: false
25        nginx_app_protect_remove_license: false
26        nginx_app_protect_start: false
  • 8行目で nginx のロールを指定し、11行目で nginx_setupuninstall を指定します

  • 20行目で nginx_app_protect のロールを指定し、22行目で nginx_app_protect_waf_setup 23行目 uninstnginx_app_protect_dos_setupalluninstall を指定します

  • 16行目で nginx_start: false 、 26行目で nginx_app_protect_start: false としています。このパラメータによりアンインストール後のプロセス再起動の動作を回避します

NGINX Plus、NGINX App Protect WAF/DoS をアンインストール

## cd ~/f5j-nginx-ansible-lab
ansible-playbook -i inventories/hosts -l nginx1 playbook/remove-nginx-plus-app-protect-waf-dos.yaml --private-key="~/.ssh/id_rsa"  --become
実行結果サンプル
 1PLAY [all] *************************************************************************************************************************************************************
 2
 3TASK [Gathering Facts] *************************************************************************************************************************************************
 4ok: [10.1.1.7]
 5
 6TASK [CleanUp nginx.conf for Uninstall] ********************************************************************************************************************************
 7
 8** 省略 **
 9
10RUNNING HANDLER [nginxinc.nginx_core.nginx_app_protect : (Handler - NGINX App Protect) Restart NGINX] ******************************************************************************************************************************************
11skipping: [10.1.1.7]
12
13RUNNING HANDLER [nginxinc.nginx_core.nginx_app_protect : (Handler - NGINX App Protect) Check NGINX] ********************************************************************************************************************************************
14fatal: [10.1.1.7]: FAILED! => {"changed": false, "cmd": "nginx -t", "msg": "[Errno 2] No such file or directory: b'nginx'", "rc": 2, "stderr": "", "stderr_lines": [], "stdout": "", "stdout_lines": []}
15...ignoring
16
17RUNNING HANDLER [nginxinc.nginx_core.nginx_app_protect : (Handler - NGINX App Protect) Print NGINX error if syntax check fails] ****************************************************************************************************************
18skipping: [10.1.1.7]
19
20PLAY RECAP *************************************************************************************************************************************************************************************************************************************
2110.1.1.7                   : ok=33   changed=10   unreachable=0    failed=0    skipped=39   rescued=0    ignored=1
  • 13-15行目で、nginx_app_protect ロールで設定ファイルの書式をチェックする nginx -t が実行されていますが、NGINX Plusがアンインストールされているためコマンドが正常に完了しません。こちらは無視してよい動作です

  • 21行目で、実行結果が表示されます。 13-15行目の実行結果が ignore として表示されています

インストール時に確認したコマンドで状態を確認すると以下のようになります。

確認結果サンプル
 1$ cat /opt/app_protect/VERSION
 2cat: /opt/app_protect/VERSION: No such file or directory
 3
 4$ nginx -v
 5
 6Command 'nginx' not found, but can be installed with:
 7
 8sudo apt install nginx-core    # version 1.18.0-0ubuntu1.3, or
 9sudo apt install nginx-extras  # version 1.18.0-0ubuntu1.3
10sudo apt install nginx-full    # version 1.18.0-0ubuntu1.3
11sudo apt install nginx-light   # version 1.18.0-0ubuntu1.3
12
13$ dpkg-query -l | grep nginx-plus
14rc  nginx-plus                         27-1~focal                            amd64        NGINX Plus, provided by Nginx, Inc.
15
16$ dpkg-query -l | grep app-protect
17rc  app-protect                        27+3.954.0-1~focal                    amd64        App-Protect package for Nginx Plus, Includes all of the default files and examples. Nginx App Protect provides web application firewall (WAF) security protection for your web applications, including OWASP Top 10 attacks.
18ii  app-protect-common                 10.87.0-1~focal                       amd64        NGINX App Protect
19rc  app-protect-compiler               10.87.0-1~focal                       amd64        Control-plane(aka CP) for waf-general debian
20rc  app-protect-dos                    27+2.4.1-1~focal                      amd64        Nginx DoS protection
21rc  app-protect-engine                 10.87.0-1~focal                       amd64        NGINX App Protect
22rc  app-protect-plugin                 3.954.0-1~focal                       amd64        NGINX App Protect plugin

プログラムとして app-protect-common は残った状態になりますので、完全に削除されたい場合には以下のコマンドを参考に削除してください。

sudo apt remove app-protect-common

Ansibleでアンインストールを行った場合、NGINX Plusの設定ファイルが残ります。Ubuntu環境で完全に削除する場合には以下コマンドを参考に削除してください。

sudo apt remove nginx-plus --purge

NGINX Plusを完全に削除した後、 /etc/nginx フォルダが削除されます

確認結果サンプル
1$ ls /etc/nginx
2ls: cannot access '/etc/nginx': No such file or directory