安装Redis,并使用PHP连接Redis
一、准备工作
1、安装LNMP
二、安装Redis
1、下载Redis二进制包
bash
wget https://download.redis.io/releases/redis-5.0.12.tar.gz
2、解压
bash
tar -zxvf redis-5.0.12.tar.gz
mv redis-5.0.12 /opt/redis
3、编译安装
3.1 编译
bash
cd /opt/redis
make
3.2 安装
bash
make install
3.3 初始化redis
bash
# cd utils/
# ./install-server.sh
Welcome to the redis service installer
This script will help you easily set up a running redis server
Please select the redis port for this instance: [6379]
Selecting default: 6379
Please select the redis config file name [/opt/redis/6379.conf]
Selected default - /etc/redis/6379.conf
Please select the redis log file name [/var/log/redis_6379.log]
Selected default - /var/log/redis_6379.log
Please select the data directory for this instance [/var/lib/redis/6379]
Selected default - /var/lib/redis/6379
Please select the redis executable path [/usr/local/bin/redis-server]
Selected config:
Port : 6379
Config file : /opt/redis/6379.conf
Log file : /var/log/redis_6379.log
Data dir : /var/lib/redis/6379
Executable : /usr/local/bin/redis-server
Cli Executable : /usr/local/bin/redis-cli
4、修改配置文件
1、修改配置
bash
vi /opt/redis/redis.conf
# 修改如下内容
# bind 127.0.0.1
bind 0.0.0.0
# 配置密码
requirepass your_password
2、修改启动脚本
bash
vi /etc/init.d/redis_6379
EXEC=/usr/local/bin/redis-server
CLIEXEC=/usr/local/bin/redis-cli
PIDFILE=/var/run/redis_6379.pid
CONF="/etc/redis/6379.conf"
REDISPORT="6379"
# 添加密码变量,密码需要和配置文件中设置的一致
PASSWORD="final123"
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
;;
stop)
if [ ! -f $PIDFILE ]
then
echo "$PIDFILE does not exist, process is not running"
else
PID=$(cat $PIDFILE)
echo "Stopping ..."
# 添加密码认证
$CLIEXEC -p $REDISPORT -a $PASSWORD shutdown
while [ -x /proc/${PID} ]
do
echo "Waiting for Redis to shutdown ..."
sleep 1
done
echo "Redis stopped"
fi
;;
status)
PID=$(cat $PIDFILE)
if [ ! -x /proc/${PID} ]
then
echo 'Redis is not running'
else
echo "Redis is running ($PID)"
fi
;;
restart)
$0 stop
$0 start
;;
*)
echo "Please use start, stop, restart or status as first argument"
;;
esac
5、启动服务
bash
/etc/init.d/redis_6379 start
# 支持stop、restart、status
6、测试
bash
# 登录redis服务
[root@localhost opt]# redis-cli
127.0.0.1:6379> auth password
OK
三、安装PHP-Redis
1、安装依赖包
bash
yum -y install php-devel
2、下载二进制安装包
bash
下载地址:
https://github.com/phpredis/phpredis/releases/tag/6.0.2
3、解压
bash
tar -zxvf phpredis-6.0.2.tar.gz
4、执行phpize
bash
cd phpredis-6.0.2/
mkdir -p /opt/php-redis/
phpize
5、编译三部曲
5.1 执行编译脚本
bash
# --with-php-config 获取php配置信息,以方便正确编译和链接到PHP
./configure --prefix=/opt/php-redis/ --with-php-config=/opt/php/bin/php-config
5.2 编译
bash
make
5.3 安装
bash
make install
6、修改php配置文件
bash
vi /opt/php/lib/php.ini
# 添加如下内容
## 可以通过 find / -name "redis.so" 查找到所在路径
extension_dir="/opt/php/lib/php/extensions/no-debug-non-zts-20180731/"
extension=redis.so
7、重启php服务
bash
systemctl restart php-frm.service
四、测试
1、编写测试脚本
bash
vi /opt/www/php/redis.php
<?php
$redis = new redis();
$redis->connect("REDIS_IP","6379");
$redis->auth("YOUR_PASSWORD");
$redis->set("TEST","TEST-REDIS");
echo $redis->get("TEST");
?>