« 2010年11月的文章归档

Memcache安装

一 安装Memcached服务端

注:需先安装libevent

tar zxvf memcached-1.4.5.tar.gz
cd memcached-1.4.5
./configure –prefix=/usr/local/memcached –with-libevent=/usr
make
make install

二 安装Memcache的PHP扩展(客户端)

下载地址: http://pecl.php.net/package/memcache
tar zxvf memcache-3.0.5.tgz
cd memcache-3.0.5
/usr/local/php/bin/phpize
./configure –with-php-config=/usr/local/php/bin/php-config
make && make install
输出:Installing shared extensions:     /usr/local/php/lib/php/extensions/no-debug-non-zts-20090626/

vi /usr/local/php/etc/php.ini
把php.ini中的extension_dir = “./”修改为
extension_dir = “/usr/local/php/lib/php/extensions/no-debug-non-zts-20090626/”
添加一行来载入memcache扩展:extension=memcache.so

重启web server

三 启动Memcached服务端
1.添加一个用户用于启动memcache
useradd -s /sbin/nologin -M memcached
2.启动命令:
例:
/usr/local/bin/memcached -d -m 10 -u memcached -l 192.168.0.200 -p 12000 -c 256 -P /tmp/memcached.pid
/usr/local/bin/memcached -d -m 10 -u memcached -P /tmp/memcached.pid
如想开机启动可以把该命令加入/etc/rc.d/rc.local中
####################################################
附memcached参数:
-d选项是启动一个守护进程,
-m是分配给Memcache使用的内存数量,单位是MB,我这里是10MB,
-u是运行Memcache的用户,我这里是root,
-l是监听的服务器IP地址,如果有多个地址的话,我这里指定了服务器的IP地址192.168.0.200,
-p是设置Memcache监听的端口,我这里设置了12000,最好是1024以上的端口,
-c选项是最大运行的并发连接数,默认是1024,我这里设置了256,按照你服务器的负载量来设定,
-P是设置保存Memcache的pid文件,我这里是保存在 /tmp/memcached.pid,
####################################################

如果要结束Memcache进程,执行:
kill `cat /tmp/memcached.pid`
也可以启动多个守护进程,不过端口不能重复。

####################################################
在我测试/usr/local/bin/memcached -h 时报错
/usr/local/bin/memcached: error while loading shared libraries: libevent-2.0.so.2: cannot open shared object file: No such file or directory
解决方法:
1.查看DEBUG
LD_DEBUG=libs /usr/local/bin/memcached -v
2.查看search path
3.查找 libevent-2.0.so.2
whereis libevent
libevent: /usr/local/lib/libevent.la /usr/local/lib/libevent.so /usr/local/lib/libevent.a
ll /usr/local/lib/libevent.so
lrwxrwxrwx 1 root root 21 11-23 14:13 /usr/local/lib/libevent.so -> libevent-2.0.so.2.2.0
得出结论,除错:
ln -s /usr/local/lib/libevent.so /usr/lib/libevent-2.0.so.2
####################################################

四 测试

<?php

$memcache = new Memcache;
$memcache->connect('localhost', 11211) or die ("Could not connect");

$version = $memcache->getVersion();
echo "Server's version: ".$version."<br/>\n";

$tmp_object = new stdClass;
$tmp_object->str_attr = "test";
$tmp_object->int_attr = 123;

$memcache->set("key", $tmp_object, false, 10) or die ("Failed to save data at the server");
echo "Store data in the cache (data will expire in 10 seconds)<br/>\n";

$get_result = $memcache->get("key");
echo "Data from the cache:<br/>\n";

var_dump($get_result);

?>

基于cent os 编译配置LNMP

参考了一些网文,记录自己的安装的一些心得经验。

一 安装前的配置环境

sudo -s
LANG=C
yum -y install gcc gcc-c++ autoconf libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel libxml2 libxml2-devel zlib zlib-devel glibc glibc-devel glib2 glib2-devel bzip2 bzip2-devel ncurses ncurses-devel curl curl-devel e2fsprogs e2fsprogs-devel krb5 krb5-devel libidn libidn-devel openssl openssl-devel openldap openldap-devel nss_ldap openldap-clients openldap-servers

yum -y install gcc openssl-devel zlib-devel pcre-devel
yum groupinstall “Developement Tools” “Development Libraries” -yt

yum -y install yum-fastestmirror
yum -y update
yum -y install patch make gcc gcc-c++ gcc-g77 flex bison
yum -y install libtool libtool-libs kernel-devel autoconf
yum -y install libjpeg libjpeg-devel libpng libpng-devel
yum -y install freetype freetype-devel libxml2 libxml2-devel zlib  zlib-devel
yum -y install glib2 glib2-devel bzip2 diff*
yum -y install bzip2-devel ncurses ncurses-devel curl curl-devel  e2fsprogs
yum -y install e2fsprogs-devel krb5 krb5-devel libidn libidn-devel
yum -y install openssl openssl-devel vim-minimal
yum -y install fonts-chinese scim-chewing scim-pinyin  scim-tables-chinese

