PHP 不僅能寫 Web,還能寫 CLI 腳本自動化運維。本文展示一個互動式 PHP Shell 腳本,透過選單建立/刪除 Nginx 虛擬主機,自動生成配置、建立目錄、備份舊配、壓縮網站檔案,適合中小型機房或開發機快速佈站。
#!/usr/bin/php -q
<?php
// 禁止 Web 訪問
if (php_sapi_name() !== 'cli') { die('CLI only'); }
$nginxSitesDir = '/etc/nginx/sites';
$nginxTemplate = '/etc/nginx/template/site_conf';
$nginxBackupDir = '/etc/nginx/backup';
$sitesRoot = '/home/sites';
@mkdir($nginxBackupDir, 0755, true);
@mkdir($sitesRoot, 0755, true);
function menu(): int
{
fwrite(STDOUT, "\n=========== Vhost Manager ===========\n");
fwrite(STDOUT, "= 1. Create 2. Delete 3. Exit =\n");
fwrite(STDOUT, "=====================================\n");
fwrite(STDOUT, 'Choose: ');
return (int)trim(fgets(STDIN));
}
function createVhost(): void
{
global $nginxSitesDir, $nginxTemplate, $sitesRoot;
fwrite(STDOUT, 'Domain: ');
$domain = trim(fgets(STDIN));
if (!$domain) { echo "Domain required\n"; return; }
$path = "$sitesRoot/$domain";
$confFile = "$nginxSitesDir/$domain";
if (file_exists($path) || file_exists($confFile)) {
echo "Domain already exists\n"; return;
}
mkdir($path, 0755, true);
$tpl = file_get_contents($nginxTemplate);
// 變量替換:模版中使用 {$domain}、{$path}
eval("\$conf = \"$tpl\";"); // 注意:模版需受信,生產環境建議用 strtr/preg_replace
file_put_contents($confFile, $conf);
echo "Created: $confFile\n";
echo "Reload nginx: systemctl reload nginx\n";
}
function deleteVhost(): void
{
global $nginxSitesDir, $nginxBackupDir, $sitesRoot;
$files = array_filter(scandir($nginxSitesDir), fn($f) => is_file("$nginxSitesDir/$f"));
if (!$files) { echo "No vhosts\n"; return; }
echo "Select number to delete:\n";
foreach ($files as $i => $f) { echo "$i. $f\n"; }
fwrite(STDOUT, 'Number: ');
$idx = (int)trim(fgets(STDIN));
$target = array_values($files)[$idx] ?? null;
if (!$target) { echo "Invalid\n"; return; }
// 備份配置
copy("$nginxSitesDir/$target", "$nginxBackupDir/$target." . date('YmdHis'));
unlink("$nginxSitesDir/$target");
// 打包網站檔案並刪除
$siteDir = "$sitesRoot/$target";
if (is_dir($siteDir)) {
exec("tar -czf $siteDir." . date('YmdHis') . ".tar.gz -C $sitesRoot $target");
exec("rm -rf " . escapeshellarg($siteDir));
}
echo "Deleted & backed up: $target\n";
}
while (true) {
switch (menu()) {
case 1: createVhost(); break;
case 2: deleteVhost(); break;
case 3: exit(0);
default: echo "Invalid\n";
}
}
?>
server {
listen 80;
server_name {$domain};
access_log /var/log/nginx/{$domain}_access.log;
error_log /var/log/nginx/{$domain}_error.log;
root {$path};
location = /favicon.ico { access_log off; log_not_found off; }
location = /robots.txt { access_log off; log_not_found off; }
location / {
index index.html index.htm index.php;
try_files $uri $uri/ @proxy;
}
location ~* \.(html|htm)$ {
expires 30s; gzip off; add_header Content-Encoding gzip;
try_files $uri $uri/ /wp-content/cache/supercache/$http_host/$request_uri/index.html.gz @proxy;
}
location ~* \.(jpe?g|gif|png|ico|css|js|flv|swf|avi|zip|rar|svg|tgz|gz|bz2|doc|xls|exe|pdf|ppt|txt|tar|mp3)$ {
expires 30d;
}
location @proxy {
proxy_pass https://127.0.0.1:81;
include /etc/nginx/proxy.conf;
}
}
將腳本放入 `/usr/local/sbin/vhost-manager`、加執行權限(`chmod +x`),即可隨時輸入 `vhost-manager` 管理虛擬主機,比手動編輯配置快十倍。