安装前提:
在安装Redis之前首要先做的是安装Unix的Tcl工具,如果不安装的话后期将无法对Redis进行测试。
1:安装Redis测试依赖tcl8.6.3
#进入src目录
cd /usr/local/src
#下载资源包
wget http://47.96.19.7/source/redis/tcl8.6.3-src.tar.gz
#解压包
tar -zxvf tcl8.6.3-src.tar.gz
#进入编译目录
cd tcl8.6.3/unix/
#编译配置
./configure
#安装 分两步走
make
make install
2:安装Redis
#进入src目录
cd /usr/local/src
#下载资源包
wget http://47.96.19.7/source/redis/redis-3.0.5.tar.gz
#解压包
tar -zxvf redis-3.0.5.tar.gz
#进入编译目录
cd redis-3.0.5
#安装到/usr/local/redis下
make
make PREFIX=/usr/local/redis install
3:测试安装
cd /usr/local/src/redis-3.0.5/src
make test
显示测试成功!
4:配置Redis
cd /usr/local/src/redis-3.0.5/
cp redis.conf /usr/local/redis/
vim /usr/local/redis/redis.conf
#修改后,redis作为守护进程在服务端运行
daemonize yes
#修改默认的端口号6379为6399
port 6399
5:设置redis服务器自启动
#创建redis启动命令
vim /etc/init.d/redis
复制以下代码
# chkconfig: 2345 10 90
# description: Start and Stop redis
PATH=/usr/local/bin:/sbin:/usr/bin:/bin
REDISPORT=6399 #实际环境而定
EXEC=/usr/local/redis/bin/redis-server #实际环境而定
REDIS_CLI=/usr/local/redis/bin/redis-cli #实际环境而定
PIDFILE=/var/run/redis.pid
CONF="/usr/local/redis/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
exitxit 1
esac
赋予权限
chmod +x /etc/init.d/redis
开启服务自启动
chkconfig redis on
6:启动或关闭服务
service redis start
#没有设置密码的关闭方式
service redis stop
#设置了密码的关闭方式
/usr/local/bin/redis-cli -h 127.0.0.1 -p 6399 -a yourpassword shutdown
7:测试redis数据写入和读取
cd /usr/local/redis/bin
./redis-cli -p 6399
127.0.0.1:6399> set test 123
OK
127.0.0.1:6399> get test
"123"
127.0.0.1:6399> exit
测试成功!
其他:启动redis错误处理
A-overcommit_memory错误
WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect.
解决方法:
编辑/etc/sysctl.conf ,改vm.overcommit_memory=1,然后sysctl -p 使配置文件生效
B-TCP设置太小问题
The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128
解决方法:
编辑/etc/sysctl.conf ,添net.core.somaxconn= 1024 然后执行sysctl -p
另外一种安装:
安装编译php7
1.下载
下载地址:http://pecl.php.net/package/redis
下载目录我放在/data/download/php下
cd到下载目录,下载:wget http://pecl.php.net/get/redis-3.1.2.tgz
2.解压
解压到当前目录 tar -zxvf redis-3.1.2.tgz
3.安装
cd redis-3.1.2目录下
在该目录下用phpize生成configure配置文件:直接运行/usr/local/php7/bin/phpize
运行/usr/local/php7/bin/phpize时出现:
Configuring for:
PHP Api Version: 20041225
Zend Module Api No: 20060613
Zend Extension Api No: 220060519
Cannot find autoconf. Please check your autoconf installation and the
$PHP_AUTOCONF environment variable. Then, rerun this script.
cd /usr/src
wget http://ftp.gnu.org/gnu/m4/m4-1.4.9.tar.gz
tar -zvxf m4-1.4.9.tar.gz
cd m4-1.4.9/
./configure && make && make install
cd ../
wget http://ftp.gnu.org/gnu/autoconf/autoconf-2.62.tar.gz
tar -zvxf autoconf-2.62.tar.gz
cd autoconf-2.62/
./configure && make && make install
yum install m4
yum install autoconf
然后就是配置、编译、安装全部在该目录下完成
./configure --with-php-config=/usr/local/php7/bin/php-config
make
make install
make install后会看到Installing shared extensions: /usr/local/php7/lib/php/extensions/no-debug-non-zts-20160303/
该目录就是redis.so文件的生成目录
ll /usr/local/php7/lib/php/extensions/no-debug-non-zts-20160303/
看到redis.so就说明安装成功了
配置支持PHP7:
vim /usr/local/php7/etc/php.ini
添加extension=redis.so
:wq!
重启php-fpm