Using PHP as a shell script is a handy thing to do. Of course, we can use PHP script to manage nginx virtual host, the following is the author's script file for your reference
| The code is 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 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)
foreach ($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]);
}
//Deletes the specified configuration and saves the backup
}
else if ($operate = = 3) {
exit;
}
else {
exit ("No Operation selected!");
}?> |
The following is the Nginx configuration template
| code is as follows |
&nbs P; |
|
server {
Listen;
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;
}
Access to #不记录对robots. txt
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_fi Les\ $uri \ $uri//wp-content/cache/supercache/\ $http _host/\ $request _uri/index.html.gz @proxy;
}
#匹配图片, script files, and so on
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;
}
} |