欢迎投稿

今日深度:

CentOS7 安装Redis PHP扩展Redis,centos7redis

CentOS7 安装Redis PHP扩展Redis,centos7redis


1、安装Redis

1.1 创建并进入redis目录

[root@i****z ~]# mkdir /usr/local/redis
[root@i****z ~]# cd /usr/local/redis/
[root@i****z redis]# 

 1.2 下载并解压redis Redis官网

[root@i****z redis]# wget http://download.redis.io/releases/redis-4.0.11.tar.gz
[root@i****z redis]# tar zxzf redis-4.0.11.tar.gz
[root@i****z redis]# cd redis-4.0.11

1.3 编译安装

[root@i****z redis-4.0.11]# make
******************
省略中间数据
******************
Hint: It's a good idea to run 'make test' ;)

make[1]: Leaving directory `/usr/local/redis/redis-4.0.11/src'
[root@i****z redis-4.0.11]# 

1.4 启动redis服务(测试是否安装成功,redis默认端口为6379,Ctrl+C结束服务)

[root@i****z redis-4.0.11]# src/redis-server
12482:C 19 Sep 21:28:10.682 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
12482:C 19 Sep 21:28:10.682 # Redis version=4.0.11, bits=64, commit=00000000, modified=0, pid=12482, just started
12482:C 19 Sep 21:28:10.682 # Warning: no config file specified, using the default config. In order to specify a config file use src/redis-server /path/to/redis.conf
                _._                                                  
           _.-``__ ''-._                                             
      _.-``    `.  `_.  ''-._           Redis 4.0.11 (00000000/0) 64 bit
  .-`` .-```.  ```\/    _.,_ ''-._                                   
 (    '      ,       .-`  | `,    )     Running in standalone mode
 |`-._`-...-` __...-.``-._|'` _.-'|     Port: 6379
 |    `-._   `._    /     _.-'    |     PID: 12482
  `-._    `-._  `-./  _.-'    _.-'                                   
 |`-._`-._    `-.__.-'    _.-'_.-'|                                  
 |    `-._`-._        _.-'_.-'    |           http://redis.io        
  `-._    `-._`-.__.-'_.-'    _.-'                                   
 |`-._`-._    `-.__.-'    _.-'_.-'|                                  
 |    `-._`-._        _.-'_.-'    |                                  
  `-._    `-._`-.__.-'_.-'    _.-'                                   
      `-._    `-.__.-'    _.-'                                       
          `-._        _.-'                                           
              `-.__.-'                                               

12482:M 19 Sep 21:28:10.683 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
12482:M 19 Sep 21:28:10.683 # Server initialized
12482:M 19 Sep 21:28:10.683 # 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.
12482:M 19 Sep 21:28:10.683 # WARNING you have Transparent Huge Pages (THP) support enabled in your kernel. This will create latency and memory usage issues with Redis. To fix this issue run the command 'echo never > /sys/kernel/mm/transparent_hugepage/enabled' as root, and add it to your /etc/rc.local in order to retain the setting after a reboot. Redis must be restarted after THP is disabled.
12482:M 19 Sep 21:28:10.683 * Ready to accept connections

1.5 启动redis服务后该终端不做任何操作,打开一个新的终端连接到服务器进行测试,如果安装成功,测试结果示例如下

Welcome to Alibaba Cloud Elastic Compute Service !

[root@i****z ~]# cd /usr/local/redis/redis-4.0.11
[root@i****z redis-4.0.11]# src/redis-cli 
127.0.0.1:6379> set foo bar
OK
127.0.0.1:6379> get foo
"bar"
127.0.0.1:6379> 

1.6 走到此步,说明redis安装成功,但是redis-server还不能后台服务运行,所以设置以redis-server以后台进程方式运行

# /etc/ 目录为配置文件目录
# 复制redis.conf文件到配置文件目录下,没有请先创建文件目录(也可以直接复制到/etc/目录下,依据个人喜好)
[root@i****z redis-4.0.11]# cp redis.conf /etc/redis/

# 编辑配置文件
[root@i****z redis-4.0.11]# vim /etc/redis/redis.conf
将 daemonize on 修改为 daemonize yes

# 保存退出

1.7 以后台服务形式启动redis-server

[root@i****z redis-4.0.11]# src/redis-server /etc/redis/redis.conf 
12530:C 19 Sep 21:51:23.096 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
12530:C 19 Sep 21:51:23.096 # Redis version=4.0.11, bits=64, commit=00000000, modified=0, pid=12530, just started
12530:C 19 Sep 21:51:23.096 # Configuration loaded
[root@i****z redis-4.0.11]# 

1.8 测试是否修改成功(此处只需在一个终端操作即可)

[root@i****z redis-4.0.11]# src/redis-cli 
127.0.0.1:6379> set foo bar
OK
127.0.0.1:6379> get foo
"bar"
127.0.0.1:6379> exit
[root@i****z redis-4.0.11]# 

1.9 设置开机启动

[root@i****z redis-4.0.11]# cp /usr/local/redis/redis-4.0.11/utils/redis_init_script /etc/init.d/redis
[root@i****z redis-4.0.11]# cd /etc/init.d/

# 编辑脚本,修改内容在下文有做提示
[root@i****z init.d]# vim redis
#!/bin/sh
#
# Simple Redis init.d script conceived to work on Linux systems
# as it does use of the /proc filesystem.

### BEGIN INIT INFO
# Provides:     redis_6379
# Default-Start:        2 3 4 5
# Default-Stop:         0 1 6
# Short-Description:    Redis data structure server
# Description:          Redis data structure server. See https://redis.io
### END INIT INFO

####################中间为处理的内容#######################
# 端口
REDISPORT=6379
# 服务启动路径,可以修改该路径,也可以选择复制文件到对应目录下
# 这里我选择复制文件:cp /usr/local/redis/redis-4.0.11/src/redis-server /usr/local/bin/
EXEC=/usr/local/bin/redis-server

# 客户端路径,同上:cp /usr/local/redis/redis-4.0.11/src/redis-cli /usr/local/bin/
CLIEXEC=/usr/local/bin/redis-cli

# 记录redis进程文件
PIDFILE=/var/run/redis_${REDISPORT}.pid

# 配置文件,前面已经对配置文件做了处理
# 这里我选择将
CONF="/etc/redis/${REDISPORT}.conf"
# 修改为
CONF="/etc/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
        ;;
    stop)
        if [ ! -f $PIDFILE ]
        then
                echo "$PIDFILE does not exist, process is not running"
        else
                PID=$(cat $PIDFILE)
                echo "Stopping ..."
                $CLIEXEC -p $REDISPORT shutdown
                while [ -x /proc/${PID} ]
                do
                    echo "Waiting for Redis to shutdown ..."
                    sleep 1
                done
                echo "Redis stopped"
        fi
        ;;
    *)
        echo "Please use start or stop as first argument"
        ;;
esac
# 注册系统服务
[root@i****z init.d]# chkconfig --add redis

# 启动Redis
[root@i****z init.d]# service redis start
Starting Redis server...
2357:C 19 Sep 22:31:49.213 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
2357:C 19 Sep 22:31:49.213 # Redis version=4.0.11, bits=64, commit=00000000, modified=0, pid=2357, just started
2357:C 19 Sep 22:31:49.213 # Configuration loaded

# 停止Redis
[root@i****z init.d]# service redis stop
Stopping ...
Redis stopped
[root@i****z init.d]# 

1.10 重启服务检测是否设置成功

# 重启
[root@i****z init.d]# reboot

# 重启后
Welcome to Alibaba Cloud Elastic Compute Service !

[root@i****z ~]# redis-cli
127.0.0.1:6379> set foo bar
OK
127.0.0.1:6379> get f
(nil)
127.0.0.1:6379> get foo
"bar"
127.0.0.1:6379> exit
[root@i****z ~]# 

1.11 Redis安装完成


2、安装php-redis扩展

2.1 下载php-redis扩展文件  http://pecl.php.net/package/redis

[root@i****z ~]# mkdir /usr/local/php/php-redis
[root@i****z ~]# cd /usr/local/php/php-redis/
[root@i****z php-redis]# wget http://pecl.php.net/get/redis-4.1.0.tgz
--2018-09-26 22:13:08--  http://pecl.php.net/get/redis-4.1.0.tgz
Resolving pecl.php.net (pecl.php.net)... 104.236.228.160
Connecting to pecl.php.net (pecl.php.net)|104.236.228.160|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 220774 (216K) [application/octet-stream]
Saving to: ‘redis-4.1.0.tgz’

100%[============================================================================>] 220,774      144KB/s   in 1.5s   

2018-09-26 22:13:12 (144 KB/s) - ‘redis-4.1.0.tgz’ saved [220774/220774]

[root@i****z php-redis]# 

2.2 解压后进入文件目录

[root@i****z php-redis]# tar zxvf redis-4.1.0.tgz 
[root@i****z php-redis]# cd redis-4.1.0
[root@i****z redis-4.1.0]# 

2.3 生成configure配置文件

[root@i****z redis-4.1.0]# /usr/local/php/bin/phpize 
Configuring for:
PHP Api Version:         20151012
Zend Module Api No:      20151012
Zend Extension Api No:   320151012
Cannot find autoconf. Please check your autoconf installation and the
$PHP_AUTOCONF environment variable. Then, rerun this script.

# 出现上面Cannot问题,安装autoconf
[root@i****z redis-4.1.0]# yum install autoconf
Loaded plugins: fastestmirror

******************
省略中间数据
******************
Total download size: 748 k
Installed size: 2.3 M
Is this ok [y/d/N]: y
Downloading packages:

******************
省略中间数据
******************
Installed:
  autoconf.noarch 0:2.69-11.el7                    
Dependency Installed:
  perl-Data-Dumper.x86_64 0:2.145-3.el7  
Complete!

# 再次执行
[root@i****z redis-4.1.0]# /usr/local/php/bin/phpize 
Configuring for:
PHP Api Version:         20151012
Zend Module Api No:      20151012
Zend Extension Api No:   320151012
[root@i****z redis-4.1.0]# 

2.4 配置环境、编译

[root@i****z redis-4.1.0]# ./configure --with-php-config=/usr/local/php/bin/php-config 
******************
省略中间数据
******************
creating libtool
appending configuration tag "CXX" to libtool
configure: creating ./config.status
config.status: creating config.h

# 编译
[root@i****z redis-4.1.0]# make && make install
******************
省略中间数据
******************
Build complete.
Don't forget to run 'make test'.

Installing shared extensions:     /usr/local/php/lib/php/extensions/no-debug-non-zts-20151012/
[root@i****z redis-4.1.0]# 

2.5 修改php.ini文件

[root@i****z redis-4.1.0]# vim /usr/local/php/etc/php.ini 

# 添加下面代码,路径查看前面编译返回的路径
extension_dir="/usr/local/php/lib/php/extensions/no-debug-non-zts-20151012/"
extension=redis.so

2.6 重启nginx、php-fpm服务

[root@i****z redis-4.1.0]# systemctl restart nginx
[root@i****z redis-4.1.0]# service php-fpm restart
Restarting php-fpm (via systemctl):                        [  OK  ]

2.7 查看是否添加成功,可以通过php -m查看,也可以phpinfo();查看,phpinfo();查看的版本实际上是这个扩展的版本

[root@i****z redis-4.1.0]# php -m
[PHP Modules]
******************
省略中间数据
******************
posix
readline
redis
Reflection
session
zlib
******************
省略中间数据
******************
[Zend Modules]

[root@i****z redis-4.1.0]# 

 

www.htsjk.Com true http://www.htsjk.com/redis/25641.html NewsArticle CentOS7 安装Redis PHP扩展Redis,centos7redis 1、安装Redis 1.1 创建并进入redis目录 [root@i****z ~]# mkdir /usr/local/redis[root@i****z ~]# cd /usr/local/redis/[root@i****z redis]#  1.2 下载并解压redis Redis官网 [root@...
相关文章
    暂无相关文章
评论暂时关闭