从零开始构建支持TLS1.2的Web服务器Linux版(六)选装-Redis(src安装方式)

博文目录

      • 下载Redis
      • 解压-编译-安装
      • 配置
      • 启动Redis

下载Redis

从Redis官网,下载所需版本的Redis,当前最新版本为Redis 4.0.1
通过wget直接下载到服务器:

[root@VM_195_229_centos setup]# wget http://download.redis.io/releases/redis-4.0.1.tar.gz

解压->编译->安装

  1. 解压:# tar -zxvf redis-4.0.1.tar.gz
  2. 编译:make

    [root@VM_195_229_centos setup]# cd redis-4.0.1/
    [root@VM_195_229_centos redis-4.0.1]# ll
    total 284
    -rw-rw-r--  1 root root 127778 Jul 24 21:58 00-RELEASENOTES
    -rw-rw-r--  1 root root     53 Jul 24 21:58 BUGS
    -rw-rw-r--  1 root root   1815 Jul 24 21:58 CONTRIBUTING
    -rw-rw-r--  1 root root   1487 Jul 24 21:58 COPYING
    drwxrwxr-x  6 root root   4096 Jul 24 21:58 deps
    -rw-rw-r--  1 root root     11 Jul 24 21:58 INSTALL
    -rw-rw-r--  1 root root    151 Jul 24 21:58 Makefile
    -rw-rw-r--  1 root root   4223 Jul 24 21:58 MANIFESTO
    -rw-rw-r--  1 root root  20530 Jul 24 21:58 README.md
    -rw-rw-r--  1 root root  57764 Jul 24 21:58 redis.conf
    -rwxrwxr-x  1 root root    271 Jul 24 21:58 runtest
    -rwxrwxr-x  1 root root    280 Jul 24 21:58 runtest-cluster
    -rwxrwxr-x  1 root root    281 Jul 24 21:58 runtest-sentinel
    -rw-rw-r--  1 root root   7606 Jul 24 21:58 sentinel.conf
    drwxrwxr-x  3 root root   4096 Jul 24 21:58 src
    drwxrwxr-x 10 root root   4096 Jul 24 21:58 tests
    drwxrwxr-x  8 root root   4096 Jul 24 21:58 utils
    [root@VM_195_229_centos redis-4.0.1]# make
    make[3]: gcc: Command not found
    make[3]: *** [net.o] Error 127
    make[3]: Leaving directory `/root/setup/redis-4.0.1/deps/hiredis'
    make[2]: *** [hiredis] Error 2
    make[2]: Leaving directory `/root/setup/redis-4.0.1/deps'
    make[1]: [persist-settings] Error 2 (ignored)
    CC adlist.o
    /bin/sh: cc: command not found
    make[1]: *** [adlist.o] Error 127
    make[1]: Leaving directory `/root/setup/redis-4.0.1/src'
    make: *** [all] Error 2
    [root@VM_195_229_centos redis-4.0.1]#

    因为在编译时提示make[3]: gcc: Command not found,所以需要先安装一下gcc,简单粗暴,使用yum install gcc
    顺带把tcl也安装了吧,编译也是需要的,如果已安装,也不会有影响yum install tcl
    安装好后,再次编译,又一次提示错误:

    [root@VM_195_229_centos redis-4.0.1]# make
    cd src && make all
    make[1]: Entering directory `/root/setup/redis-4.0.1/src'
        CC Makefile.dep
    make[1]: Leaving directory `/root/setup/redis-4.0.1/src'
    make[1]: Entering directory `/root/setup/redis-4.0.1/src'
        CC adlist.o
    In file included from adlist.c:34:0:
    zmalloc.h:50:31: fatal error: jemalloc/jemalloc.h: No such file or directory
     #include 
                                   ^
    compilation terminated.
    make[1]: *** [adlist.o] Error 1
    make[1]: Leaving directory `/root/setup/redis-4.0.1/src'
    make: *** [all] Error 2
    [root@VM_195_229_centos redis-4.0.1]# 

    执行make distclean后,再次执行make:

    [root@VM_195_229_centos redis-4.0.1]# make
    Hint: It's a good idea to run 'make test' ;)
    
    make[1]: Leaving directory `/root/setup/redis-4.0.1/src'
    [root@VM_195_229_centos redis-4.0.1]# make test
    
    \o/ All tests passed without errors!
    
    Cleanup: may take some time... OK
    make[1]: Leaving directory `/root/setup/redis-4.0.1/src'
    [root@VM_195_229_centos redis-4.0.1]# 

    提醒 make test会有点花时间,如果时间紧迫,可以不必test。

  3. 安装:make install

    [root@VM_195_229_centos redis-4.0.1]# make install
    cd src && make install
    make[1]: Entering directory `/root/setup/redis-4.0.1/src'
    
    Hint: It's a good idea to run 'make test' ;)
    
        INSTALL install
        INSTALL install
        INSTALL install
        INSTALL install
        INSTALL install
    make[1]: Leaving directory `/root/setup/redis-4.0.1/src'
    [root@VM_195_229_centos redis-4.0.1]# 
  4. 检查安装结果:redis-server -v

    [root@VM_195_229_centos redis-4.0.1]# redis-server -v
    Redis server v=4.0.1 sha=00000000:0 malloc=jemalloc-4.0.3 bits=64 build=8a9e0b3e8c0b0954
    [root@VM_195_229_centos redis-4.0.1]# 

    如果提示错误,找不到命令,则需要修改/etc/profile/usr/local/bin添加到环境目录中:export PATH=$PATH:/usr/local/bin

配置

  1. 创建目录:mkdir -p /etc/redis /var/redis/data /var/redis/run /var/redis/log

    
    # 配置文件目录,配置文件一般放在/etc/下
    
    mkdir /etc/redis
    
    # dump file 目录,一般放在/var/目录下
    
    mkdir /var/redis/data
    
    # 进程pid目录,一般放在/var/目录下
    
    mkdir /var/redis/run
    
    # log目录,一般放在/var/目录下
    
    mkdir /var/redis/log
  2. 修改配置文件,配置参数
    拷贝解压包下的redis.conf文件至/etc/redis

    [root@VM_195_229_centos redis-4.0.1]# cp -pi redis.conf /etc/redis/
    [root@VM_195_229_centos redis-4.0.1]# cd /etc/redis/

    修改配置文件的如下几项内容:

    • 修改端口(默认6379),此项可以不修改
      • port 6379
    • 修改pid目录为新建目录
      • pidfile /var/redis/run/redis_6379.pid
    • 修改dump目录为新建目录
      • dbfilename dump.rdb
      • # dir ./
      • dir /var/redis/data
    • 修改log存储目录为新建目录
      • logfile /var/redis/log/redis.log
    • 持久化,默认为rdb,可选择是否开启aof,若开启,修改配置文件appendonly
      • appendonly no
      • # The name of the append only file (default: “appendonly.aof”)
      • appendfilename “appendonly.aof”
    • 设置redis密码,安全起见,还是需要设置的
      • requirepass redispassword
    • 公网使用redis,需要放开ip绑定,将绑定注释掉,或指定宽泛的公网ip范围
      • # bind 127.0.0.1
    • 配置redis在background运行
      • daemonize yes

启动Redis

  1. 命令行启动:

    [root@VM_195_229_centos redis]# redis-server /etc/redis/redis.conf

    客户端连接redis:

    [root@VM_195_229_centos ~]# redis-cli 
    127.0.0.1:6379> auth redispassword
    OK
    127.0.0.1:6379> keys "*"
    (empty list or set)
    127.0.0.1:6379> 

    客户端连接成功, 检查各个目录下的文件是否正常生成:

    [root@VM_195_229_centos ~]# ls -lR /var/redis/
    /var/redis/:
    total 12
    drwxr-xr-x 2 root root 4096 Aug 12 15:39 data
    drwxr-xr-x 2 root root 4096 Aug 12 16:18 log
    drwxr-xr-x 2 root root 4096 Aug 12 16:23 run
    
    /var/redis/data:
    total 0
    
    /var/redis/log:
    total 4
    -rw-r--r-- 1 root root 2234 Aug 12 16:23 redis.log
    
    /var/redis/run:
    total 4
    -rw-r--r-- 1 root root 5 Aug 12 16:23 redis_6379.pid
    [root@VM_195_229_centos ~]# 

    检查发现/var/redis/data/下面没有生成dump文件,将redis停止,然后重新启动redis,则可以发现dump文件已正常生成。

    [root@VM_195_229_centos ~]# ps -ef | grep redis
    root      9380     1  0 16:23 ?        00:00:00 redis-server 127.0.0.1:6379
    root     11251  4471  0 16:47 pts/1    00:00:00 grep --color=auto redis
    [root@VM_195_229_centos ~]# kill 9380
    [root@VM_195_229_centos ~]# redis-server /etc/redis/redis.conf 
    [root@VM_195_229_centos ~]# ls -l /var/redis/data
    total 4
    -rw-r--r-- 1 root root 172 Aug 12 16:47 dump.rdb
    [root@VM_195_229_centos ~]# 

    另外,若配置了aof持久化方式,data目录下还会有aof的相关文件。

  2. 配置服务器开机自启动。

    1. 创建redis启动脚本,拷贝压缩包下的初始化脚本,到/etc/init.d/,可修改脚本名,以方便识别:
      cp -pi utils/redis_init_script /etc/init.d/redis

    2. 修改脚本/etc/init.d/redis 的pid及conf路径为实际路径:

      
      # 修改启动脚本文件 /etc/init.d/redis
      
      REDISPORT=6379
      EXEC=/usr/local/bin/redis-server
      CLIEXEC=/usr/local/bin/redis-cli
      
      # 生产环境下,配置时,配置文件、pid等最好加上端口标识,以便区分
      
      PIDFILE=/var/run/redis_${REDISPORT}.pid
      CONF="/etc/redis/redis_${REDISPORT}.conf"
      
      
      # 按照上面的操作方法,需要将redis.conf重命名一下:
      
      [root@VM_195_229_centos redis-4.0.1]# mv /etc/redis/redis.conf /etc/redis/redis_6379.conf
      [root@VM_195_229_centos redis-4.0.1]# ls -l /etc/redis/redis_6379.conf
      -rw-rw-r-- 1 root root 57820 Aug 12 16:23 /etc/redis/redis_6379.conf
      [root@VM_195_229_centos redis-4.0.1]# 

      至此,在/etc/init.d/目录下,已经可以通过service redis start/stop/restart 来操作redis服务了。

    3. 给启动脚本添加权限:chmod +x /etc/init.d/redis

    4. 设置开机自启动Redis

      • 修改/etc/init.d/redis, 向其中加入redis启动优先级信息

        
        # chkconfig: 2345 90 10
        
        
        # description: Redis is a persistant key-value database.
        
        
        REDISPORT=6379
        EXEC=/usr/local/bin/redis-server
        CLIEXEC=/usr/local/bin/redis-cli
      • 执行chkconfig redis on

      • 至此,自启动配置完毕。

参考
1. CentOS6.5下Redis安装与配置

你可能感兴趣的:(Linux服务器配置)