安装Redis,并使用PHP连接Redis
一、准备工作
1、安装LNMP
参考:搭建LNMP服务器-CSDN博客文章浏览阅读876次,点赞14次,收藏4次。LNMP 架构通常用于构建高性能、可扩展的 Web 应用程序。Nginx 作为前端 Web 服务器,负责处理 HTTP 请求和响应,并将请求代理到后端 PHP 应用程序(通过 FastCGI、PHP-FPM 等)。PHP 应用程序与 MySQL数据库进行交互,以存储和检索数据。https://blog.csdn.net/weixin_44295677/article/details/138958399?spm=1001.2014.3001.5501
二、安装Redis
1、下载Redis二进制包
wget https://download.redis.io/releases/redis-5.0.12.tar.gz
2、解压
tar -zxvf redis-5.0.12.tar.gz
mv redis-5.0.12 /opt/redis
3、编译安装
3.1 编译
cd /opt/redis
make
3.2 安装
make install
3.3 初始化redis
# cd utils/# ./install-server.sh
Welcome to the redis service installer
This script will help you easily set up a running redis serverPlease 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、修改配置
vi /opt/redis/redis.conf# 修改如下内容
# bind 127.0.0.1
bind 0.0.0.0
# 配置密码
requirepass your_password
2、修改启动脚本
vi /etc/init.d/redis_6379EXEC=/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" instart)if [ -f $PIDFILE ]thenecho "$PIDFILE exists, process is already running or crashed"elseecho "Starting Redis server..."$EXEC $CONFfi;;stop)if [ ! -f $PIDFILE ]thenecho "$PIDFILE does not exist, process is not running"elsePID=$(cat $PIDFILE)echo "Stopping ..."# 添加密码认证$CLIEXEC -p $REDISPORT -a $PASSWORD shutdownwhile [ -x /proc/${PID} ]doecho "Waiting for Redis to shutdown ..."sleep 1doneecho "Redis stopped"fi;;status)PID=$(cat $PIDFILE)if [ ! -x /proc/${PID} ]thenecho 'Redis is not running'elseecho "Redis is running ($PID)"fi;;restart)$0 stop$0 start;;*)echo "Please use start, stop, restart or status as first argument";;
esac
5、启动服务
/etc/init.d/redis_6379 start
# 支持stop、restart、status
6、测试
# 登录redis服务
[root@localhost opt]# redis-cli
127.0.0.1:6379> auth password
OK
三、安装PHP-Redis
1、安装依赖包
yum -y install php-devel
2、下载二进制安装包
下载地址:
https://github.com/phpredis/phpredis/releases/tag/6.0.2
3、解压
tar -zxvf phpredis-6.0.2.tar.gz
4、执行phpize
cd phpredis-6.0.2/
mkdir -p /opt/php-redis/phpize
5、编译三部曲
5.1 执行编译脚本
# --with-php-config 获取php配置信息,以方便正确编译和链接到PHP
./configure --prefix=/opt/php-redis/ --with-php-config=/opt/php/bin/php-config
5.2 编译
make
5.3 安装
make install
6、修改php配置文件
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服务
systemctl restart php-frm.service
四、测试
1、编写测试脚本
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");
?>