在CentOS 6上安装Kdump

最近把扔在客厅的下载机从Ubuntu换成了centos,每天都会出现kernel panic,为了调试,我选择了Kdump来导出核心vmcore,在看了N多网上说的安装Kdump方法无果之后,果断GOOGLE搜索,终于找到一个可以用的方法。

一.安装kexec-tools

[root@CentOS ~]# yum install kexec-tools

二.添加 crashkernel=64M 在grub.conf中
vi /boot/grub/grub.conf

kernel /vmlinuz-2.6.32-279.14.1.el6.i686 ro root=/dev/mapper/VolGroup-lv_root rd_NO_LUKS rd_NO_MD rd_LVM_LV=VolGroup/lv_swap crashkernel=auto LANG=zh_CN.UTF-8 rd_LVM_LV=VolGroup/lv_root KEYBOARDTYPE=pc KEYTABLE=us rd_NO_DM rhgb quiet crashkernel=64M

然后重启电脑,待重启后使用free -m会发现总内存比添加之前少了64M。

三.设置Kdump导出的vmcore保存位置。

[root@CentOS ~]# df
文件系统                 1K-块      已用      可用 已用% 挂载点
/dev/mapper/VolGroup-lv_root
                      51606140   1883516  47101184   4% /
tmpfs                   477572         0    477572   0% /dev/shm
/dev/sda1               495844     53469    416775  12% /boot
/dev/mapper/VolGroup-lv_home
                     1868714292 197403328 1576385756  12% /home

mkdir /var/crash
vi /etc/kdump.conf 添加

ext4 /dev/mapper/Volume00-var

四.启动Kdump服务。

[root@CentOS ~]# service kdump start
Starting kdump:                                            [确定]

五.测试Kdump运行

echo 1 > /proc/sys/kernel/sysrq
echo c > /proc/sysrq-trigger

这时系统会马上panic,重启后检查/var/crash 会发现目录中有类似于127.0.0.1-2012-12-17-21:53:58目录生成,则代表Kdump运行正常。

参考:http://linuxczar.net/archives/383

php从一组数字(字符)随机选择一个数字或者一串字符

转自:http://www.blogguy.cn/show-376-1.html

第一个函数: 随机选择一串数字或者字符中的一个。

function GetID(){         
 $characters = array("99","149","150","151","145","146","147","141","142","143","137","138","139","133","134","135","129","130","131","125","126","127","96","97","98","105","106","107","108","109","110","111","112","115","116","117","118","119","120","121","122","123","103","102","100","153","154","155");   
 shuffle($characters);   
 $ReqID = $characters[mt_rand(0, count($characters))];   
 return $ReqID;   
  }         
  
  $ReqID .= GetID();   
  
  echo $ReqID;    

第二个函数:指定字符,返回指定长度的随机串

function code($nc, $a='abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789') {   
   $l=strlen($a)-1; $r='';   
   while($nc-->0) $r.=$a{mt_rand(0,$l)};   
   return $r;   
}   
echo(code(10));  

第三个函数:指定字符返回8位随机串

$acceptedChars = 'azertyuiopqsdfghjklmwxcvbnAZERTYUIOPQSDFGHJKLMWXCVBN0123456789';    
$max = strlen($acceptedChars)-1;   
$password = null;   
for($i=0; $i < 8; $i++) {    
 $password .= $acceptedChars{mt_rand(0, $max)};    
}   
echo $password."

";

第四个函数:指定字符,返回序列号格式的字符串

function GetID($x){         
  
    $characters = array("A", "B", "C", "D", "E", "F", "G", "H", "J", "K", "L", "M", "N", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "2", "3", "4", "5", "6", "7", "8", "9");   
    shuffle($characters);   
  
    for (; strlen($ReqID)<$x;){   
      $ReqID .= $characters[mt_rand(0, count($characters))];   
    }   
  
    return $ReqID;   
       
  }         
  
  
  $ReqID .= GetID(3);   
  $ReqID .= "-";   
  $ReqID .= GetID(4);   
  $ReqID .= "-";   
  $ReqID .= GetID(3);   
  
  echo $ReqID 

