It is convenient to use PHP as a shell script. As a matter of course, we can use PHP script to manage nginx virtual host, the following is the author's script file for you to reference
Code to copy code as follows
#!/usr/bin/php-q
<?php
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";
$conf _str = "";
Variable initialization
if (file_exists ($path)) exit ("Domain existed!\n");
else mkdir ($path, 0700);
if (file_exists ($nginx _conf)) exit ("Nginx Config file existed!\n");
else {
$conf _str = file_get_contents ($nginx _template);
}
Directory detection and configuration file copy
Eval ("\ $conf _str =\" $conf _str\ ";");
$succes = file_put_contents ($nginx _conf, $conf _str);
if (! $succes) Exit ("Write Config File fauile!");
else echo "Create Vhost success!\n";
Goto start;
Write a configuration file
}
else if ($operate = = 2) {
$confs _dir = Dir ("/etc/nginx/sites");
$confs _list = Array ();
$count = 0;
while (False!== ($conf _file = $confs _dir->read ())) {
if ($conf _file = = ".") continue;
if ($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)
for (www.111cn.net) each ($confs _list as $k = + $v) {
echo "{$k}. $v \ n ";
}
$index = Trim (fgets (STDIN));
if (In_array ($index, Array_keys ($confs _list))) {
Copy ($confs _dir->path. " /". $confs _list[$index], "/etc/nginx/backup/{$confs _list[$index]}");
Unlink ($confs _dir->path. " /". $confs _list[$index]);
EXEC ("tar-zcf/home/sites/{$confs _list[$index]}.tar.gz/home/sites/". $confs _list[$index]);
EXEC ("rm-rf/home/sites/". $confs _list[$index]);
}
Delete the specified configuration and save the backup
}
else if ($operate = = 3) {
Exit
}
else {
Exit ("No operation Selected!");
}?>
Below is the Nginx configuration template
Code to copy code as follows
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;
}
#不记录对robots. txt access
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;
}
#匹配html
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;
}
#匹配图片, script files, etc.
Location ~*\. (Jpe?g|gif|png|ico|css|js|flv|swf|avi|zip|rar|svg|tgz|gz|rar|bz2|doc|xls|exe|pdf|ppt|txt|tar|mp3) $ {
Expires 30d;
}
#传递给apache
Location @proxy {
Index index.htm index.html index.php;
Proxy_pass http://127.0.0.1:81;
include/etc/nginx/proxy.conf;
}
}
From:http://www.111cn.net/phper/php-cy/68199.htm
PHP Management Nginx Virtual Host shell script