Author:


Bash Shell中逗号操作符示例

Posted by – 2010-09-03

#!/bin/bash
#     The comma operator links together a series of arithmetic operations.
#+    All are evaluated, but only the last one is returned. 

# Set "a = 9" and "t2 = 15 / 3"
let "t2 = ((a = 9, 15 / 3))"
echo $t2.$a

# The comma  operator can also concatenate strings.

for file in /{,usr/}sbin/ifconfig
#             ^    Find all executable files ending in "ifconfig"
#+                 in /bin and /usr/bin directories.
do
        if [ -x "$file" ]
        then
          echo $file
        fi
done

引自:《Advanced Bash-Scripting Guide》

Nginx下配置Cache-Control头

Posted by – 2010-09-01

HTTP协议的Cache -Control指定请求和响应遵循的缓存机制。
在请求消息或响应消息中设置 Cache-Control并不会影响另一个消息处理过程中的缓存处理过程。
请求时的缓存指令包括no-cache、no-store、max-age、 max-stale、min-fresh、only-if-cached等。
响应消息中的指令包括public、private、no-cache、no- store、no-transform、must-revalidate、proxy-revalidate、max-age。

Nginx的ngx_http_headers_module模块可以对Cache-Control头相关的东西进行配置
例如:

      # 相关页面设置Cache-Control头信息

      if ($request_uri ~* "^/$|^/search/.+/|^/company/.+/") {
        add_header    Cache-Control  max-age=3600;
      }

      if ($request_uri ~* "^/search-suggest/|^/categories/") {
        add_header    Cache-Control  max-age=86400;
      }

个人理解的max-age意思是:客户端本地的缓存,在配置的生存时间内的,客户端可以直接使用,超出生存时间的,到服务器上取新数据。当然这些还要看客户端浏览器的设置。
如有偏颇,欢迎指正。非常感谢!

CentOS安装视频教程

Posted by – 2010-08-27

闲来无事,做了个CentOS的安装教程,主要用于服务器安装。传到youku了,这样就不受天朝网速影响了。。

Windows下监控应用程序

Posted by – 2010-08-23

从来没想到有一天我需要写批处理。
功能很简单,监控应用程序search的三个进程,如果进程不在了就重启该进程,因为启动后会有多个cmd窗口,所以最后把多余的cmd窗口用taskkill关掉。
这个批处理结合windows自带的计划任务,几分钟执行一次就可以了

@echo off
CD\
D:
CD D:\[Search]\Search1\
tasklist | find /i “Search.1″ || start start.bat

CD D:\[Search]\Search2\
tasklist | find /i “Search.2″ || start start.bat

CD D:\[Search]\Search3\
tasklist | find /i “Search.3″ || start start.bat

tasklist | find /i “cmd” && taskkill /f /im cmd.exe
echo off

Memcached自动安装脚本

Posted by – 2010-07-22

今天写了一个小脚本。两点收获:
一、只要稍做修改就可以自动下载最新的开源软件包,像nginx就可以,难点在于得到下载用的url。这里采用的是elinks和wget。
二、用tar tf加上head命令就可以得到解压后的目录,没想到这么简单,大部分情况都是适用的,除非是有些变态软件解压后没建目录,直接解压到当前目录的那种。
脚本贴出,如下:

#!/bin/bash
# Memcached Auto Install
# Author: 楚霏
# Company: chengyongxu.com
# Create Date: 2010-07-22
# Last Update Date: 2010-07-22 17:49
#########################################################
#
# 在这一部分定义相关变量
#
#########################################################
# 下载链接地地址
LATEST_MEMCACHED_LINK=`elinks http://memcached.org | grep tar.gz | grep http | awk {'print $2'}`
LATEST_LIBEVENT_LINK=`elinks http://www.monkey.org/~provos/libevent/ | grep stable.tar.gz$ | awk {'print $2'} | sort -rn | head -n 1`
DOWNLOAD_DIR=/usr/local/src