【转】Chrome去除干扰码插件,简单快捷!

来自:http://www.ra1nker.com/99.html

清除隐藏文字的脚本,方便拷贝一些经过特别处理过的网页文字,快捷键alt+q
将以下代码保存为cleanhidemod.user.js。再增加插件

// ==UserScript==
// @name ra1nker.com// @match http://*/*
// ==/UserScript==
var debug=false; //debug or release switch!!
var CleanHideObj=new CleanHideClass();
function CleanHideClass()
{
//functions;id mean cleanhid ,AppID mean call from firefox or mozilla (different! call from overlayFF or overlayMZ!)
this.on=function(id,bMZ)
{
//this.strBundle = document.getElementById("cleanhide-strings"); //get the strings use in js!
if(id == 0)
{
var page=/* document.getElementById("content").contentDocument */document; //main page
this.RemoveHide(page,"span");
this.RemoveHide(page,"font");
this.RemoveHide(page,"div");
this.RemoveHide(page,"p");
//alert(this.strBundle.getString("CleanHideSuccess"));
//document.getElementById("statusbar-display").label=this.strBundle.getString("CleanHideSuccess");
}}
this.getobjstyle=function(node,prop)
{
try
{
return node.ownerDocument.defaultView.getComputedStyle(node, "").getPropertyValue(prop);
}
catch(e)
{
if(debug)
{
alert(e.message);
}return "200";
}
}
this.RemoveHide=function(page,objname)
{
try
{
var minValue=25;
var objs=page.getElementsByTagName(objname); //get all element

if(objs.length == 0) //no span or font object found!
{
if(debug)
{
alert("no object found!:"+objname);
}
}
else
{
//determean every object!
for(var i=objs.length-1;i>=0;i--) //must from last to first!!! for detect every element!!
{
var node=objs.item(i); //get one element

//if this object is invisible,we delete it!!
if(this.getobjstyle(node,"display") == "none" ||
this.getobjstyle(node,"visibility") =="hidden" ||
parseInt(this.getobjstyle(node,"font-size")) == 0) //if this element'style is none,than remove it!!
{
if(debug)
{
alert("remove invisiable!");
}
node.parentNode.removeChild(node);

}
else //normal object!have color!
{
if(node.parentNode) //if it has parent node!!
{
var orgNode=node;
var orgColor=this.getobjstyle(orgNode,"color");
var orgRGB=getRGB(orgColor); //get org node's RGB only color.
var parentNode=node.parentNode
var parentBgColor=this.getobjstyle(parentNode,"background-color");

var isLast=false;
regtest=/rgba\((\s)*0,(\s)*0,(\s)*0,(\s)*0\)/;
while(regtest.test(parentBgColor)) //parent bg is transparent!!,get last no transparent node!!
{

if(parentNode.parentNode == null)
{
isLast=true;
break;
}
parentNode=parentNode.parentNode;
parentBgColor=this.getobjstyle(parentNode,"background-color");
parentBgColor=parentBgColor+"";
if(!regtest.test(parentBgColor))
{

break;
}
}
if(isLast) //body is RGB(255,255,255)!!!
{
if(debug)
{
alert("last!!");
}
if((Math.abs(orgRGB[0]-255)<=minValue)&&(Math.abs(orgRGB[1]-255)<=minValue)&&(Math.abs(orgRGB[2]-255)<=minValue))
{
orgNode.parentNode.removeChild(orgNode); //color is near white,we delete it!
}
}
else
{
if(debug)
{
alert("compare color and parent bgcolor!");
}
parentBgRGB=getRGB(parentBgColor);
if((Math.abs(orgRGB[0]-parentBgRGB[0])<=minValue)&&(Math.abs(orgRGB[1]-parentBgRGB[1])<=minValue)&&(Math.abs(orgRGB[2]-parentBgRGB[2])<=minValue))
{
orgNode.parentNode.removeChild(orgNode); //color is near white,we delete it!
}
}
}//end of hava parent
}//if invisible
}//for ... every object
}//object lentgth == 0

//find sub frames
var itemFrames = page.getElementsByTagName("frame");
var itemiFrames = page.getElementsByTagName("iframe");

var frame, iframe;
if (itemFrames.length > 0)
{
for (var i = 0 ; i< itemFrames.length ; i++)
{
frame = itemFrames[i].contentDocument;
this.RemoveHide(frame,objname); // recursion for frames
}
}
if (itemiFrames.length > 0)
{
for (var i = 0 ; i < itemiFrames.length ; i++)
{
iframe = itemiFrames[i].contentDocument;
this.RemoveHide(iframe,objname); // recursion for iframes
}
}
}
catch(e)
{
if(debug)
{
alert(e.message);
}
}
}

}
//this function for get R G B data from string rgb(r,g,b) or blue,etc
function getRGB(colorString)
{
var RGB = new Array;
var tempSting=colorString.substring(4,colorString.length-1);
var tempArray=tempSting.split(",");

RGB[0]=parseInt(tempArray[0]);
RGB[1]=parseInt(tempArray[1]);
RGB[2]=parseInt(tempArray[2]);

return RGB;
}
function keys(evt)
{ //var evt=evt?evt:(window.event?window.event:null);

if(evt.keyCode == 81 && !evt.shiftKey && !evt.ctrlKey && evt.altKey)
{
CleanHideObj.on(0,1);
alert("Ra1nker:成功去除干扰码");
}

}
document.addEventListener('keydown', keys, false);

【原创】Nginx Accesskey使用COOKIE作为KEY

网上有一篇文章是介绍使用COOKIE作为ACCESSKEY的KEY值的文章,以下是完整的方案:

NGINX 虚拟主机配置:

server
  {
    listen 80;
    server_name  www.yourdomian.com;
    root  /home/wwwroot/test;

if ( $http_cookie ~* " ng_cookie=(.+?)(?:;|$)")
{
    set $ng_cookie $1;
}

location /down/ {
  accesskey             on;
  accesskey_hashmethod  sha1;
  accesskey_arg         "token";
  accesskey_signature   "jzbk$ng_cookie";

}

location ~ .*\.(php|php5)?$ {
include php.conf;
        }

}

PHP部分:

0) $r.=$a{mt_rand(0,$l)}; 
   return $r; 
}
$key=code('32');//生成一个32位的随机值

$File='pure-ftpd'; //文件名

if(!isset($_COOKIE['ng_cookie'])) {
setcookie("ng_cookie",$key,time()+1800); //设置COOKIE有效期半小时
header("Refresh: 0"); //刷新页面
}

$token=sha1(jzbk.$_COOKIE['ng_cookie']); //KEY

echo '
'; echo "http://www.yourdomian.com/down/$File?token=$token"; //下载地址 echo '
'; echo $_COOKIE['ng_cookie']; ?>

FreeBSD开机自动同步时间。

最近把VPS系统换成了FreeBSD,发现每次重启之后时间都会不正确,使用以下的方法可以让FreeBSD在开机的时候自动使用NTP来校对时间。

编辑 /etc/rc.conf 加入以下代码:

ntpdate_enable="YES" # Run ntpdate to sync time on boot (or NO). 
ntpdate_program="ntpdate" # path to ntpdate, if you want a different one. 
ntpdate_flags="us.pool.ntp.org" # Flags to ntpdate (if enabled). 

WordPress前台不加载自带jQuery,去版本号等信息

现在很多WordPress主题和插件都使用jQuery,而且一般会直接调用Google的jQuery库,减轻我们服务器的负担,但WordPress本身会加载jQuery,这样就造成了重复载入,如下图所示:

那么如何让WordPress前台不加载本身jQuery?其实很简单,直接打开/wp-includes/script-loader.php文件,查找“jquery.js”字符串,在第122行能找到这么一句:

	$scripts->add( 'jquery', '/wp-includes/js/jquery/jquery.js', array(), '1.7.2' );

由于在后台必须得用到这个库,我们不能将这一句直接删除或者注释掉。那么加个判断,让这一句只在后台的时候运行就信了,将其改为:

	if(is_admin()){
	$scripts->add( 'jquery', '/wp-includes/js/jquery/jquery.js', array(), '1.7.2' );}

这样就OK啦!

注:由于直接修改了WordPress核心文件,如果你以后进行版本升级,记得查看这次修改是否被覆盖!

去掉版本号等信息

WordPress默认会加载wp_head,里面包含了版本号在内的很多信息,如下图所示:

如何去除这些多余的信息呢?也许你会说直接删除,但是你安装的插件一般都需要wp_head来加载插件必须的css和JS文件,所以我们不能直接删除。只要在主题根目录下的function.php中加入如下代码:

//移除顶部多余信息
function wpbeginner_remove_version() { 
return ; 
} add_filter('the_generator', 'wpbeginner_remove_version');//wordpress的版本号
remove_action('wp_head', 'feed_links', 2);// 包含文章和评论的feed 
remove_action('wp_head', 'index_rel_link');//当前文章的索引 
remove_action('wp_head', 'feed_links_extra', 3);// 额外的feed,例如category, tag页 
remove_action('wp_head', 'start_post_rel_link', 10, 0);// 开始篇 
remove_action('wp_head', 'parent_post_rel_link', 10, 0);// 父篇 
remove_action('wp_head', 'adjacent_posts_rel_link', 10, 0); // 上、下篇. 
remove_action('wp_head', 'adjacent_posts_rel_link_wp_head', 10, 0 );//rel=pre
remove_action('wp_head', 'wp_shortlink_wp_head', 10, 0 );//rel=shortlink 
remove_action('wp_head', 'rel_canonical' ); 
remove_action('wp_head', 'wlwmanifest_link'); // 外部编辑器
remove_action( 'wp_head','rsd_link');//ML-RPC

P.S:大部分内容来自:http://www.cmhello.com/remove-wordpress-jquery-and-other-information.html

使用SAE的jQuery节约又拍流量

使用又拍云加速BLOG几天了,在又拍后台的日志里看到,每日使用流量最多的就是jquery,为了节约又拍的流量,并且更好的加速,我们把jquery换成SAE的链接,方法如下。

P.S:如果你和我一样在WP中设置了CDN,我觉得只有方法1适合你!

1.在主题的header.php中找到相关语句改成一下样式:




2.或者是在主题的functions.php添加以下的代码:

//Making jQuery Google API in SAE
function my_scripts_method() {
if (!is_admin()){
wp_deregister_script( 'jquery' );
wp_register_script( 'jquery', 'http://lib.sinaapp.com/js/jquery/1.7.2/jquery.js');
wp_enqueue_script( 'jquery' );
}
}
add_action(‘wp_enqueue_scripts‘, ‘my_scripts_method’);

如果你需要其他版本,可以访问 http://lib.sinaapp.com/?path=/jquery 获取链接。

NGINX下可用的WP super cache规则

最近把BLOG的缓存换成了WP super cache,但是这个插件默认是运行在APACHE下面的,根据各种度娘谷歌后找到了对应的NGINX下的规则。
1.简洁形:

location / {
if (-f $request_filename/index.html){
rewrite (.*) $1/index.html break;
}
if (-f $request_filename/index.php){
rewrite (.*) $1/index.php;
}
if (!-f $request_filename){
rewrite (.*) /index.php;
}
}