—————–          华丽的分割线          —————–

二 安装NGINX

1 安装PCRE.

注:由于NGINX里用到了pcre,所以需先安装该包,否则会报错

yum -y install pcre-devel

2 安装nginx

2.1 添加一个不能登录的且没有家目录 名为nginx的用户

useradd -s /sbin/nologin -M nginx

2.2 解压缩nginx并安装

tar zxvf nginx-0.8.53.tar.gz
cd nginx-0.8.53
./configure \
–prefix=/usr/local/nginx \
–user=nginx \
–group=nginx \
–with-http_ssl_module \
–with-http_flv_module \
–with-http_stub_status_module \
–with-http_gzip_static_module \

make && make install

2.3 增加nginxd文件,方便管理

vi /etc/init.d/nginxd

#!/bin/sh
#
# nginx – this script starts and stops the nginx daemon
#
# chkconfig:   – 85 15
# description:  Nginx is an HTTP(S) server, HTTP(S) reverse \
#               proxy and IMAP/POP3 proxy server
# processname: nginx
# config:      /usr/local/nginx/conf/nginx.conf
# config:      /etc/sysconfig/nginx
# pidfile:     /usr/local/nginx/logs/nginx.pid

# Source function library.
. /etc/rc.d/init.d/functions

# Source networking configuration.
. /etc/sysconfig/network

# Check that networking is up.
[ "$NETWORKING" = "no" ] && exit 0

nginx=”/usr/local/nginx/sbin/nginx”
prog=$(basename $nginx)

NGINX_CONF_FILE=”/usr/local/nginx/conf/nginx.conf”

[ -f /usr/local/nginx/conf/sysconfig/nginx ] && . /usr/local/nginx/conf/sysconfig/nginx

lockfile=/usr/local/nginx/logs/nginx.lock

make_dirs() {
# make required directories
user=`nginx -V 2>&1 | grep “configure arguments:” | sed ‘s/[^*]*–user=\([^ ]*\).*/\1/g’ -`
options=`$nginx -V 2>&1 | grep ‘configure arguments:’`
for opt in $options; do
if [ `echo $opt | grep '.*-temp-path'` ]; then
value=`echo $opt | cut -d “=” -f 2`
if [ ! -d "$value" ]; then
# echo “creating” $value
mkdir -p $value && chown -R $user $value
fi
fi
done
}

start() {
[ -x $nginx ] || exit 5
[ -f $NGINX_CONF_FILE ] || exit 6
make_dirs
echo -n $”Starting $prog: ”
daemon $nginx -c $NGINX_CONF_FILE
retval=$?
echo
[ $retval -eq 0 ] && touch $lockfile
return $retval
}

stop() {
echo -n $”Stopping $prog: ”
killproc $prog -QUIT
retval=$?
echo
[ $retval -eq 0 ] && rm -f $lockfile
return $retval
}

restart() {
configtest || return $?
stop
sleep 1
start
}

reload() {
configtest || return $?
echo -n $”Reloading $prog: ”
killproc $nginx -HUP
RETVAL=$?
echo
}

force_reload() {
restart
}

configtest() {
$nginx -t -c $NGINX_CONF_FILE
}

rh_status() {
status $prog
}

rh_status_q() {
rh_status >/dev/null 2>&1
}

case “$1″ in
start)
rh_status_q && exit 0
$1
;;
stop)
rh_status_q || exit 0
$1
;;
restart|configtest)
$1
;;
reload)
rh_status_q || exit 7
$1
;;
force-reload)
force_reload
;;
status)
rh_status
;;
condrestart|try-restart)
rh_status_q || exit 0
;;
*)
echo $”Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}”
exit 2
esac

2.4 将nginx服务设为开机启动,并启动nginx

chmod +x /etc/init.d/nginxd
chkconfig –add nginxd ##让入开机启动选项中
chkconfig nginxd on #让其开机自动启动
service nginxd  start #立即启动nginx 服务

—————–          华丽的分割线          —————–

三 安装MYSQL

1 解压并安装MYSQL

tar -zxvf mysql-mysql-5.1.53.tar.gz
cd mysql-5.1.53
./configure –prefix=/usr/local/mysql –with-extra-charsets=all –enable-thread-safe-client –enable-assembler –with-charset=utf8 –enable-thread-safe-client –with-extra-charsets=all –with-big-tables –with-readline –with-ssl –with-embedded-server –enable-local-infile –without-debug
make && make install

2 创建MySQL数据库,用默认的配置my.cnf

groupadd mysql
useradd -g mysql mysql
cp /usr/local/mysql/share/mysql/my-medium.cnf /etc/my.cnf
/usr/local/mysql/bin/mysql_install_db –user=mysql
chown -R mysql /usr/local/mysql/var
chgrp -R mysql /usr/local/mysql/.

3 添加Mysql启动服务,并且设置root密码

cp /usr/local/mysql/share/mysql/mysql.server /etc/init.d/mysql
chmod 755 /etc/init.d/mysql
chkconfig –level 345 mysql on
echo “/usr/local/mysql/lib/mysql” >> /etc/ld.so.conf
echo “/usr/local/lib” >> /etc/ld.so.conf
ldconfig
ln -s /usr/local/mysql/lib/mysql /usr/lib/mysql
ln -s /usr/local/mysql/include/mysql /usr/include/mysql
service mysql start
/usr/local/mysql/bin/mysqladmin -u root password <your password>
service mysql restart

—————–          华丽的分割线          —————–

四 安装PHP(fast-cgi 模式)

1 前期配置(安装libevent , libconv)

tar zxvf libevent-1.4.14b-stable.tar.gz
cd  libevent-1.4.14b-stable
./configure && make && make install
tar zxvf  libconv-1.13.1.tar.gz
cd libiconv-1.13.1
./configure
make && make install

2 安装PHP(version>=5.3.3)

注:版本5.3.3之前不内置php-fpm,需打补丁。而5.3.3版本和以后版本内置,不需要打补丁,这里选择php 5.3.3版本。

tar zxvf php-5.3.3.tar.gz
./configure –prefix=/usr/local/php –with-mysql=/usr/local/mysql –with-mysqli=/usr/local/mysql/bin/mysql_config  –with-openssl –enable-fpm  –with-libevent-dir=/usr/local –enable-mbstring –with-freetype-dir –with-jpeg-dir –with-png-dir –with-zlib –with-libxml-dir=/usr –enable-xml –with-iconv-dir=/usr/local
make ZEND_EXTRA_LIBS=’-liconv’
make install

2.1 配置php.ini

cp /usr/local/src/php-5.3.3/php.ini-production  /usr/local/php/etc/php.ini

2.2 配置fpm

cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf

vi /usr/local/php/etc/php-fpm.conf

pid 那行注释去掉(因为php 5.3.3里内置的php-fpm不能用原来的方式重载,为了方便下面的关闭,重启等命令使用,把pid前面的注释去掉。 )
pm.max_children = 50
pm.start_servers = 10
pm.min_spare_servers = 5
pm.max_spare_servers = 35

/usr/local/php/sbin/php-fpm & #启动

netstat -tnlp #查看是否启动(fpm 9000端口)

#开机启动
vi /etc/rc.d/rc.local
在空白行添加   /usr/local/php/sbin/php-fpm &

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

附:php-fpm操作指令
SIGINT, SIGTERM 立刻终止
SIGQUIT 平滑终止
SIGUSR1 重新打开日志文件
SIGUSR2 平滑重载所有worker进程并重新载入配置和二进制模块

示例:
php-fpm 关闭:
kill -SIGINT `cat /usr/local/php/var/run/php-fpm.pid`
php-fpm 重启:
kill -SIGUSR2 `cat /usr/local/php/var/run/php-fpm.pid`
##########################################################################################

2.3 配置fastcgi_params 文件

vi /usr/local/nginx/conf/fastcgi_params

将里面内容替换为

fastcgi_param  GATEWAY_INTERFACE  CGI/1.1;
fastcgi_param  SERVER_SOFTWARE    nginx;
fastcgi_param  QUERY_STRING       $query_string;
fastcgi_param  REQUEST_METHOD     $request_method;
fastcgi_param  CONTENT_TYPE       $content_type;
fastcgi_param  CONTENT_LENGTH     $content_length;
fastcgi_param  SCRIPT_FILENAME    $document_root$fastcgi_script_name;
fastcgi_param  SCRIPT_NAME        $fastcgi_script_name;
fastcgi_param  REQUEST_URI        $request_uri;
fastcgi_param  DOCUMENT_URI       $document_uri;
fastcgi_param  DOCUMENT_ROOT      $document_root;
fastcgi_param  SERVER_PROTOCOL    $server_protocol;
fastcgi_param  REMOTE_ADDR        $remote_addr;
fastcgi_param  REMOTE_PORT        $remote_port;
fastcgi_param  SERVER_ADDR        $server_addr;
fastcgi_param  SERVER_PORT        $server_port;
fastcgi_param  SERVER_NAME        $server_name;
# PHP only, required if PHP was built with –enable-force-cgi-redirect
fastcgi_param  REDIRECT_STATUS    200;

2.4 修改nginx.conf配置文件

vi /usr/local/nginx/conf/nginx.conf

#user  nobody; 改为 user  nginx;
location ~ \.php$ {
root           /var/www;
fastcgi_pass   127.0.0.1:9000;
fastcgi_index  index.php;
fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
include        fastcgi_params;
}
location / {
root   /www;
index  index.html index.htm index.php;
}

重启nginx
service nginxd restart

—————–          华丽的EOF        —————–