目录
- 目录
- 前言
- php memcached扩展安装
- 服务器端安装
- 客户端安装
- 设置memcached开机自启脚本
- 创建脚本
- 设置服务
- 服务命令
- php redis扩展安装
- redis安装
- 服务命令
- 开放redis端口
- redis手动安装
- 修改redis配置文件
- 设置redis开机自启脚本
- 创建脚本
- 设置服务
- 服务命令
- 安装php的redis扩展
前言
继上文《Linux中Web环境的搭建LNMP》,在上文的环境下,安装memcached
和redis
,以及对应的PHP扩展。
php memcached扩展安装
(参考:linux php安装memcached扩展)
(使用root用户进行安装)
服务器端安装
- 安装
libevent 库
cd /home/app/pkg/memcached
tar zxvf libevent-2.0.22-stable.tar.gz
cd libevent-2.0.22-stable
./configure --prefix=/usr/local/libevent
make && make install
- 安装memcached服务器版
cd /home/app/pkg/memcached
tar zxvf memcached-1.4.33.tar.gz
cd memcached-1.4.33
./configure --prefix=/app/env/memcached --with-libevent=/usr/local/libevent
- 运行
/app/env/memcached/bin/memcached -m 64 -p 11211 -u app -d
ps aux | grep memcached # 查看是否运行成功
客户端安装
- 安装libmemcached库
cd /home/app/pkg/memcached
tar zxvf libmemcached-1.0.18.tar.gz
cd libmemcached-1.0.18
./configure --prefix=/app/env/libmemcached
make && make install
- 安装php-fpm的memcached扩展
cd /home/app/pkg/memcached
tar zxvf phpmemcached-2.2.0.tar.gz
cd phpmemcached-2.2.0
/app/env/php/bin/phpize --with-php-config=/app/env/php/bin/php-config
./configure --with-php-config=/app/env/php/bin/php-config --with-libmemcached-dir=/app/env/libmemcached --disable-memcached-sasl
make && make install
安装成功提示:
Installing shared extensions: /app/env/php/lib/php/extensions/no-debug-non-zts-20100525/
- 为php添加扩展
cd /app/env/php/lib
sudo vim php.ini
在php.ini
中添加,如下内容:
extension=memcached.so
- 重启php-fpm
service php-fpm restart
- 写一个php文件测试一下 test_memcached.php
<?php
$mem=new Memcached(); //实例化Memcached类
$server=array(
array('127.0.0.1',11211),
);
$mem->addServers($server);
$mem->set('name','DreamBoy',15); 设置缓存值,有效时间15秒,如果有效时间设置为0,则表示该缓存值永久存在的(系统重启前)
// 如果失效值被设置为0(默认),此元素永不过期(但是它可能由于服务端为了给其他新的元素分配空间而被删除)
echo $mem->get('name');
?>
设置memcached开机自启脚本
创建脚本
cd /etc/init.d/
vim memcached
#! /bin/sh
#
# chkconfig: - 55 45
# description: The memcached daemon is a network memory cache service.
# processname: memcached
# config: /etc/sysconfig/memcached
# Source function library.
. /etc/rc.d/init.d/functions
PORT=11211
USER=root
MAXCONN=1024
CACHESIZE=128
OPTIONS=""
if [ -f /etc/sysconfig/memcached ];then
. /etc/sysconfig/memcached
fi
# Check that networking is up.
if [ "$NETWORKING" = "no" ]
then
exit 0
fi
RETVAL=0
prog="memcached"
start () {
echo -n $"Starting $prog: "
# insure that /var/run/memcached has proper permissions
# chown $USER /var/run/memcached
daemon /app/env/memcached/bin/memcached -d -p $PORT -u $USER -m $CACHESIZE -c $MAXCONN -P /var/run/memcached/memcached.pid $OPTIONS
RETVAL=$?
echo
[ $RETVAL -eq 0 ] && touch /var/lock/subsys/memcached
}
stop () {
echo -n $"Stopping $prog: "
killproc memcached
RETVAL=$?
echo
if [ $RETVAL -eq 0 ] ; then
rm -f /var/lock/subsys/memcached
rm -f /var/run/memcached/memcached.pid
fi
}
restart () {
stop
start
}
# See how we were called.
case "$1" in
start)
start
;;
stop)
stop
;;
status)
status memcached
;;
restart|reload)
restart
;;
condrestart)
[ -f /var/lock/subsys/memcached ] && restart || :
;;
*)
echo $"Usage: $0 {start|stop|status|restart|reload|condrestart}"
exit 1
esac
exit $?
chmod +x /etc/init.d/memcached
设置服务
chkconfig memcached on
查看服务是否开启成功
chkconfig --list memcached
# memcached 0:off 1:off 2:on 3:on 4:on 5:on 6:off
服务命令
service memcached start
service memcached stop
service memcached restart
service memcached reload
/etc/init.d/memcached start
/etc/init.d/memcached stop
/etc/init.d/memcached restart
/etc/init.d/memcached reload
php redis扩展安装
(参考:Linux下redis安装与使用、linux 添加开机自启动脚本)
(使用root用户进行安装)
redis安装
cd /app/env/
mkdir redis
cd /home/app/pkg/redis
tar zxf redis-3.2.9.tar.gz
cd redis-stable/
make
cd src/
make PREFIX=/app/env/redis/ install
cd /home/app/pkg/redis/redis-stable/utils
./install_server.sh
# 选择默认端口
6379
# 为端口6379设置配置文件的位置(此时配置文件中daemonize为yes,即以以守护进程的方式运行)
/app/env/redis/redis_6379.conf
# 为端口6379设置redis日志路径
/app/log/redis/redis_6379.log
# 设置配置文件中dir值,默认为redis-server启动时会在当前目录生成或读取dump.rdb,这里设置为绝对路径
/app/env/redis/lib/6379
# 设置redis-server服务的路径
/app/env/redis/bin/redis-server
在这种安装方式的情况下,redis_6379脚本将自动生成在/etc/init.d/
目录下,作为服务存在,并且设置为系统开启自动运行的脚本。
chkconfig --list redis_6379
redis_6379 0:off 1:off 2:on 3:on 4:on 5:on 6:off
服务命令
service redis_6379 start
service redis_6379 stop
service redis_6379 restart
service redis_6379 status
/etc/init.d/redis_6379 start
/etc/init.d/redis_6379 stop
/etc/init.d/redis_6379 restart
/etc/init.d/redis_6379 status
开放redis端口
如果redis需要作为远程服务进行使用时,需要开放redis服务的端口(默认端口为:6379),如下:
vim /etc/sysconfig/iptables
在文件中添加:
-A INPUT -p tcp -m state --state NEW -m tcp --dport 6379 -j ACCEPT
重启防火墙:
service iptables restart
redis手动安装
(这一部分可以跳过,这里当做额外学习进行记录而已!!!)
另一种安装方式:
cd /home/app/pkg/redis
tar zxf redis-3.2.9.tar.gz
cd redis-stable/
make
cd /app/env/
mkdir redis
cp /home/app/pkg/redis/redis-stable/src/redis-server redis/
cp /home/app/pkg/redis/redis-stable/src/redis-benchmark redis/
cp /home/app/pkg/redis/redis-stable/src/redis-cli redis/
cp /home/app/pkg/redis/redis-stable/redis.conf redis/
cd redis/
启动redis服务:
./redis-server redis.conf
测试redis服务:
./redis-cli
> set name test
> get name
修改redis配置文件
在这种情况下,建议修改下配置文件的内容:
cd /app/env/redis
sudo vim redis.conf
# 以守护进程的方式运行
daemonize yes
# 默认的话redis-server启动时会在当前目录生成或读取dump.rdb,这里修改为绝对路径
dir /app/env/redis
设置redis开机自启脚本
创建脚本
cd /etc/init.d/
vim redis
#! /bin/sh
#
# chkconfig: - 55 45
# description: Starts,stops and restart the redis-server
# Ver:1.1
# Write by ND chengh(200808)
# usage: ./script_name -p [port] {start|stop|status|restart}
# Source function library.
. /etc/rc.d/init.d/functions
# Source networking configuration.
. /etc/sysconfig/network
# Check networking is up.
[ "$NETWORKING" = "no" ] && exit 0
RETVAL=0
REDIS_PORT=6379
PID=
if [ "$1" = "-p" ]; then
REDIS_PORT=$2
shift 2
fi
REDIS_DIR="/app/env/redis"
REDIS="${REDIS_DIR}/redis-server"
PROG=$(basename $REDIS)
CONF="${REDIS_DIR}/redis-${REDIS_PORT}.conf"
if [ ! -f $CONF ]; then
if [ -f "${REDIS_DIR}/redis.conf" ];then
CONF="${REDIS_DIR}/redis.conf"
else
echo -n $"$CONF not exist.";warning;echo
exit 1
fi
fi
PID_FILE=`grep "pidfile" ${CONF}|cut -d ' ' -f2`
PID_FILE=${PID_FILE:=/var/run/redis.pid}
LOCKFILE="/var/lock/subsys/redis-${REDIS_PORT}"
if [ ! -x $REDIS ]; then
echo -n $"$REDIS not exist.";warning;echo
exit 0
fi
start() {
echo -n $"Starting $PROG: "
$REDIS $CONF
RETVAL=$?
if [ $RETVAL -eq 0 ]; then
success;echo;touch $LOCKFILE
else
failure;echo
fi
return $RETVAL
}
stop() {
echo -n $"Stopping $PROG: "
if [ -f $PID_FILE ] ;then
read PID < "$PID_FILE"
else
failure;echo;
echo -n $"$PID_FILE not found.";failure;echo
return 1;
fi
if checkpid $PID; then
kill -TERM $PID >/dev/null 2>&1
RETVAL=$?
if [ $RETVAL -eq 0 ] ;then
success;echo
echo -n "Waiting for Redis to shutdown .."
while checkpid $PID;do
echo -n "."
sleep 1;
done
success;echo;rm -f $LOCKFILE
else
failure;echo
fi
else
echo -n $"Redis is dead and $PID_FILE exists.";failure;echo
RETVAL=7
fi
return $RETVAL
}
restart() {
stop
start
}
rhstatus() {
status -p ${PID_FILE} $PROG
}
hid_status() {
rhstatus >/dev/null 2>&1
}
case "$1" in
start)
hid_status && exit 0
start
;;
stop)
rhstatus || exit 0
stop
;;
restart)
restart
;;
status)
rhstatus
RETVAL=$?
;;
*)
echo $"Usage: $0 -p [port] {start|stop|status|restart}"
RETVAL=1
esac
exit $RETVAL
chmod +x /etc/init.d/redis
设置服务
chkconfig redis on
查看服务是否开启成功
chkconfig --list redis
# redis 0:off 1:off 2:on 3:on 4:on 5:on 6:off
服务命令
service redis start
service redis stop
service redis restart
service redis status
/etc/init.d/redis start
/etc/init.d/redis stop
/etc/init.d/redis restart
/etc/init.d/redis status
安装php的redis扩展
cd /home/app/pkg/redis
tar zxf phpredis-2.2.4.tar.gz
cd phpredis-2.2.4
/app/env/php/bin/phpize --with-php-config=/app/env/php/bin/php-config
./configure --with-php-config=/app/env/php/bin/php-config
make && make install
安装成功提示:
Installing shared extensions: /app/env/php/lib/php/extensions/no-debug-non-zts-20100525/
为php添加扩展
cd /app/env/php/lib
sudo vim php.ini
在php.ini中添加,如下内容:
extension=redis.so
重启php-fpm
service php-fpm restart
写一个php文件测试一下 test_redis.php
<?php
$redis = new redis();
$redis->connect('127.0.0.1', 6379);
$result = $redis->set('test',"11111111111");
var_dump($result); //结果:bool(true)
$result = $redis->get('test');
var_dump($result); //结果:string(11) "11111111111"
?>