commit 6a9fa3321ec50271129d5fd39cc6a9ade1da8635 Author: JianMiau Date: Mon Aug 4 17:11:47 2025 +0800 [add] first diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..22444b2 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +/*.DS_Store +/conf/conf.d diff --git a/README.md b/README.md new file mode 100644 index 0000000..59360aa --- /dev/null +++ b/README.md @@ -0,0 +1,71 @@ +# NGINX 動態 Server 管理工具 + +此專案提供一個簡易的 PHP 網頁介面,可以動態新增和刪除 NGINX Server 配置,並即時重載 NGINX 配置,方便本地多專案開發環境管理。 + +--- + +## 主要功能 + +- 顯示目前所有 NGINX 動態 Server(以 conf.d/*.conf 設定檔為基礎) +- 新增 Server:指定 Port 與根目錄(根目錄為指定的相對路徑,會自動補上預設絕對路徑前綴) +- 刪除 Server +- 新增、刪除後自動重載 NGINX 配置 +- 預設排除保留端口(如 80、8080)及已被使用的端口 +- 前端輸入檢查,避免重複端口與保留端口使用 + +--- + +## 系統需求 + +- PHP 7.0 以上 +- NGINX 已安裝且可透過命令行重載 +- PHP 有權限讀寫 NGINX conf.d 目錄 +- 適用於 macOS 或 Linux 環境,路徑請自行調整 + +--- + +## 專案結構 + +nginx-manager/ +│ +├── conf/ +│ └── conf.d/ # NGINX 動態 Server 設定檔目錄 +├── index.php # 主頁面,顯示及新增刪除 Server +├── servers.php # 處理新增刪除 Server 請求並重載 NGINX +└── README.md + +--- + +## 使用說明 + +1. 修改 `servers.php` 中的 `$confDir` 及 `$nginxExe` 變數,設定對應的 NGINX 配置目錄與 nginx 可執行檔路徑 + +2. 修改 `index.php` 中 `$basePath` 變數(預設為 `/Users/catantech/Desktop/`,用於拼接使用者輸入的相對路徑) + +3. 將此專案放置於支援 PHP 的 Web 伺服器中(例如內建 PHP server 或 Apache) + +4. 開啟瀏覽器進入 `index.php`,即可看到目前動態 Server,並可新增或刪除 + +5. 新增時,請輸入相對於 `$basePath` 的路徑(例如:`Project/Line_Project_1/Official/out-dev`) + +--- + +## 注意事項 + +- 確保 PHP 執行者有讀寫 NGINX 動態設定檔目錄的權限 + +- 新增 Server 會立即寫入設定檔並重載 NGINX,請確保 NGINX 配置無誤,避免重載失敗 + +- 本專案不包含完整的安全檢查,建議用於本地開發環境 + +--- + +## 授權 + +MIT License + +--- + +## 聯絡 + +如有問題,歡迎聯絡建喵。 diff --git a/favicon.png b/favicon.png new file mode 100644 index 0000000..a496302 Binary files /dev/null and b/favicon.png differ diff --git a/index.php b/index.php new file mode 100644 index 0000000..a68c689 --- /dev/null +++ b/index.php @@ -0,0 +1,140 @@ + $port, + 'file' => $file, + 'content' => file_get_contents($file) + ]; + } + return $servers; +} + +$servers = listServerFiles(); + +$reservedPorts = [80, 8080]; + +// 合併所有已使用 port 與保留 port +$usedPorts = array_unique(array_merge( + $reservedPorts, + array_map('intval', array_column($servers, 'port')) +)); + +function findRandomAvailablePort($start = 3000, $end = 9000, $usedPorts = []) { + $candidates = range($start, $end); + shuffle($candidates); + foreach ($candidates as $port) { + if (!in_array($port, $usedPorts)) { + return $port; + } + } + return null; +} + +$autoPort = findRandomAvailablePort(3000, 9000, $usedPorts); + +$resultMsg = ''; +if (isset($_SESSION['resultMsg'])) { + $resultMsg = $_SESSION['resultMsg']; + unset($_SESSION['resultMsg']); +} +?> + + + + + NGINX 動態 Server 管理 + + + + + +
+ +
+ + +

📡 現有 Servers

+ + +
+

➕ 新增 Server

+
+ + + +
+ + +
+ +
+ + + + + diff --git a/servers.php b/servers.php new file mode 100644 index 0000000..3a84b24 --- /dev/null +++ b/servers.php @@ -0,0 +1,82 @@ +&1"; + exec($cmd, $output, $code); + return ['code' => $code, 'output' => $output]; +} + +if ($_SERVER['REQUEST_METHOD'] === 'POST') { + $port = intval($_POST['port']); + $confFile = "$confDir/{$port}.conf"; + $resultMsg = ''; + + if ($_POST['action'] === 'add') { + $baseDir = '/Users/catantech/Desktop/'; + $relativeRoot = trim($_POST['root']); + + // 防止目錄跳脫 + if (strpos($relativeRoot, '..') !== false) { + $resultMsg = "❌ Root 路徑不能包含 '..' "; + } else { + // 拼接完整路徑 + $fullRoot = rtrim($baseDir, '/') . '/' . ltrim($relativeRoot, '/'); + + $serverBlock = <<'; +echo shell_exec('whoami'); +echo shell_exec('C:\nginx\nginx.exe -t 2>&1'); +echo shell_exec('C:\nginx\nginx.exe -s reload 2>&1'); +echo ''; \ No newline at end of file