This article mainly introduces the implementation of php's shell script for managing nginx virtual hosts, and describes how to manage nginx virtual hosts using PHP scripts in the form of instances, which has good reference value, for more information, see
This article mainly introduces the implementation of php's shell script for managing nginx virtual hosts, and describes how to manage nginx virtual hosts using PHP scripts in the form of instances, which has good reference value, for more information, see
This example describes how to use php to manage nginx virtual host shell scripts. The specific analysis is as follows:
Using php as a shell script is very convenient. Of course, we can use the php script to manage the 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 the 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]);
}
// Delete the specified configuration and save the backup
}
Else if ($ operate = 3 ){
Exit;
}
Else {
Exit ("No Operation Selected! ");
}
?>
The following is the nginx configuration template.
The Code is 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 };
# Access to site icons is not recorded
Location =/favicon. ico {
Log_not_found off;
Access_log off;
}
Robot does not Log 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;
}
# Matching html
Location ~ * \. (Html | htm) $ {
Expires 30 s;
Gzip off;
Add_header Content-Encoding gzip;
Try_files \ $ uri // wp-content/cache/supercache/\ $ http_host/\ $ request_uri/index.html.gz @ proxy;
}
# Matching images and script files
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;
}
# Pass to apache
Location @ proxy {
Index index.htm index.html index. php;
Proxy_pass: 81;
Include/etc/nginx/proxy. conf;
}
}
I hope this article will help you with php programming.