centos编译安装Redis和PHPredis
网络上很多安装redis教程参差不齐,于是自己便总结一份以留作以后查阅之用。
安装redis
下载redis
wget http://download.redis.io/releases/redis-3.0.0.tar.gz
编译源程序
tar zxvf redis-3.0.0.tar.gz
cd redis-3.0.0
make
cd src
make install
创建redis目录,移动文件,便于以后管理
mkdir -p /usr/local/redis/bin
mkdir -p /usr/local/redis/etc
mv redis.conf /usr/local/redis/etc
mv mkreleasehdr.sh redis-benchmark redis-check-aof redis-check-dump redis-cli redis-server /usr/local/redis/bin
默认情况下,redis不是在后台运行的,我们需要把开启的redis后台运行 vi /usr/local/redis/etc/redis.conf
将daemonize的值改为yes。另外,为了安全起见,可以将redis设置为仅本机访问。将"#bind 127.0.0.1"前的"#"号去掉即可
编写脚本,vi /etc/init.d/redis
输入一下内容并保存
`
# chkconfig: 2345 10 90
# description: Start and Stop redis
PATH=/usr/local/bin:/sbin:/usr/bin:/bin
REDISPORT=6379 #实际环境而定
EXEC=/usr/local/redis/bin/redis-server #实际环境而定
REDIS_CLI=/usr/local/redis/bin/redis-cli #实际环境而定
PIDFILE=/var/run/redis.pid
CONF="/usr/local/redis/etc/redis.conf" #实际环境而定
case "$1" in
start)
if [ -f $PIDFILE ]
then
echo "$PIDFILE exists, process is already running or crashed."
else
echo "Starting Redis server..."
$EXEC $CONF
fi
if [ "$?"="0" ]
then
echo "Redis is running..."
fi
;;
stop)
if [ ! -f $PIDFILE ]
then
echo "$PIDFILE exists, process is not running."
else
PID=$(cat $PIDFILE)
echo "Stopping..."
$REDIS_CLI -p $REDISPORT SHUTDOWN
while [ -x $PIDFILE ]
do
echo "Waiting for Redis to shutdown..."
sleep 1
done
echo "Redis stopped"
fi
;;
restart|force-reload)
${0} stop
${0} start
;;
*)
echo "Usage: /etc/init.d/redis {start|stop|restart|force-reload}" >&2
exit 1
esac
`
保存后赋予执行权限: chmod +x /etc/init.d/redis
启动
# 尝试启动或停止redis
service redis start
service redis stop
# 开启服务自启动
chkconfig redis on
PHPredis扩展安装
下载扩展包 wget https://github.com/phpredis/phpredis/archive/4.2.0.tar.gz
解压安装redis扩展
tar -zxvf 4.2.0.tar.gz
cd phpredis-4.2.0
phpize
./configure --with-php-config=/usr/bin/php-config
make
make install
配置PHP的配置文件,添加redis扩展 vi /etc/php.ini
末尾添加 extension="redis.so"
重启httpd服务让扩展生效 service httpd restart
21313
刚才用字典写了一个存储上下文的东西,调试的时候好好的。到真的运行的时候发现一个大大的BUG[生病]多进程访问的变量都乱了得
无锁的情况下多进程访问是有风险的了,大概自己实现锁的机制,不要同时读写会好点
通过实战应用觉得还是predis比较好用,长连接比较稳定
有机会我也试试predis