How to install redis under linux and extend redis in PHP

墨辰丷
Release: 2023-03-25 19:42:01
Original
1604 people have browsed it

本篇文章主要介绍如何在linux下安装redis以及PHP中扩展redis,感兴趣的朋友参考下,希望对大家有所帮助。

第一部分:安装redis 
希望将redis安装到此目录 

/usr/local/redis
Copy after login

希望将安装包下载到此目录

/usr/local/src
Copy after login

那么安装过程指令如下:

$ mkdir /usr/local/redis  
$ cd /usr/local/src  
$ wget http://redis.googlecode.com/files/redis-2.6.14.tar.gz  
$ tar xzf redis-2.6.14.tar.gz   
$ ln -s redis-2.6.14 redis #建立一个链接  
$ cd redis  
$ make PREFIX=/usr/local/redis install #安装到指定目录中
Copy after login

注意上面的最后一行,我们通过PREFIX指定了安装的目录。如果make失败,一般是你们系统中还未安装gcc,那么可以通过yum安装:

yum install gcc
Copy after login

安装完成后,继续执行make.

在安装redis成功后,你将可以在/usr/local/redis看到一个bin的目录,里面包括了以下文件:

redis-benchmark  redis-check-aof  redis-check-dump  redis-cli  redis-server
Copy after login

1.复制脚本到/etc/rc.d/init.d目录
第二部分:将redis做成一个服务

ps: /etc/rc.d/init.d/目录下的脚本就类似与windows中的注册表,在系统启动的时候某些指定脚本将被执行
按以上步骤安装Redis时,其服务脚本位于:

/usr/local/src/redis/utils/redis_init_script
Copy after login

必须将其复制到/etc/rc.d/init.d的目录下:

cp /usr/local/src/redis/utils/redis_init_script /etc/rc.d/init.d/redis
Copy after login

如果这时添加注册服务:
将redis_init_script复制到/etc/rc.d/init.d/,同时易名为redis。

chkconfig --add redis
Copy after login

将报以下错误:

redis服务不支持chkconfig
Copy after login

2.更改redis脚本
为此,我们需要更改redis脚本。

打开使用vi打开脚本,查看脚本信息:

vim /etc/rc.d/init.d/redis
Copy after login

看到的内容如下(下内容是更改好的信息):

#!/bin/sh 
#chkconfig: 2345 80 90 
# Simple Redis init.d script conceived to work on Linux systems 
# as it does use of the /proc filesystem. 
   
REDISPORT=6379 
EXEC=/usr/local/redis/bin/redis-server 
CLIEXEC=/usr/local/redis/bin/redis-cli 
   
PIDFILE=/var/run/redis_${REDISPORT}.pid 
CONF="/etc/redis/${REDISPORT}.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
Copy after login

1.原文件是没有以下第2行的内容的,
和原配置文件相比:

#chkconfig: 2345 80 90
EXEC=/usr/local/redis/bin/redis-server   
CLIEXEC=/usr/local/redis/bin/redis-cli
Copy after login

2.原文件EXEC、CLIEXEC参数,也是有所更改。


3.redis开启的命令,以后台运行的方式执行。

$EXEC $CONF &
Copy after login

占据在前台,占用了主用户界面,造成其它的命令执行不了。
ps:注意后面的那个“&”,即是将服务转到后面运行的意思,否则启动服务时,Redis服务将

4.将redis配置文件拷贝到/etc/redis/${REDISPORT}.conf


这样,redis服务脚本指定的CONF就存在了。默认情况下,Redis未启用认证,可以通过开启6379.conf的requirepass 指定一个验证密码。

以上操作完成后,即可注册yedis服务:

chkconfig --add redis
Copy after login
service redis start
Copy after login

3.启动redis服务

第三,将Redis的命令所在目录添加到系统参数PATH中

修改profile文件:

vi /etc/profile
Copy after login

在最后行追加:

export PATH="$PATH:/usr/local/redis/bin"
Copy after login

然后马上应用这个文件:

. /etc/profile
Copy after login

这样就可以直接调用redis-cli的命令了,如下所示:
执行上一步的时候遇到权限不够:chmod 777 /etc/profile

$ redis-cli   
redis 127.0.0.1:6379> auth superman   
OK   
redis 127.0.0.1:6379> ping   
PONG   
redis 127.0.0.1:6379>
Copy after login

至此,redis 就成功安装了。

 

下载:https://github.com/nicolasff/phpredis/archive/2.2.4.tar.gz

Upload phpredis-2.2.4.tar.gz to the /usr/local/src directory

cd /usr/local/src #Enter the software package storage directory

tar zxvf phpredis- 2.2.4.tar.gz #Unzip

cd phpredis-2.2.4 #Enter the installation directory

/usr/local/php/bin/phpize #Use phpize to generate the configure configuration file, directory May be different. phpize is an executable file in the bin directory in php, so you must first find the corresponding directory.

./configure --with-php-config=/usr/local/php/bin/php-config #Configuration, please make sure that /usr/local/php/bin/php-config exists first. Corresponding to php-config in the bin directory of php

make #Compile

make install #Installation

After the installation is completed, the following installation path appears

/usr/local/php/lib/php/extensions/no-debug-non-zts-20090626/

may also appear
Installing shared extensions: /usr/lib64/php/modules/

This is normal, continue below.

2. Configure php support

vi /usr/local/php/etc/php.ini #Edit the configuration file and add the following content to the last line

Add

extension="redis.so"

:wq! #Save and exit

Restart httpd------------->ok


Related recommendations:

php Processing redis

Detailed explanation of redis application cases in nodejs

Detailed explanation of flash sale products with PHP combined with redis

The above is the detailed content of How to install redis under linux and extend redis in PHP. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template