Redis服务器简单搭建

2018-07-17 70次浏览 发表评论

安装依赖:

yum install gcc gcc-c++

Redis服务器安装

下载地址:http://download.redis.io/releases/

安装

wget http://download.redis.io/releases/redis-3.2.11.tar.gz
tar zxvf redis-3.2.11.tar.gz
cd redis-3.2.11
make PREFIX=/usr/local/redis install
mkdir -p /usr/local/redis/etc/
\cp redis.conf /usr/local/redis/etc/

设置后台启动

sed -i 's/daemonize no/daemonize yes/g' /usr/local/redis/etc/redis.conf
sed -i 's#^pidfile /var/run/redis_6379.pid#pidfile /var/run/redis.pid#g' /usr/local/redis/etc/redis.conf
bind 0.0.0.0            # 允许远程连接
requirepass password    # 设置密码

服务脚本

vim /etc/init.d/redis
#!/bin/bash
#   
# redis - this script starts and stops the redis-server daemon
#   
# chkconfig:    2345 80 90
# description:  Redis is a persistent key-value database
#   
### BEGIN INIT INFO
# Provides:          redis
# Required-Start:    $syslog
# Required-Stop:     $syslog
# Should-Start:        $local_fs
# Should-Stop:        $local_fs
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description:    redis-server daemon
# Description:        redis-server daemon
### END INIT INFO

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 -n "Starting Redis server..."
            $EXEC $CONF
            if [ "$?"="0" ]; then
                echo " done"
            else
                echo " failed"
            fi
        fi
        ;;
    stop)
        if [ ! -f "$PIDFILE" ]; then
            echo "$PIDFILE does not exist, process is not running"
        else
            PID=$(cat $PIDFILE)
            echo "Stopping  Redis server..."
            $REDIS_CLI -a "password" -p $REDISPORT shutdown     # -a 后面是设置的密码
            if [ "$?"="0" ]; then
                echo " done"
            else
                echo " failed"
            fi
        fi
        ;;
    restart)
        ${0} stop
        ${0} start
        ;;
    kill)
        echo "force kill redis server..."
        killall redis-server
        if [ "$?"="0" ]; then
            echo " done"
        else
            echo " failed"
        fi
        ;;
    status)
        if [ -f "$PIDFILE" ]; then
            echo "Redis server is running."
        else
            echo "Redis server is stopped."
        fi
        ;;
  *)
       echo "Usage: /etc/init.d/redis {start|stop|restart|status|kill}" >&2
       exit 1
esac
chmod +x /etc/init.d/redis

PHP安装redis扩展

下载地址:https://github.com/phpredis/phpredis/releases

安装

wget https://github.com/phpredis/phpredis/archive/3.1.4.tar.gz
cd phpredis-3.1.4
/usr/local/php/bin/phpize
./configure --with-php-config=/usr/local/php/bin/php-config
make && make install

修改php.ini文件

vim /usr/local/php/lib/php.ini

增加如下内容:

extension_dir = "/usr/local/php/lib/php/extensions/no-debug-zts-20090626"
extension=redis.so

重启PHP

评论

随便说点,聊聊天~