Created
December 30, 2012 09:17
-
-
Save horsley/4411757 to your computer and use it in GitHub Desktop.
lnmp的设置补丁以适应awstats日志统计, 在服务器上chmod +x 之后即可以运行,修改的文件均会被备份
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/php | |
<?php | |
/** | |
* lnmp的设置补丁以适应awstats日志统计 | |
* @author: horsley <imhorsley@qq.com> | |
* | |
* 可用于lnmp和lnmpa,已在0.8和0.9版本上面测试过,默认状态的日志格式有误,少了双引号 | |
*/ | |
error_reporting(E_ALL); | |
//虚拟主机脚本 | |
$vhost_sh = '/root/vhost.sh'; | |
//配置文件 | |
$vhost_conf_dir = array( | |
'/usr/local/nginx/conf/vhost', | |
'/usr/local/apache/conf/vhost' | |
); | |
$nginx_conf = '/usr/local/nginx/conf/nginx.conf'; | |
//日志文件 @todo: 现有日志文件的格式修正? | |
$nginx_logs = '/home/wwwlogs'; //*.log | |
$apache_logs = '/usr/local/apache/logs'; //*_log | |
//主机脚本里面替换 | |
$old_str = array('"\$request"', '"\$http_referer"', '"\$http_user_agent"', '\$http_x_forwarded_for\''); //forward这一个特殊处理 不会被重复替换 | |
$new_str = array('\"\$request\"', '\"\$http_referer\"', '\"\$http_user_agent\"', '\"\$http_x_forwarded_for\"\''); | |
//已有配置文件里面的替换 | |
$old_conf = array(' $request ', ' $http_referer ', '$http_user_agent ', ' $http_x_forwarded_for'); | |
$new_conf = array(' "$request" ', ' "$http_referer" ', '"$http_user_agent" ', ' "$http_x_forwarded_for"'); | |
///////////////////////////////////////////////////////////////////////// | |
echo 'Modifying vhost.sh ...'; | |
do_replacement($vhost_sh, $old_str, $new_str); | |
echo "Completed.\n"; | |
echo 'Modifying nginx.conf ...'; | |
do_replacement($nginx_conf, $old_conf, $new_conf); | |
echo "Completed.\n"; | |
foreach ($vhost_conf_dir as $dir) { | |
if (!file_exists($dir)) continue; //only lnmp | |
echo '>Enter Config dir ' . $dir . "\n"; | |
$confs = scandira($dir); | |
foreach ($confs as $conf_file) { | |
if (substr($conf_file, -5) != '.conf') continue; //保护其他文件,包括备份文件 | |
echo "Modifying $conf_file..."; | |
do_replacement($dir . '/' . $conf_file, $old_conf, $new_conf); | |
echo "Completed.\n"; | |
} | |
} | |
function do_replacement($filename, $old_str, $new_str) { | |
//echo $filename; | |
$tmp = file_get_contents($filename); | |
$tmp = str_replace($old_str, $new_str, $tmp, $count); | |
if ($count > 0) { | |
copy($filename, $filename . '.bak'); | |
file_put_contents($filename, $tmp); | |
} else { | |
echo 'target string not found. '; | |
} | |
} | |
/////////////////////////////////////////////////////////////////////////// | |
//lnmp套装默认禁用了scandir | |
function scandira($dir, $sortorder = 0) { | |
if(is_dir($dir)) { | |
$dirlist = opendir($dir); | |
while( ($file = readdir($dirlist)) !== false) { | |
if(!is_dir($file)) { | |
$files[] = $file; | |
} | |
} | |
($sortorder == 0) ? asort($files) : rsort($files); | |
return $files; | |
} else { | |
return FALSE; | |
break; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment