首页 > 编程技术 > php

php管理nginx虚拟主机shell脚本

发布时间:2016-11-25 15:38

下文整理了一段php管理nginx虚拟主机shell脚本,希望这段脚本能帮助到各位同学哦。

使用php作为shell脚本是一件很方便的事情。理所当然,我们可以使用php脚本来管理 nginx虚拟主机,下面是笔者的 脚本 文件供各位参考

 代码如下 复制代码

#!/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 = "";
 
//变量初始化
 
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 );
}
 
//目录检测及配置文件拷贝
 
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;
//写入配置文件
}
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]);
    }
    //删除指定配置,并保存备份
}
else if( $operate == 3 ) {
    exit;
}
else {
 exit ("No Operation Selected!");
}?>

下面是nginx的配置模版

 代码如下 复制代码

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的访问
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;
}

#匹配图片、脚本文件等
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;
}
}

下面来看一个关于顺丰BSP订单号查询接口SOAP的请求示例,这个例子能帮助到各位同学哦。

在一些接口的设计中有时用到了SOAP方式的请求,比如顺丰BSP的接口。
我尝试不同方式的POST方式请求,要么完全请求不到东西,要么接收到乱码,均以失败告终。

针对顺丰接口的SOAP请求的核心代码分享如下:

 代码如下复制代码

/**
* 发送soap请求
*/
private function _soap()
{
$client = new SoapClient($this->serviceUrl);
$info = new stdClass();
$info->arg0 = $this->xml;
$param = array($info);
$response = $client->__call(“sfexpressService”,$param);
return $response->return ;
}

/**
* 拼接出请求的xml
*/
public function createXml()
{
$xml = ‘
’.$this->user.’,’.$this->passWord.’

invoice_no.’” />

’;
$this->xml = $xml;
}

提示:
1.$this->serviceUrl 顺丰分配的BSP请求地址,注意是?wsdl的地址
2.$this->user,$this->passWord 即是顺丰分配的ID和校验码
3.返回的return是一个XML的String,你可能需要simplexml_load_string来解析。

 

原文来自:http://www.jxm.cc/blogs/498.html

清除缓存我们只需要设置页面为no-cache就可以了,当然像asp,php这种只需要设置Expires操作即可,具体如下。
 代码如下 复制代码


HTML网页
<META HTTP-EQUIV="pragma" CONTENT="no-cache">
<META HTTP-EQUIV="Cache-Control" CONTENT="no-cache, must-revalidate">
<META HTTP-EQUIV="expires" CONTENT="Wed, 26 Feb 1997 08:21:57 GMT">
或者<META HTTP-EQUIV="expires" CONTENT="0">

ASP网页
Response.Expires = -1
Response.ExpiresAbsolute = Now() - 1
Response.cachecontrol = "no-cache"

PHP网页
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
header("Cache-Control: no-cache, must-revalidate");
header("Pragma: no-cache");

JSP网页
response.setHeader("Pragma", "No-cache");
response.setHeader("Cache-Control", "no-cache");
response.setDateHeader("Expires", 1);

树形菜单是很多应用中会碰到的一个算法,下文小编就为大家介绍php树形菜单实现方法。

数据库结果

 

php多级树形菜单代码


打印如下:

 代码如下 复制代码
array(7) {
  [0] => array(5) {
    ["id"] => string(1) "4"
    ["fzname"] => string(12) "山猫小号"
    ["userid"] => string(2) "12"
    ["pid"] => string(1) "0"
    ["time"] => string(10) "1413359977"
  }
  [1] => array(5) {
    ["id"] => string(1) "5"
    ["fzname"] => string(12) "山猫一组"
    ["userid"] => string(2) "12"
    ["pid"] => string(1) "4"
    ["time"] => string(10) "1413361206"
  }
  [2] => array(5) {
    ["id"] => string(1) "6"
    ["fzname"] => string(12) "山猫二组"
    ["userid"] => string(2) "12"
    ["pid"] => string(1) "4"
    ["time"] => string(10) "1413361225"
  }
  [3] => array(5) {
    ["id"] => string(1) "7"
    ["fzname"] => string(15) "山猫一组子"
    ["userid"] => string(2) "12"
    ["pid"] => string(1) "5"
    ["time"] => string(10) "1413361388"
  }
  [4] => array(5) {
    ["id"] => string(1) "8"
    ["fzname"] => string(15) "山猫一组子"
    ["userid"] => string(2) "12"
    ["pid"] => string(1) "5"
    ["time"] => string(10) "1413361409"
  }
  [5] => array(5) {
    ["id"] => string(1) "9"
    ["fzname"] => string(16) "山猫一组子3"
    ["userid"] => string(2) "12"
    ["pid"] => string(1) "5"
    ["time"] => string(10) "1413361460"
  }
  [6] => array(5) {
    ["id"] => string(2) "10"
    ["fzname"] => string(15) "山猫二组子"
    ["userid"] => string(2) "12"
    ["pid"] => string(1) "6"
    ["time"] => string(10) "1413361506"
  }
}