2.完整形:

location / 
{ 

set $wp_super_cache_file ''; 
set $wp_super_cache_uri $request_uri;

if ( $request_method = POST ) 
{ 
set $wp_super_cache_uri ''; 
}

if ( $query_string ) 
{ 
set $wp_super_cache_uri ''; 
}

if ( $http_cookie ~* "comment_author_|wordpress|wp-postpass_" ) 
{ 
set $wp_super_cache_uri ''; 
}

if ( $wp_super_cache_uri ~ ^(.+)$ ) 
{ 
set $wp_super_cache_file /wp-content/cache/wp_super_cache/$http_host/$1index.html; 
}

if ( -f $document_root$wp_super_cache_file ) 
{ 
rewrite ^(.*)$ $wp_super_cache_file break; 
}

if (-f $request_filename) 
{ 
expires 30d; 
break; 
}

if (!-e $request_filename) 
{ 
rewrite ^(.+)$ /index.php?q=$1 last; 
} 
}

说实话,对于这两种规则的区别不大搞得清,但是经测试都是可以用的,而且根据查看源码显示的都能正确读取到缓存,具体用哪一种就看自己的喜好了。

[转]WordPress在Nginx中启用Apache的mod_rewrite

其实不是真的可以另到 Nginx 支持 Apache 的 mod_rewrite 的.因为如果你的web服务器是 Nginx ,那么你的 WordPress 就会提示你的服务器环境不支持 mod_rewrite 了,但实际上是支持的,例如安装了 WP Super Cache 这插件就有这样的提示了,现在我们只要添加一行代码就可以关闭这个提示,欺骗WordPress ,让它认为是支持 mod_rewrite 方法是在主题的 functions.php 文件添加下面的代码:

add_filter('got_rewrite', 'nginx_has_rewrites');
function nginx_has_rewrites() {
    return true;
}

另外,因为Nginx运行PHP是用PHP-FPM的,所以有时会导致WordPress认为php没有写权限,表现为无法自动升级,和升级插件等,下面这个代码可以修改这个错误.

add_filter('filesystem_method', 'nginx_make_filesystem_direct');
function nginx_make_filesystem_direct() {
    return 'direct';
}

来自:http://imcat.in/wordpress-on-nginx-apache-mod_rewrite-enabled/

[转载]准确判断PHP上传的文件头

上传文件的功能是各类站点的基本功能,但是网络上也存在一些用心不良的人伪装图片或其他文件,上传一些木马或病毒到服务器上。普通php函数文件类型函数无法正确识别,安全的做法应该是头文件判断:

";
echo "gif:".checkTitle('img/weather.gif')."
"; echo "gif:".checkTitle('img/7768752.gif')."
"; echo "gif:".checkTitle('img/gif001.gif')."
"; echo "gif:".checkTitle('img/gif002.gif')."
"; echo "gif:".checkTitle('img/gif007.gif')."
"; echo "jpeg:".checkTitle('img/weather.jpg')."
"; echo "jpeg:".checkTitle('img/0001.jpg')."
"; echo "jpeg:".checkTitle('img/1.jpg')."
"; echo "jpeg:".checkTitle('img/0002.jpg')."
"; echo "jpeg:".checkTitle('img/4.jpg')."
"; echo "jpeg:".checkTitle('img/5.jpg')."
"; echo "jpeg:".checkTitle('img/71885.jpg')."
"; echo "jpeg:".checkTitle('img/imgad.jpg')."
"; echo "png:".checkTitle('img/weather.png')."
"; echo "png:".checkTitle('img/1.png')."
"; echo "png:".checkTitle('img/2.png')."
"; echo "png:".checkTitle('img/3.png')."
"; echo "png:".checkTitle('img/4.png')."
"; echo "png:".checkTitle('img/5.png')."
"; echo hexdec(89)."
"; echo hexdec(50)."
"; //6072,102119 ?>