PHP Management Nginx Virtual Host shell script instance _php tips

Source: Internet
Author: User
Tags file copy php script rar stdin

The example of this article tells the PHP management Nginx Virtual Host shell script, share for everyone reference. The specific analysis is as follows:

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:

Copy Code 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 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
Copy Code 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;
}
}

I hope this article will help you with your PHP program design.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.