首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> 網頁程式設計 >> PHP 程式設計 >> PHP 相關知識

PHP 管理 Nginx 虛擬主機 Shell 腳本實戰:交互式增刪備份一體化

     編輯:PHP 相關知識

PHP 管理 Nginx 虛擬主機 Shell 腳本實戰

利用 PHP 的 CLI 模式編寫交互式腳本,替代傳統 Bash,管理 Nginx 虛擬主機配置:創建、刪除、備份、目錄管理一氣呵成。

核心腳本 vhost_manager.php

<?php
#!/usr/bin/php -q
// 菜單循環
start:
fwrite(STDOUT, "=========== Vhost Script ===========\n");
fwrite(STDOUT, "= Choose an operation \n");
fwrite(STDOUT, "= 1.Create 2.Delete 3.Exit\n");
fwrite(STDOUT, "==================================\n");
$operate = trim(fgets(STDIN));

if ($operate == 1) { // 創建
    fwrite(STDOUT, "Please Enter a Domain Name: ");
    $domain = trim(fgets(STDIN));
    $path = "/home/sites/{$domain}";
    $nginx_conf = "/etc/nginx/sites/{$domain}";
    $nginx_template = "/etc/nginx/template/site_conf";
    $apache_conf = "/etc/httpd/conf/httpd.conf";

    if (file_exists($path)) exit("Domain Existed!\n");
    mkdir($path, 0700);

    if (file_exists($nginx_conf)) exit("Nginx Config file existed!\n");
    $conf_str = file_get_contents($nginx_template);

    // 變量插值:模板中 {$domain}、{$path} 將被替換
    eval("\$conf_str = \"$conf_str\";");

    $success = file_put_contents($nginx_conf, $conf_str);
    if (!$success) exit("Write Config File Failed!");
    echo "Create Vhost success!\n";
    goto start;
}
elseif ($operate == 2) { // 刪除
    $confs_dir = dir("/etc/nginx/sites");
    $confs_list = [];
    $count = 0;
    while (false !== ($conf_file = $confs_dir->read())) {
        if (in_array($conf_file, ['.', '..'])) continue;
        if (is_file($confs_dir->path . "/$conf_file")) {
            $confs_list[$count++] = $conf_file;
        }
    }
    echo "Select a site by number which to delete:\n";
    if (count($confs_list) > 0) {
        foreach ($confs_list as $k => $v) echo "{$k}. $v\n";
    }
    $index = trim(fgets(STDIN));
    if (in_array($index, array_keys($confs_list))) {
        $target = $confs_list[$index];
        copy($confs_dir->path . "/$target", "/etc/nginx/backup/{$target}");
        unlink($confs_dir->path . "/$target");
        exec("tar -zcf /home/sites/{$target}.tar.gz /home/sites/{$target}");
        exec("rm -Rf /home/sites/{$target}");
        echo "Deleted & backed up: {$target}\n";
    }
    goto start;
}
elseif ($operate == 3) {
    exit;
}
else {
    exit("No Operation Selected!");
}

Nginx 模板 /etc/nginx/template/site_conf

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 { log_not_found off; access_log off; }
    location = /robots.txt { allow all; log_not_found off; access_log off; }
    location = / { try_files @proxy; }
    location / {
        index index.htm index.html index.php;
        try_files $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 {
        index index.htm index.html index.php;
        proxy_pass https://127.0.0.1:81;
        include /etc/nginx/proxy.conf;
    }
}

關鍵技術點

  • CLI 交互:fwrite(STDOUT, ...)、fgets(STDIN) 實現菜單交互。
  • 模板引擎:eval() 實現簡單變量插值(生產建議用成熟模板引擎)。
  • 原子操作:創建前檢查存在性、刪除前備份配置與站點文件打包。
  • 權限控制:目錄 0700、配置文件由 root 管理,腳本需以足夠權限運行。

將腳本放入 /usr/local/bin/ 並 chmod +x,即可隨時輸入 vhost_manager 管理虛擬主機。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved