环境: centos7 PHP7
1.切到准备安装的目录
cd /usr/local
2.下载Redis
wget http://download.redis.io/redis-stable.tar.gz
3.解压
tar -zxf redis-stable.tar.gz
4.编译
make
5.休息十秒钟
6.进入src目录
cd src
7.编译检查
make test
8.安装
make install
9.切到外层将目录改名
cd /usr/local
mv redis-stable redis
10.修改配置
vi redis/redis.conf
搜索daemonize 改为 daemonize yes (是否守护进程)
搜索pidfile 改为 pidfile /usr/local/redis/redis.pid
搜索logfile 改为 logfile /usr/local/redis/redis.log
搜索save 60 10000 改为 save 60 1000
搜索#maxmemory <bytes> 改为 maxmemory 256M (最大允许使用内存)
远程连接配置:
bind 127.0.0.1 改为 #bind 127.0.0.1 (注释掉)
protected-mode yes 改为 protected-mode no
requirepass yourpassword (密码)
11.启动Redis
/usr/local/redis/src/redis-server
12.开机启动
vi /etc/init.d/redis
输入以下内容 路径换成实际路径:
#!/bin/bash
# chkconfig: 2345 10 90
# description: Start and Stop redis
PATH=/usr/local/bin:/sbin:/usr/bin:/bin
REDISPORT=6379
EXEC=/usr/local/redis/src/redis-server
REDIS_CLI=/usr/local/redis/src/redis-cli
PIDFILE=/usr/local/redis/redis.pid
CONF="/usr/local/redis/redis.conf"
AUTH="1234"
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
sleep 2
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
保存退出:wq
给个权限:chmod 755 /etc/init.d/redis
13.开启服务
/etc/init.d/redis start
如果看到以下信息则成功
Starting Redis server...
Redis is running...
14.启动客户端:
/usr/local/redis/src/redis-cli
用一下set get
存储数据正常
15.设置开机自启
chkconfig redis on
安装Redis扩展
16.下载地址:
https://github.com/phpredis/phpredis
用ftp传到Linux (或者是右键点击 Download ZIP 按钮 复制链接 使用wget直接下载)
17.移动到准备安装的目录
mv develop.zip /usr/local/develop.zip
18.解压
unzip develop.zip
19.进入目录中
cd phpredis-develop
20.执行PHP安装目录下的phpize
/usr/local/php7/bin/phpize
21.报错信息:
Cannot find autoconf. Please check your autoconf installation and the
$PHP_AUTOCONF environment variable. Then, rerun this script.
解决办法:
yum install m4
yum install autoconf
再次执行phpize:
/usr/local/php7/bin/phpize
22.搜索php-config路径
find /usr/local/php7/ -name php-config
得到/usr/local/php7/bin/php-config
23.配置
./configure --with-php-config=/usr/local/php7/bin/php-config
24.编译
make
25.安装
make install
出现如下结果:
26.编辑php.ini
vi /usr/local/php7/etc/php.ini
在一排extension的地方加入:
extension=redis.so
27.重启Apache
systemctl restart httpd
若是Nginx
杀掉php-fpm进程
ps -ef | grep php-fpm
kill -9 ***
重新启动php-fpm
/usr/local/php7/sbin/php-fpm -c /usr/local/php7/etc/php.ini
28.新建PHP文件运行phpinfo()
搜索有无Redis扩展
29.运行以下代码 如果存取正常 则安装成功
$redis = new Redis();
$redis->connect('127.0.0.1', 6379);
$redis->set("name", "hello world");
echo $redis->get("name");
感谢阅读