# 最新的软件包名
REMOTE_MEMCACHED_PACKAGE_NAME=`elinks http://memcached.org | grep tar.gz | grep http | awk {'print $2'} | awk -F/ '{print $5}'`
REMOTE_LIBEVENT_PACKAGE_NAME=`elinks http://www.monkey.org/~provos/libevent/ | grep stable.tar.gz$ | awk {'print $2'} | sort -rn | head -n 1 | awk -F/ '{print $5}'`

#########################################################
#
# 检查系统环境
#
#########################################################
# 检查发行版并安装elinks,wget
DISTRIBUTION=`lsb_release -i | awk '{print $3}'`
case "$DISTRIBUTION" in
*CentOS*|*"Red Hat"*|*Fedora*)
    yum -y install elinks wget
    ;;
*)
    echo 'Do not support this distribution !' && exit 0
    ;;
esac

#########################################################
#
# 下载最新版软件
#
#########################################################
if [ ! -d $DOWNLOAD_DIR ]
then
        mkdir -p $DOWNLOAD_DIR
fi

if [ ! -e $DOWNLOAD_DIR/$REMOTE_MEMCACHED_PACKAGE_NAME ]
then
        wget -c $LATEST_MEMCACHED_LINK -P $DOWNLOAD_DIR
else
        echo "本地已有最新版本"
fi

if [ ! -e $DOWNLOAD_DIR/$REMOTE_LIBEVENT_PACKAGE_NAME ]
then
        wget -c $LATEST_LIBEVENT_LINK -P $DOWNLOAD_DIR
else
        echo "本地已有最新版本"
fi

#########################################################
#
# 安装
#
#########################################################
# 软件包解压后的目录
MEMCACHED_EXTRACT_DIR=`tar tf $DOWNLOAD_DIR/$REMOTE_MEMCACHED_PACKAGE_NAME | head -n 1`
LIBEVENT_EXTRACT_DIR=`tar tf $DOWNLOAD_DIR/$REMOTE_LIBEVENT_PACKAGE_NAME | head -n 1`
cd $DOWNLOAD_DIR

tar zxvf $REMOTE_LIBEVENT_PACKAGE_NAME
#echo "$REMOTE_MEMCACHED_PACKAGE_NAME"
#echo "$MEMCACHED_EXTRACT_DIR"
#echo "$DOWNLOAD_DIR/$MEMCACHED_EXTRACT_DIR"
#exit
cd $DOWNLOAD_DIR/$LIBEVENT_EXTRACT_DIR
./configure --prefix=/usr
make
make install

cd $DOWNLOAD_DIR

tar zxvf $REMOTE_MEMCACHED_PACKAGE_NAME

cd $DOWNLOAD_DIR/$MEMCACHED_EXTRACT_DIR
./configure --with-libevent=/usr
make
make install

echo "Memcached 成功安装!"

#########################################################
#
# 输出帮助信息
#
#########################################################
echo "\
启动示例:
/usr/local/bin/memcached -d -m 1024 -u root -l 127.0.0.1 -p 11001 -c 256 -P /tmp/memcached.pid

-d选项是启动一个守护进程
-m是分配给Memcache使用的内存数量,单位是MB,我这里是1GB
-u是运行Memcache的用户,我这里是root
-l是监听的服务器IP地址,如果有多个地址的话,我这里指定了服务器的IP地址127.0.0.1
-p是设置Memcache监听的端口,我这里设置了11001,最好是1024以上的端口
-c选项是最大运行的并发连接数,默认是1024,我这里设置了256,按照你服务器的负载量来设定
-P是设置保存Memcache的pid文件,我这里是保存在 /tmp/memcached.pid

Have Fun !\
"

VsFTPD自动安装脚本

Posted by – 2010-07-20

虽然平时配置一个ftp要不了5分钟,但写完脚本才发现,浪费的时间实现太多了,脚本一般半分钟就装好了。
本脚本在CentOS 和 Redhat Enterprise环境中测试通过。有空再测其它发行版。
脚本中用到了以下几个东西:
shell检查linux发行版;
shell向文本写东西;
shell生成随机密码;
sed输出匹配行的下一行;
……
本文采用的是虚拟用户方式
具体内容如下:

#----------------------------引用文字-开始----------------------------
#!/bin/bash
# VsFTPD Virtual User Configuration
# Author: 楚霏
# Company: chengyongxu.com
# Create Date: 2010-07-20
# Last Update Date: 2010-07-20 17:36
#################################################
#
# 在这一部分定义相关变量
#
#################################################
YUM_INSTALL_LIST="vsftpd db4 db4-tcl db4-utils"
APT_INSTALL_LIST="vsftpd db4.6-util"

# 通常只需要编辑以下两个变量
FTP_USER=www
FTP_HOME=/www

VSFTPD_BASE_DIR=/etc/vsftpd

#################################################
#
# 检查系统环境,并安装所需软件包
#
#################################################
# 检查是否以ROOT用户执行本脚本
[ `id -u` -ne '0' ] && echo 'Must be root!' && exit

# 检查发行版是否为本脚本所支持
[ `uname -i` = 'x86_64' ] && PLATFORM='x86_64' || PLATFORM='i386'
if cat /etc/issue|grep '4.' ;then
        OS=4
elif cat /etc/issue|grep '5.';then
        OS=5
else
        echo 'Do not support this system ' && exit
fi

DISTRIBUTION=`lsb_release -i | awk '{print $3}'`
case "$DISTRIBUTION" in
*CentOS*|*"Red Hat"*|*Fedora*)
    yum -y install $YUM_INSTALL_LIST
    ;;
*Debian*|*Ubuntu*)
    apt-get -y install $APT_INSTALL_LIST
    ;;
*)
    echo 'Do not support this distribution !' && exit 0
    ;;
esac

#################################################
#
# 创建FTP用的系统用户
#
#################################################
# 检查ftp要使用的用户是否存在
EXISTS_USER=`grep -E "^\<$FTP_USER\>" /etc/passwd |awk -F: '{print $1}'`
if [ "$EXISTS_USER" != "$FTP_USER" ]
then
        useradd -u 78 $FTP_USER -d $FTP_HOME -s /sbin/nologin
fi 

# 检查ftp用户的家目录是否正确
if [ "$EXISTS_USER" = "$FTP_USER" ]
then
       usermod -d $FTP_HOME $FTP_USER
fi

#################################################
#
# 创建FTP的目录和文件
#
#################################################
# 创建安装目录
if [ ! -d $VSFTPD_BASE_DIR ]
then
        mkdir -p $VSFTPD_BASE_DIR
fi

# 把FTP的系统用户写入
echo "$FTP_USER" >> $VSFTPD_BASE_DIR/vsftpd.chroot_list

# 创建日志文件
touch /var/log/vsftpd.log

# 创建虚拟用户的配置文件路径目录
mkdir -p $VSFTPD_BASE_DIR/user_config

# 创建密码文件, 单行为用户名, 双行为密码
touch $VSFTPD_BASE_DIR/passwd.txt

#################################################
#
# 修改配置文件
#
#################################################
# 写入测试用户和密码
echo ftpuser1 >> $VSFTPD_BASE_DIR/passwd.txt
echo `< /dev/urandom tr -dc A-Za-z0-9 | head -c 20` >> $VSFTPD_BASE_DIR/passwd.txt

# 修改PAM认证文件
echo "    auth       required     pam_userdb.so db=$VSFTPD_BASE_DIR/user_passwd" > /etc/pam.d/vsftpd
echo "    account       required     pam_userdb.so db=$VSFTPD_BASE_DIR/user_passwd" >> /etc/pam.d/vsftpd

# 编辑vsftpd的配置文件
> $VSFTPD_BASE_DIR/vsftpd.conf && echo "已删除默认配置"
echo "\
listen=YES
background=YES
anonymous_enable=NO
local_enable=YES
write_enable=YES
local_umask=022
anon_upload_enable=NO
anon_mkdir_write_enable=NO
dirmessage_enable=YES
xferlog_enable=YES
connect_from_port_20=YES
chown_uploads=NO
xferlog_file=/var/log/vsftpd.log
xferlog_std_format=YES
async_abor_enable=YES
ascii_upload_enable=YES
ascii_download_enable=YES
ftpd_banner=Welcome to Ismole FTP servers
pam_service_name=vsftpd
chroot_local_user=NO
chroot_list_enable=YES
chroot_list_file=/etc/vsftpd/vsftpd.chroot_list
guest_enable=YES
guest_username=$FTP_USER
user_config_dir=$VSFTPD_BASE_DIR/user_config\
" >> $VSFTPD_BASE_DIR/vsftpd.conf

# 为虚拟用户创建家目录和配置文件
if [ ! -d $FTP_HOME/wwwroot/ftpuser1 ]
then
        mkdir -p $FTP_HOME/wwwroot/ftpuser1
        chown -R $FTP_USER $FTP_HOME
fi

echo "\
local_root=$FTP_HOME/wwwroot/ftpuser1
write_enable=YES
anon_umask=022
anon_world_readable_only=NO
anon_upload_enable=YES
anon_mkdir_write_enable=YES
anon_other_write_enable=YES\
" >> $VSFTPD_BASE_DIR/user_config/ftpuser1

#################################################
#
# 创建虚拟用户密码认证数据库文件,并写成脚本
#
#################################################
case "$DISTRIBUTION" in
*CentOS*|*"Red Hat"*|*Fedora*)
    echo "\
if [ -e $VSFTPD_BASE_DIR/user_passwd.db ]
then
        rm -f $VSFTPD_BASE_DIR/user_passwd.db
fi
db_load -T -t hash -f $VSFTPD_BASE_DIR/passwd.txt $VSFTPD_BASE_DIR/user_passwd.db\
" >> $VSFTPD_BASE_DIR/db_load.sh
    ;;
*Debian*|*Ubuntu*)
    echo "\
if [ -e $VSFTPD_BASE_DIR/user_passwd.db ]
then
        rm -f $VSFTPD_BASE_DIR/user_passwd.db
fi
db4.6_load -T -t hash -f $VSFTPD_BASE_DIR/passwd.txt $VSFTPD_BASE_DIR/user_passwd.db\
" >> $VSFTPD_BASE_DIR/db_load.sh
    ;;
*)
    echo 'Do not support this distribution !!' && exit
    ;;
esac

chmod 500 $VSFTPD_BASE_DIR/db_load.sh
$VSFTPD_BASE_DIR/db_load.sh

#################################################
#
# 启动并测试
#
#################################################
/etc/init.d/vsftpd start

# 验证登录
echo "测试用户名是: ftpuser1"
# 显示出匹配行的下一行,也就是密码
echo "测试用户密码是: `sed -n '/ftpuser1/{n;p;}' $VSFTPD_BASE_DIR/passwd.txt`"
echo "测试无误的话别忘了删除测试用户,Good luck ! "
#----------------------------引用文字-结束----------------------------

Nginx与php分离配置

Posted by – 2010-05-12

####################################
#Nginx与php分离配置
#Author:楚霏
#Date: 2010-5-12
#Update: 2010-5-12
#Env: Centos 5.4 x86_64
####################################

一、准备工作
####################################
环境:Centos 5.4 x86_64
WEBServer IP: 10.0.0.240
PHPServer1 IP: 10.0.0.241
PHPServer1 IP: 10.0.0.242
####################################

二、思路
####################################
WEBServer:
1.只负责接收http请求并分发;
2.只安装nginx,不安装php;
3.只建/www/wwwroot目录,不存放php代码;
4.所有静态文件使用CDN。

PHPServer:
1.只负责处理php请求;
2.只安装php-fastcgi和mysql客户端;
3.建/www/wwwroot目录,最好从NFS文件服务器上挂载网站代码。
####################################

三、配置
####################################
#WEBServer:
#增加一行到nginx.conf

#----------------------------引用文字-开始----------------------------
    include upstream.conf;
#----------------------------引用文字-结束----------------------------

#新加upstream配置文件

#vi /usr/local/nginx/conf/upstream.conf
#----------------------------引用文字-开始----------------------------
upstream FastCGIServers {
        server 10.0.0.241:9000;
        server 10.0.0.251:9000;
}
#----------------------------引用文字-结束----------------------------

#修改虚拟主机配置文件的fastcgi_pass为负载均衡指定的upstream值

#----------------------------引用文字-开始----------------------------
    location ~ \.php$ {
        include fastcgi_params;
        #fastcgi_pass  unix:/tmp/php-fcgi.sock;
        fastcgi_pass  FastCGIServers;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME /www/wwwroot/test.com$fastcgi_script_name;
    }
#----------------------------引用文字-结束----------------------------

#PHPServer:
vi /usr/local/php-fcgi/etc/php-fpm.conf
#----------------------------引用文字-开始----------------------------
#把监听地址改为本机ip:port
#找到:
/tmp/php-fcgi.sock

#改为:
10.0.0.241:9000

#把允许的客户端改为WEBServer的地址
#找到:
127.0.0.1
#改为:
10.0.0.240
#----------------------------引用文字-结束----------------------------

####################################

四、特点
####################################
1.扩展容易:增加一台php服务器,就不用配置nginx了,直接到nginx配置文件中增加一行即可
2.nginx配置全在一台机器,不再需要建太多虚拟主机,也不需要每台服务器hosts文件中解析自定义的域名
2.更好发挥各自优势,nginx只用来接收http请求,有望达到传说中的并发10万;php-fcgi也可以开更多的进程。
####################################

注:仅在虚拟机中实验过,生产环境未经测试不建议使用。

转出万网记

Posted by – 2010-05-06

这个域名在万网呆了快两年了,用了一段Godaddy的服务感觉不错,至于其它原因,每个想转移域名的都差不多,不再赘述。
2010年3月,我首先想到的是投诉到ICANN,没什么效果;
2010年4月,我朋友卖扣赵给万网又发了两封快递,填写那个万恶的转移表,发给代理商再发给万网;
2010年5月5日,我等不及了,给万网客服打了4个电话,不同的客服,最终第四个客服态度不错,赞了她一下,顺便鄙视了她之前的三个客服,她答应第二天回复我;
2010年5月6日,一上班,就接到电话说已经给我朋友发过转移密码了,看来每个人都需要赞美呀!
收到密码,迫不及待的打开Godaddy,按网上教程索骥,现在到验证这一步了,期待中。

2010年5月7日,等待

2010年5月12日,大功告成

XtraDB internals

Posted by – 2010-04-27

原文地址:http://www.percona.com/docs/wiki/percona-xtradb:internals:start

Gearman_Configuration

Posted by – 2010-04-21

####################################
#Gearman_Configuration
#Author:楚霏
#Date: 2010-4-21
#Update:2010-4-21
#Env: Centos 5.4 x86_64
####################################

一、准备工作
####################################
环境:Centos 5.4 x86_64
所需软件:
gearmand
gearman(php支持所需)
####################################

cd /usr/local/src/
wget -c http://launchpad.net/gearmand/trunk/0.13/+download/gearmand-0.13.tar.gz
wget -c http://pecl.php.net/get/gearman-0.7.0.tgz

二、安装配置
####################################
(1)安装服务端gearmand

cd /usr/local/src/
tar xvf gearmand-0.13.tar.gz
cd gearmand-0.13
yum install libevent-devel
./configure --prefix=/usr/local/gearmand
make
make install
cp scripts/gearmand-init /etc/init.d/gearmand
chmod 755 /etc/init.d/gearmand
mkdir /usr/local/gearmand/var/
vi /etc/init.d/gearmand
#----------------------------引用文字-开始----------------------------
#!/bin/sh

# Gearman server and library
# Copyright (C) 2008 Brian Aker, Eric Day
# All rights reserved.
#
# Use and distribution licensed under the BSD license.  See
# the COPYING file in this directory for full text.

prefix=/usr/local/gearmand
exec_prefix=${prefix}
GEARMAND=${exec_prefix}/sbin/gearmand
PIDFILE=${prefix}/var/gearmand.pid

start()
{
  $GEARMAND -d -u root -P $PIDFILE
}

stop()
{
  kill `cat $PIDFILE`
  rm -f $PIDFILE
}

case "$1" in

  start)
    start
  ;;

  stop)
    stop
  ;;

  restart)
    stop
    start
  ;;

  *)
    echo "Usage: $0 {start|stop|restart}"
  ;;

esac
#----------------------------引用文字-结束----------------------------

(2)安装PHP的gearman模块

cd /usr/local/src/
tar xvf gearman-0.7.0.tgz
cd gearman-0.7.0
/usr/local/php-fcgi/bin/phpize
./configure --with-php-config=/usr/local/php-fcgi/bin/php-config --with-gearman=/usr/local/gearmand/
make
make install
cp  /usr/local/php-fcgi/lib/php/extensions/no-debug-non-zts-20090626/gearman.so /usr/local/php-fcgi/ext/
echo "extension = gearman.so" >> /usr/local/php-fcgi/etc/php.ini
service nginx restart
#检查一下是否加载上了
/usr/local/php-fcgi/bin/php -m | grep gear
#也可以
/usr/local/php-fcgi/bin/php --info | grep gear

三、启动
####################################

service gearmand start

#默认使用的是4730端口

#把测试用的php文件拷贝到一个WEB站点目录下

cp /usr/local/src/gearman-0.7.0/test_*php /www/wwwroot/test.com/
chown -R www:www /www/wwwroot/test.com/

四、测试

#----------------------------引用文字-开始----------------------------
[root@chengyongxu.com test.com]# /usr/local/php-fcgi/bin/php test_worker.php &
[1] 4453
[root@chengyongxu.com test.com]# gearman_worker_create() pass
gearman_worker_add_servers() pass
gearman_worker_add_function() pass
Starting Worker...

[root@chengyongxu.com test.com]# /usr/local/php-fcgi/bin/php test_client.php
# TESTING CLIENT INTERFACE
gearman_client_create() pass
gearman_client_clone() pass
unset client pass
gearman_client_error() pass
gearman_client_errno() pass
gearman_client_set_options() pass
gearman_client_add_servers() pass
gearman_client_do() pass -> gearman_client_do
gearman_client_do_background() pass -> H:www.chengyongxu.com:2
gearman_client_do_high() pass -> gearman_client_do_high
gearman_client_do_low() pass -> gearman_client_do_low
gearman_client_do_high_background() pass -> H:www.chengyongxu.com:5
gearman_client_do_low_background() pass -> H:www.chengyongxu.com:6
gearman_client_job_status() pass -> is_known: 1 is_running: 1 2 / 4
gearman_client_do_job_handle() pass -> H:www.chengyongxu.com:7
gearman_client_echo() pass
gearman_client_add_task() pass
gearman_send_job_status() pass
Testing Job Functions
        gearman_job_handle() pass - H:www.chengyongxu.com:8
        gearman_job_workload() pass - test_gearman_job
        gearman_job_workload_size() pass - 16
        gearman_job_function_name() pass - test_gearman_job
gearman_client_run_tasks() pass
gearman_client_clear_fn() pass
        gearman_client_set_created_fn() pass
        gearman_job_send_status() pass
        gearman_client_set_status_fn() pass
        gearman_job_send_warning() pass
        gearman_client_set_warning_fn() pass
        gearman_job_send_exception() pass
        gearman_job_send_fail() pass
        gearman_client_set_fail_fn() pass
Testing Task Functions
        gearman_task_function_name() pass - test_tasks
        gearman_task_unique() pass - c947b588-f0d8-4d2a-a8a9-dc07e913a787
        gearman_task_job_handle() pass - H:www.chengyongxu.com:10
        gearman_task_numerator() pass - 1
        gearman_task_denominator() pass - 1
        gearman_task_data() pass - test_tasks
        gearman_task_data_size() pass - 10
        gearman_client_add_task_status() pass
        test_tasks_background() pass
        gearman_task_is_known() pass
        gearman_task_is_running() pass
gearman_client_add_task_background() pass
gearman_client_add_task_high() pass
gearman_client_add_task_low() pass
gearman_client_add_task_high_background() pass
gearman_client_add_task_low_background() pass
        test_task_high() pass
        test_task_low() pass
        test_task_high_background() pass
Done Working
gearman_worker_work() pass
DONE 0
[1]+  Done                    /usr/local/php-fcgi/bin/php test_worker.php
#----------------------------引用文字-结束----------------------------

####################################