[add] first
This commit is contained in:
82
servers.php
Normal file
82
servers.php
Normal file
@@ -0,0 +1,82 @@
|
||||
<?php
|
||||
session_start();
|
||||
|
||||
$confDir = realpath('/Users/catantech/Desktop/Project/php/nginx-manager/conf/conf.d');
|
||||
$nginxExe = '/opt/homebrew/bin/nginx';
|
||||
|
||||
if (!$confDir) {
|
||||
die("❌ 找不到 conf.d 目錄");
|
||||
}
|
||||
|
||||
function reloadNginx() {
|
||||
global $nginxExe;
|
||||
$configPath = '/opt/homebrew/etc/nginx/nginx.conf'; // 請根據實際路徑調整
|
||||
$cmd = "\"$nginxExe\" -s reload -c \"$configPath\" 2>&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 = <<<CONF
|
||||
server {
|
||||
listen $port;
|
||||
server_name localhost;
|
||||
|
||||
location / {
|
||||
root $fullRoot;
|
||||
index index.html;
|
||||
try_files \$uri \$uri/ /index.html;
|
||||
}
|
||||
}
|
||||
CONF;
|
||||
|
||||
$writeOk = file_put_contents($confFile, $serverBlock) !== false;
|
||||
if (!$writeOk) {
|
||||
$resultMsg = "❌ 寫入設定檔失敗";
|
||||
} else {
|
||||
$reload = reloadNginx();
|
||||
if ($reload['code'] !== 0) {
|
||||
$resultMsg = "❌ NGINX 重啟失敗:" . implode("\n", $reload['output']);
|
||||
} else {
|
||||
$resultMsg = "✅ 新增 Server 成功,並已重啟 NGINX";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
} elseif ($_POST['action'] === 'delete') {
|
||||
if (file_exists($confFile)) {
|
||||
$delOk = unlink($confFile);
|
||||
if (!$delOk) {
|
||||
$resultMsg = "❌ 刪除設定檔失敗";
|
||||
} else {
|
||||
$reload = reloadNginx();
|
||||
if ($reload['code'] !== 0) {
|
||||
$resultMsg = "❌ NGINX 重啟失敗:" . implode("\n", $reload['output']);
|
||||
} else {
|
||||
$resultMsg = "✅ 刪除 Server 成功,並已重啟 NGINX";
|
||||
}
|
||||
}
|
||||
} else {
|
||||
$resultMsg = "❌ 設定檔不存在";
|
||||
}
|
||||
}
|
||||
|
||||
$_SESSION['resultMsg'] = $resultMsg;
|
||||
header("Location: index.php");
|
||||
exit;
|
||||
}
|
Reference in New Issue
Block a user