nginx-proxy-manager-Bash-API/README.md

294 lines
10 KiB
Markdown
Raw Normal View History

2024-07-10 14:23:30 +02:00
# Nginx Proxy Manager CLI Script
2024-07-13 15:42:14 +02:00
## Table of Contents
1. [Description](#description)
2. [Reference API](#reference)
3. [Prerequisites](#prerequisites)
4. [Installation](#installation)
5. [Settings](#settings)
6. [Usage](#usage)
7. [Options](#options)
8. [Examples](#examples)
2024-07-29 14:03:47 +02:00
- [Info script](#info)
- [List HOST](#list)
- [Enable SSL](#ssl)
2024-07-28 20:58:33 +02:00
12. [Screens](#screens)
13. [TODO](#todo)
2024-07-13 14:48:27 +02:00
> [!WARNING]
2024-09-07 14:45:18 +02:00
> The --restore command is disabled (a fix is in progress).
2024-07-30 00:16:12 +02:00
>
2024-07-30 23:51:53 +02:00
2024-07-13 15:42:14 +02:00
## Description
2024-07-10 14:23:30 +02:00
2024-07-13 15:45:31 +02:00
🛠️ This BASH script enables the management of ![Nginx Proxy Manager](https://github.com/NginxProxyManager/nginx-proxy-manager?utm_source=nginx-proxy-manager) through its **API**.
2024-07-10 14:23:30 +02:00
2024-07-13 15:45:31 +02:00
🔑 **Automatically generates** and **manages tokens**, ensuring their validity, so you don't have to worry about token expiration.
2024-07-10 14:26:46 +02:00
2024-07-13 15:45:31 +02:00
⚙️ Provides functionalities such as creating and deleting proxy hosts, managing users, displaying configurations, creating **BACKUPS**, and more.
2024-07-10 14:17:48 +02:00
2024-07-29 14:03:47 +02:00
2024-07-28 20:58:33 +02:00
### French description:
2024-07-12 13:11:23 +02:00
Ce script permet de gérer Nginx Proxy Manager via l'API. Il fournit des fonctionnalités telles que la création de hosts proxy, la gestion des utilisateurs, et l'affichage des configurations avec creation de BACKUP !
2024-07-28 20:58:33 +02:00
La fonction RESTORE n'est pas encore terminée.
2024-07-10 21:15:14 +02:00
2024-07-12 13:30:59 +02:00
## Reference
![https://github.com/NginxProxyManager/nginx-proxy-manager/tree/develop/backend/schema](https://github.com/NginxProxyManager/nginx-proxy-manager/tree/develop/backend/schema)
2024-07-11 18:25:09 +02:00
## Prerequisites
2024-07-10 21:15:14 +02:00
2024-07-13 14:47:34 +02:00
And of course the excellent NPM (![Nginx Proxy Manager](https://github.com/NginxProxyManager/nginx-proxy-manager?utm_source=nginx-proxy-manager))
2024-07-13 14:13:47 +02:00
2024-07-29 14:03:47 +02:00
Required basic dependencies.
> The script will automatically check if they are installed and will download them if necessary:
2024-07-13 14:13:47 +02:00
2024-07-10 21:15:14 +02:00
- `curl`
- `jq`
2024-07-10 14:45:21 +02:00
2024-07-10 22:44:33 +02:00
## Installation
```
2024-07-11 12:39:16 +02:00
wget https://raw.githubusercontent.com/Erreur32/nginx-proxy-manager-API/main/nginx_proxy_manager_cli.sh
chmod +x nginx_proxy_manager_cli.sh
2024-07-29 14:03:47 +02:00
# Check script
./nginx_proxy_manager_cli.sh --info
2024-07-10 22:44:33 +02:00
```
2024-07-10 14:45:21 +02:00
2024-07-28 19:12:10 +02:00
2024-07-29 14:03:47 +02:00
> [!NOTE]
> With the new `V2.0.0`, some command arguments have been changed to be simpler, and need to set `BASE_DIR` variable to store `Tokens` and `Backups`.
2024-07-28 20:47:33 +02:00
2024-07-11 18:27:04 +02:00
## Settings
2024-09-28 20:21:48 +02:00
Optionnal but recommanded, Create config file in the root directory
2024-07-29 14:03:47 +02:00
Required to edit these 4 variables:
2024-07-10 14:45:21 +02:00
2024-07-28 17:57:09 +02:00
```bash
2024-07-11 19:16:35 +02:00
## Nginx proxy IP address (your Nginx IP)
2024-07-10 20:48:13 +02:00
NGINX_IP="127.0.0.1"
2024-07-10 22:44:33 +02:00
## Existing user (user and password) on NPM
2024-07-10 20:48:13 +02:00
API_USER="existingUser@mail.com"
2024-07-10 14:45:21 +02:00
API_PASS="password"
2024-07-28 15:46:38 +02:00
# Path to store .txt files and Backups
BASE_DIR="/path/nginx_proxy_script/data"
2024-07-10 14:45:21 +02:00
```
2024-07-11 18:27:04 +02:00
## Usage
2024-07-10 14:17:48 +02:00
```bash
./nginx_proxy_manager_cli.sh [OPTIONS]
2024-07-29 14:03:47 +02:00
./nginx_proxy_manager_cli.sh --info
./nginx_proxy_manager_cli.sh --show-default
2024-07-10 14:17:48 +02:00
```
2024-07-11 18:27:04 +02:00
## Options
2024-07-10 14:17:48 +02:00
```tcl
2024-07-28 15:46:38 +02:00
🌐 Host proxy creation:
2024-07-15 12:51:40 +02:00
-d DOMAIN_NAMES Domain name (required for creating/updating hosts)
-i FORWARD_HOST IP address or domain name of the target server (required for creating/updating hosts)
-p FORWARD_PORT Port of the target server (required for creating/updating hosts)
-f FORWARD_SCHEME Scheme for forwarding (http/https, default: http)
-c CACHING_ENABLED Enable caching (true/false, default: false)
-b BLOCK_EXPLOITS Block exploits (true/false, default: true)
-w ALLOW_WEBSOCKET_UPGRADE Allow WebSocket upgrade (true/false, default: true)
2024-07-28 15:47:56 +02:00
-l CUSTOM_LOCATIONS Custom locations (JSON array of location objects)
2024-07-15 12:51:40 +02:00
-a ADVANCED_CONFIG Advanced configuration (block of configuration settings)
2024-07-28 15:46:38 +02:00
📦 Backup and Restore:
2024-07-30 23:51:53 +02:00
--backup Backup all configurations to a file
--backup-host id Backup a single host configuration and its certificate (if exists)
2024-07-15 12:51:40 +02:00
🔧 Miscellaneous:
2024-09-28 20:21:48 +02:00
# --check-token Check if the current token is valid
# --create-user user pass email Create a user with a username, password and email
# --delete-user username Delete a user by username
# --host-delete id Delete a proxy host by ID
# --host-show id Show full details for a specific host by ID
# --show-default Show default settings for creating hosts
# --host-list List the names of all proxy hosts
# --host-list-full List all proxy hosts with full details
# --host-list-users List all users
# --host-search hostname Search for a proxy host by domain name
# --host-enable id Enable a proxy host by ID
# --host-disable id Disable a proxy host by ID
# --host-ssl-enable id Enable SSL, HTTP/2, and HSTS for a proxy host
# --host-ssl-disable id Disable SSL, HTTP/2, and HSTS for a proxy host
# --list-ssl-certificates List All SSL certificates availables (JSON)
# --generate-cert domain email Generate certificate for the given domain and email
# --delete-cert domain Delete certificate for the given domain
# --help Display this help
2024-07-12 21:58:22 +02:00
2024-07-10 14:17:48 +02:00
```
2024-07-10 14:05:48 +02:00
2024-07-11 18:27:04 +02:00
## Examples
2024-07-28 17:57:09 +02:00
2024-07-10 14:17:48 +02:00
```bash
📦 Backup First !
2024-07-12 21:58:22 +02:00
./nginx_proxy_manager_cli.sh --backup
2024-07-12 13:23:51 +02:00
2024-07-28 15:46:38 +02:00
🌐 Host Creation:
2024-07-12 21:58:22 +02:00
./nginx_proxy_manager_cli.sh -d example.com -i 192.168.1.10 -p 8080 (check default values below)
2024-07-28 17:57:09 +02:00
./nginx_proxy_manager_cli.sh --info
2024-07-12 21:58:22 +02:00
./nginx_proxy_manager_cli.sh --show-default
2024-07-12 13:23:51 +02:00
./nginx_proxy_manager_cli.sh --create-user newuser password123 user@example.com
./nginx_proxy_manager_cli.sh --delete-user 'username'
2024-07-28 15:46:38 +02:00
./nginx_proxy_manager_cli.sh --host-list
./nginx_proxy_manager_cli.sh --host-ssl-enable 10
2024-07-12 13:23:51 +02:00
2024-07-28 15:46:38 +02:00
🔧 Advanced Example:
2024-07-12 13:26:49 +02:00
./nginx_proxy_manager_cli.sh -d example.com -i 192.168.1.10 -p 8080 -a 'proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;'
2024-07-12 21:58:22 +02:00
2024-07-28 15:46:38 +02:00
🛡️ Custom Certificate:
2024-09-28 20:21:48 +02:00
./nginx_proxy_manager_cli.sh --generate-cert example.com user@example.com
2024-07-28 15:46:38 +02:00
🛡️ Custom locations:
./nginx_proxy_manager_cli.sh -d example.com -i 192.168.1.10 -p 8080 -l '[{"path":"/api","forward_host":"192.168.1.11","forward_port":8081}]'
🔖 Full options:
./nginx_proxy_manager_cli.sh -d example.com -i 192.168.1.10 -p 8080 -f https -c true -b true -w true -a 'proxy_set_header X-Real-IP $remote_addr;' -l '[{"path":"/api","forward_host":"192.168.1.11","forward_port":8081}]'
2024-07-10 14:17:48 +02:00
```
2024-07-12 22:30:04 +02:00
2024-07-28 19:24:10 +02:00
##### Verifying the Configuration
2024-07-29 14:05:15 +02:00
2024-07-29 14:03:47 +02:00
###### Info
2024-07-30 00:16:12 +02:00
2024-07-30 12:57:19 +02:00
Some info of settings in the script with `./nginx_proxy_manager_cli_.sh --info`
2024-07-30 00:16:12 +02:00
2024-07-30 12:57:19 +02:00
```bash
2024-07-30 00:16:12 +02:00
./nginx_proxy_manager_cli_.sh --info
2024-07-29 14:05:15 +02:00
Script Info: 2.3.5
2024-07-12 22:30:04 +02:00
2024-07-29 14:05:15 +02:00
Script Variables Information:
BASE_URL http://127.0.0.1:81/api
NGINX_IP 127.0.0.1
API_USER user@monmail.com
BASE_DIR /path/to/nginx_proxy
BACKUP_DIR /path/to/nginx_proxy/backups
BACKUP HOST 40
Token NPM /path/to/nginx_proxy/token/token_127.0.0.1.txt
```
2024-07-30 00:16:12 +02:00
2024-07-30 12:57:19 +02:00
**How to activate SSL ?**
2024-07-12 22:30:04 +02:00
By following these steps, you can enable SSL for your proxy host for the first time using Let's Encrypt.
2024-07-29 14:03:47 +02:00
##### List
List all Host in one command and show ´id´ , ´status´ and ´SSL´ status:
2024-07-13 13:57:30 +02:00
2024-07-28 19:24:10 +02:00
./nginx_proxy_manager_cli.sh --host-list
👉 List of proxy hosts (simple)
ID Domain Status SSL
2024-07-30 12:57:19 +02:00
1 toto.fun enabled ✘
2024-07-28 19:24:10 +02:00
2 titi.fun disable ✅
3 tutu.fun enabled ✅
2024-07-29 14:03:47 +02:00
##### SSL
Enable SSL for the Host:
2024-07-30 12:57:19 +02:00
Assuming the host ID is *1*, you would enable SSL for the host as follows:
./nginx_proxy_manager_cli.sh --host-ssl-enable 1
2024-07-29 14:03:47 +02:00
2024-07-30 12:57:19 +02:00
SSl is enable successfully, check again with --host-list
./nginx_proxy_manager_cli.sh --host-list
👉 List of proxy hosts (simple)
ID Domain Status SSL
1 toto.fun enabled ✅
2 titi.fun disable ✅
3 tutu.fun enabled ✅
2024-07-28 20:58:33 +02:00
2024-07-30 00:16:12 +02:00
2024-07-30 00:12:27 +02:00
Et Voilà.
2024-07-29 14:03:47 +02:00
2024-07-30 12:57:19 +02:00
**Other Exemple command:**
2024-07-30 00:12:27 +02:00
2024-07-29 14:03:47 +02:00
Host proxy info command `--host-show id`
2024-07-28 20:58:33 +02:00
2024-07-30 00:12:27 +02:00
2024-07-13 14:08:53 +02:00
```
2024-07-30 12:57:19 +02:00
./nginx_proxy_manager_cli_.sh --host-show 1
2024-07-13 14:08:53 +02:00
✅ Nginx url: http://127.0.0.1:81/api
✅ The token is valid. Expiry: 2025-07-12T08:14:58.521Z
👉 Full details for proxy host ID: 59...
{
"id": 10,
"created_on": "2024-07-11 13:16:34",
"modified_on": "2024-07-13 09:42:40",
"owner_user_id": 1,
"domain_names": [
"test.domain.com"
],
"forward_host": "127.0.0.1",
"forward_port": 80,
"access_list_id": 0,
"certificate_id": 81,
"ssl_forced": 1,
"caching_enabled": 0,
"block_exploits": 1,
"advanced_config": "",
"meta": {
"letsencrypt_agree": true,
"letsencrypt_email": "",
"nginx_online": true,
"nginx_err": null
},
"allow_websocket_upgrade": 1,
"http2_support": 1,
"forward_scheme": "http",
"enabled": 1,
"locations": [],
"hsts_enabled": 1,
"hsts_subdomains": 0
}
```
2024-07-13 13:57:30 +02:00
## Screens:
2024-07-13 11:51:35 +02:00
```
# ./nginx_proxy_manager_cli.sh --backup
2024-07-29 14:03:47 +02:00
✅ Users backup completed 🆗: /path/to/nginx_proxy/backups/users_127_0_0_1_2024_07_29__14_01_23.json
✅ Settings backup completed 🆗: /path/to/nginx_proxy/backups/settings_127_0_0_1_2024_07_29__14_01_23.json
✅ Proxy host backup completed 🆗: /path/to/nginx_proxy/backups
✅ Access lists backup completed 🆗: /path/to/nginx_proxy/backups/access_lists_127_0_0_1_2024_07_29__14_01_23.json
✅ Backup 🆗
📦 Backup Summary:
- Number of users backed up: 1
- Number of proxy hosts backed up: 31
- Total number of backup files: 42
2024-07-13 11:51:35 +02:00
```
2024-07-10 14:19:15 +02:00
2024-07-13 13:57:30 +02:00
2024-07-12 21:58:22 +02:00
![https://github.com/Erreur32/nginx-proxy-manager-API/blob/main/screen-nginx-proxy-default.png](https://github.com/Erreur32/nginx-proxy-manager-API/blob/main/screen-nginx-proxy-default.png)
2024-07-10 14:23:30 +02:00
2024-07-10 14:19:15 +02:00
## TODO:
2024-07-13 11:55:50 +02:00
- [x] add setting for ADVANCED configuration in npm `location / { ... }`
2024-07-12 13:11:23 +02:00
- [x] Add documentation on certain functions
2024-07-12 13:08:46 +02:00
- [x] ADD: a configuration function for Custom Locations
2024-07-13 11:55:50 +02:00
- [x] Backup all settings from NPM
2024-07-11 18:50:35 +02:00
- [ ] Domain TLS check validity
2024-07-15 14:18:59 +02:00
- [ ] Better Error Handeling
2024-07-28 15:46:38 +02:00
- [ ] Restore Function need to be optimized