结果:

 

php多级树形菜单代码


 打印如下:

 代码如下 复制代码
array(1) {
  [0] => array(5) {
    ["id"] => string(1) "4"
    ["fzname"] => string(12) "山猫小号"
    ["userid"] => string(2) "12"
    ["pid"] => array(2) {
      [0] => array(5) {
        ["id"] => string(1) "5"
        ["fzname"] => string(12) "山猫一组"
        ["userid"] => string(2) "12"
        ["pid"] => array(3) {
          [0] => array(5) {
            ["id"] => string(1) "7"
            ["fzname"] => string(15) "山猫一组子"
            ["userid"] => string(2) "12"
            ["pid"] => string(0) ""
            ["time"] => string(10) "1413361388"
          }
          [1] => array(5) {
            ["id"] => string(1) "8"
            ["fzname"] => string(15) "山猫一组子"
            ["userid"] => string(2) "12"
            ["pid"] => string(0) ""
            ["time"] => string(10) "1413361409"
          }
          [2] => array(5) {
            ["id"] => string(1) "9"
            ["fzname"] => string(16) "山猫一组子3"
            ["userid"] => string(2) "12"
            ["pid"] => string(0) ""
            ["time"] => string(10) "1413361460"
          }
        }
        ["time"] => string(10) "1413361206"
      }
      [1] => array(5) {
        ["id"] => string(1) "6"
        ["fzname"] => string(12) "山猫二组"
        ["userid"] => string(2) "12"
        ["pid"] => array(1) {
          [0] => array(5) {
            ["id"] => string(2) "10"
            ["fzname"] => string(15) "山猫二组子"
            ["userid"] => string(2) "12"
            ["pid"] => string(0) ""
            ["time"] => string(10) "1413361506"
          }
        }
        ["time"] => string(10) "1413361225"
      }
    }
    ["time"] => string(10) "1413359977"
  }
}

工用到两个函数1生成树形代码:

 代码如下 复制代码
function getTree($data, $pId)
{
$tree = '';
foreach($data as $k => $v)
{
   if($v['pid'] == $pId)
   {
    $v['pid'] = $this->getTree($data, $v['id']);
    $tree[] = $v;
    //unset($data[$k]);
   }
}
return $tree;
}

函数2转化为html代码:

 代码如下 复制代码
function procHtml($tree)
{
$html = '';
foreach($tree as $t)
{
   if($t['pid'] == '')
   {
    $html .= "<li>{$t['fzname']}</li>";
   }
   else
   {
    $html .= "<li>".$t['fzname'];
    $html .= $this->procHtml($t['pid']);
    $html = $html."</li>";
   }
}
return $html ? '<ul>'.$html.'</ul>' : $html ;
}
在php中删除文件最简单的办法直接使用unlink命令,但本文章介绍的是需要删除指定目录下的指定文件,所以我需要遍历删除了,具体看这个例子。
 代码如下 复制代码


<?php

 //删除指定文件夹下的非法文件

 function my_del($dir)
 {
    if(is_dir($dir)){
        //打开指定文件夹
        if($handle = opendir($dir))
        {
            while(false !== ($file = readdir($handle)))
            {
                if($file !== '.' && $file !== '..')
                 {
                        my_del($dir.'/'.$file);
                 }
            }
            $res = closedir($handle);
        }
    }else{
        //删掉除图片意外的所有文件
        $avatararr = array('180x180.jpg', '30x30.jpg', '45x45.jpg', '90x90.jpg');
        $ex = explode('/', $dir);
        $endex = end($ex);
        if((strripos($endex,'.jpg') === false) || (substr($endex, -4) != '.jpg')){
                    //按名称过滤
                @unlink($dir);
        } else {
                    //按文件实质内容过滤
                $info = @getimagesize($dir);
                if(!$info || $info[2] !=2) {
                        @unlink($dir);
                }
        }
    }
 }

 $dir='D:/xampp/htdocs/www/avatar001/12/47/';
 my_del($dir);

标签:[!--infotagslink--]

您可能感兴趣的文章: