当前位置: 首页 > news >正文

CentOS7安装Redis

安装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 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、修改配置

vi /opt/redis/redis.conf

# 修改如下内容
# bind 127.0.0.1
bind 0.0.0.0
# 配置密码
requirepass your_password

2、修改启动脚本

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、启动服务
/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");
?>
2、浏览器访问测试

相关文章:

  • C语言游戏实战(12):植物大战僵尸(坤版)
  • 深度学习中特征(tensor)维度转换
  • 数据链路层简单介绍
  • 阿里云数据库 SelectDB 版全面商业化,开启现代化实时数据仓库的全新篇章
  • 向郭老师学习研发项目管理
  • 赶紧收藏!2024 年最常见 20道 Redis面试题(三)
  • 文件搜索相关命令
  • 5.23小结
  • 老题重测,国产AI大模型从“智障”走向“智能”?
  • JS对象超细
  • 基于springboot+vue的学生考勤管理系统
  • LP-MSPM03507学习资料汇总
  • 2024年5月23号PMP每日三题含答案
  • 关于如何创建一个可配置的 SpringBoot Web 项目的全局异常处理
  • 创建vue工程、Vue项目的目录结构、Vue项目-启动、API风格
  • QComboBox
  • 专题汇编 | ChatGPT引领AIGC新浪潮(一)
  • React Query
  • Codeforces Round 821 (Div. 2) C. Parity Shuffle Sorting (构造之全变成一样的)
  • Python 全栈体系【四阶】(五十二)
  • 东莞网站建设公司/做网站seo怎么赚钱
  • 台州网站制作台州网站建设/某个产品营销推广方案
  • 静态网站策划书/免费下载百度到桌面
  • 免费seo网站的工具/谷歌搜索入口
  • 商城 网站 功能/搜索引擎优化排名案例
  • 豪柏大厦做网站的公司/市场营销公司