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 2 3 4 5 6 | @all: |--@nginx1: | |--ansible_host:10.1.1.7 |--@nginx2: | |--ansible_host:10.1.1.6 |--@ungrouped: |
nginx1
と nginx2
という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 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | --- - hosts: all collections: - nginxinc.nginx_core tasks: - name: Install NGINX Plus ansible.builtin.include_role: name: nginx vars: nginx_type: plus nginx_license: certificate: ~/nginx-repo.crt key: ~/nginx-repo.key nginx_remove_license: false - name: Install NGINX App Protect WAF/DoS ansible.builtin.include_role: name: nginx_app_protect vars: nginx_app_protect_waf_enable: true nginx_app_protect_dos_enable: true nginx_app_protect_install_signatures: true nginx_app_protect_install_threat_campaigns: true nginx_app_protect_setup_license: false 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 を参照してください
- 10行目
- 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 を参照してください
- 20行目
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
1 2 3 4 5 6 7 8 9 10 11 | PLAY [all] ************************************************************************************************************************************************************* TASK [Gathering Facts] ************************************************************************************************************************************************* ok: [10.1.1.7] TASK [Install NGINX Plus] ********************************************************************************************************************************************** ** 省略 ** PLAY RECAP ************************************************************************************************************************************************************* 10.1.1.7 : ok=49 changed=22 unreachable=0 failed=0 skipped=45 rescued=0 ignored=0 |
実行したコマンドのオプションの指定パラメータは以下です
1 | ansible-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
1 2 3 | ii nginx-plus 25-1~focal amd64 NGINX Plus, provided by Nginx, Inc. ii nginx-plus-module-appprotect 25+3.671.0-1~focal amd64 NGINX Plus app protect dynamic module version 3.671.0 ii nginx-plus-module-appprotectdos 25+2.0.1-1~focal amd64 NGINX Plus appprotectdos dynamic module |
dpkg-query -l | grep app-protect
1 2 3 4 5 6 7 | ii 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. ii app-protect-attack-signatures 2021.11.16-1~focal amd64 Attack Signature Updates for App-Protect ii app-protect-common 8.12.1-1~focal amd64 NGINX App Protect ii app-protect-compiler 8.12.1-1~focal amd64 Control-plane(aka CP) for waf-general debian ii app-protect-dos 25+2.0.1-1~focal amd64 Nginx DoS protection ii app-protect-engine 8.12.1-1~focal amd64 NGINX App Protect ii 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 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | --- - hosts: all collections: - nginxinc.nginx_core tasks: - name: Uniinstall NGINX Plus ansible.builtin.include_role: name: nginx vars: nginx_type: plus nginx_setup: uninstall nginx_license: certificate: ~/nginx-repo.crt key: ~/nginx-repo.key nginx_remove_license: false nginx_start: false - name: Uninstall NGINX App Protect WAF/DoS ansible.builtin.include_role: name: nginx_app_protect vars: nginx_app_protect_waf_setup: uninstall nginx_app_protect_dos_setup: uninstall nginx_app_protect_setup_license: false nginx_app_protect_remove_license: false nginx_app_protect_start: false |
- 8行目で
nginx
のロールを指定し、11行目でnginx_setup
でuninstall
を指定します - 20行目で
nginx_app_protect
のロールを指定し、22行目でnginx_app_protect_waf_setup
23行目uninstnginx_app_protect_dos_setupall
でuninstall
を指定します - 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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | PLAY [all] ************************************************************************************************************************************************************* TASK [Gathering Facts] ************************************************************************************************************************************************* ok: [10.1.1.7] TASK [CleanUp nginx.conf for Uninstall] ******************************************************************************************************************************** ** 省略 ** RUNNING HANDLER [nginxinc.nginx_core.nginx_app_protect : (Handler - NGINX App Protect) Restart NGINX] ****************************************************************************************************************************************** skipping: [10.1.1.7] RUNNING HANDLER [nginxinc.nginx_core.nginx_app_protect : (Handler - NGINX App Protect) Check NGINX] ******************************************************************************************************************************************** fatal: [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": []} ...ignoring RUNNING HANDLER [nginxinc.nginx_core.nginx_app_protect : (Handler - NGINX App Protect) Print NGINX error if syntax check fails] **************************************************************************************************************** skipping: [10.1.1.7] PLAY RECAP ************************************************************************************************************************************************************************************************************************************* 10.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 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | $ cat /opt/app_protect/VERSION cat: /opt/app_protect/VERSION: No such file or directory $ nginx -v Command 'nginx' not found, but can be installed with: sudo apt install nginx-core # version 1.18.0-0ubuntu1.3, or sudo apt install nginx-extras # version 1.18.0-0ubuntu1.3 sudo apt install nginx-full # version 1.18.0-0ubuntu1.3 sudo apt install nginx-light # version 1.18.0-0ubuntu1.3 $ dpkg-query -l | grep nginx-plus rc nginx-plus 27-1~focal amd64 NGINX Plus, provided by Nginx, Inc. $ dpkg-query -l | grep app-protect rc 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. ii app-protect-common 10.87.0-1~focal amd64 NGINX App Protect rc app-protect-compiler 10.87.0-1~focal amd64 Control-plane(aka CP) for waf-general debian rc app-protect-dos 27+2.4.1-1~focal amd64 Nginx DoS protection rc app-protect-engine 10.87.0-1~focal amd64 NGINX App Protect rc 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 2 | $ ls /etc/nginx ls: cannot access '/etc/nginx': No such file or directory |