appserv本地开发环境,经常需要添加多站点,手动操作很麻烦,于是创建控制台操作

#!/usr/bin/env php
<?php
//apache多站点配置
$apache_config ="E:/AppServ/Apache24/conf/extra/httpd-vhosts.conf";
//本地从定向
$host_config ="C:/Windows/System32/drivers/etc/hosts";
//appserv根目录
$www ="E:/AppServ/www";
//apache 重启
$apache_Restart ="E:\AppServ\Apache24\bin\httpd.exe -k restart -n Apache24";
//域名
$domain_suffix ='myweb.com';

//apache多站点模板
$str_apache = <<<EOD
<VirtualHost *:80>
    ServerAdmin [email protected]
    DocumentRoot "E:/AppServ/www/{dirname}"
    ServerName {domain_suffix}
    ServerAlias {hostname}.{domain_suffix}
    ErrorLog "logs/dummy-host2.example.com-error.log"
    CustomLog "logs/dummy-host2.example.com-access.log" common
</VirtualHost>
EOD;
//主机模板
$str_host =<<<EOD
127.0.0.1   {hostname}.{domain_suffix}
EOD;


// +----------------------------------------------------------------------
// / 程序执行流程
// +----------------------------------------------------------------------
echo "\n  AppServ新建项目工具 v1.0\n\n";
echo "\n请选择模式 [0-1, or \"q\" to 退出] ";
echo "\n  0:新增模式";
echo "\n  1:删除模式";
echo "\n  :";
$answer = trim(fgets(STDIN));
if($answer ==0){
    echo "\n请输入项目名称:";
    $dirname = strtolower(trim(fgets(STDIN)));
    $project_dir =$www .'/'.$dirname;
    echo "\n请输入主机名称:";
    $hostname = strtolower(trim(fgets(STDIN)));
    echo "\n请输入运行目录名称:";
    $runDir = strtolower(trim(fgets(STDIN)));
    if(!is_dir($project_dir)){
        mkdir($project_dir,0777);
    }
    if(!empty($runDir)){
        $dirname .='/'.$runDir;
        echo $project_dir.'/'.$runDir;
        if(!is_dir($project_dir.'/'.$runDir)){
            mkdir($project_dir.'/'.$runDir,0777);
        }
        
    }
    
    
    
    if(is_config($apache_config,
        "ServerAlias $hostname'.'$domain_suffix") === false){
        $config = config_replace($str_apache,[
            'dirname'=>$dirname,
            'domain_suffix'=>$domain_suffix,
            'hostname'=>$hostname,
        ]);
        file_add($apache_config, $config);
        echo "\n创建apache站点成功";
        file_add($host_config,"\n".config_replace($str_host,['domain_suffix'=>$domain_suffix,'hostname'=>$hostname]));
        echo "\n创建本地重定向成功";
        exec($apache_Restart);
        echo "\napache重启成功";
        die();
    }
    

}elseif($answer ==1){
    echo "\n请输入主机名称:";
    $hostname = strtolower(trim(fgets(STDIN)));
    $domain =$hostname.".".$domain_suffix ;

    if(is_config($apache_config,$domain)=== false){
        echo "\n没有找到站点";
    }else{
        $config = file_get_contents($apache_config );
        preg_match_all("/<\VirtualHost \*:80>[^<]*<\/\VirtualHost>/",$config ,$hosts);
        foreach($hosts[0] as $str){
            $s = get_config($str);
            if( isset($s['ServerAlias']) and  $s['ServerAlias'] ==$domain){
                //更新apache配置
                file_del($apache_config,$str);
                echo "\n删除apache站点成功";
                //更新重定向配置
                file_del($host_config,"\n127.0.0.1   ".$domain);
                echo "\n删除本地重定向成功";
                die();
                
            }
        }
    
    }
    
    
}else{
    echo "\n退出初始化.\n";
    exit(0);
}


// +----------------------------------------------------------------------
// / 自定义函数
// +----------------------------------------------------------------------
//新增配置
function file_add($file,$config){

    file_put_contents($file,$config,FILE_APPEND);

}

//删除配置文件
function file_del($file,$config){
    $str = file_get_contents($file);
    $str = str_replace($config,'',$str);
    file_put_contents($file,$str);
}

//判断配置是否存在
function is_config($file,$config){
    $str = file_get_contents($file);
    return strpos($str,$config);
}

//字符串替换
function config_replace($template,$data=[]){
    foreach($data as $key=>$value){
        $template = str_replace("{".$key."}",$value,$template);   
    }
    return $template;
}
//获取站点配置
function get_config($str){

    if(strpos($str,"ServerAlias")){
        $str =str_replace("\r",'',$str);
        $str =str_replace("\"",'',$str);
        $data =  explode(chr(10),$str);
        $arr =[];
        for($i=1;$i<=4;$i++){
            $r= array_filter(explode(' ', $data[$i]));
            $arr[current( $r)]=next($r);
        }
        return $arr;
   }
   
   
    
   
   
}
This entry was posted in 未分类. Bookmark the permalink.

发表回复