源码编译 Redis 环境

Summary: Author: 张亚飞 | 阅读时间: 3 minute read | Published: 2015-08-08
Filed under Categories: DevOpsTags: Linux, Server, Software, DevOps,

Redis 安装

安装Redis服务器,会自动地一起安装Redis命令行客户端程序.


源码编译安装 redis-server:

wget http://download.redis.io/redis-stable.tar.gz
wget http://download.redis.io/releases/redis-4.0.8.tar.gz
tar -zxvf redis-4.0.8.tar.gz
cd redis-4.0.8
sudo make
sudo make install

后台启动

  • 如果需要后台启动,则可以使用 redis 提供的 install_server.sh 脚本:
cd utils
sudo ./install_server.sh

下面是运行过程,全程使用默认配置:

$ sudo ./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 [/etc/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    : /etc/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

Is this ok? Then press ENTER to go on or Ctrl-C to abort.
Copied /tmp/6379.conf => /etc/init.d/redis_6379
Installing service...
 Adding system startup for /etc/init.d/redis_6379 ...
   /etc/rc0.d/K20redis_6379 -> ../init.d/redis_6379
   /etc/rc1.d/K20redis_6379 -> ../init.d/redis_6379
   /etc/rc6.d/K20redis_6379 -> ../init.d/redis_6379
   /etc/rc2.d/S20redis_6379 -> ../init.d/redis_6379
   /etc/rc3.d/S20redis_6379 -> ../init.d/redis_6379
   /etc/rc4.d/S20redis_6379 -> ../init.d/redis_6379
   /etc/rc5.d/S20redis_6379 -> ../init.d/redis_6379
Success!
Starting Redis server...
Installation successful!

可以看到名为 redis_6379service 已经安装并启动,之后可以通过以下方式操作:

sudo service redis_6379 start
sudo service redis_6379 stop

php redis 扩展安装

// TODO…

  • 测试代码 php 为例
<?php
connect('127.0.0.1',6379);  
$redis->set('name','给我一支烟!');  
echo $redis->get('name');  
?>

到此安装全部完成 当然一些 redis 的配置信息 还需要了解

  • redis 配置文件
/etc/redis/reids.conf

使用 iptables 配置

  • 注释掉 /etc/redis/6379.conf 中的 bind 127.0.0.1 后允许所有外网访问,于是只能通过 iptables 来过滤访问请求来源 IP
ALLOW_IP_ADDRESS=1.2.3.4
REDIS_PORT=6379
# create a new chain
iptables -N redis-protection
# allow your IP
iptables -A redis-protection --src $ALLOW_IP_ADDRESS -j ACCEPT
# allow Redsmin IP if you want to connect from Redsmin
iptables -A redis-protection --src 62.210.240.77 -j ACCEPT
# drop everyone else
iptables -A redis-protection -j DROP
# use chain xxx for packets coming to TCP port $REDIS_PORT
iptables -I INPUT -m tcp -p tcp --dport $REDIS_PORT -j redis-protection

问题分析

coam@coamn:~/RunProject/redis-3.2.0/utils$ redis-cli -h 43.241.222.110
43.241.222.110:6379> keys *
(error) DENIED Redis is running in protected mode because protected mode is enabled, no bind address was specified, no authentication password is requested to clients. In this mode connections are only accepted from the loopback interface.
If you want to connect from external computers to Redis you may adopt one of the following solutions: 1) Just disable protected mode sending the command 'CONFIG SET protected-mode no' from the loopback interface by connecting to Redis from 
the same host the server is running, however MAKE SURE Redis is not publicly accessible from internet if you do so. Use CONFIG REWRITE to make this change permanent. 2) Alternatively you can just disable the protected mode by editing the Redis 
configuration file, and setting the protected mode option to 'no', and then restarting the server. 3) If you started the server manually just for testing, restart it with the '--protected-mode no' option. 4) Setup a bind address or an authentication 
password. NOTE: You only need to do one of the above things in order for the server to start accepting connections from the outside.

Redis 主要修改部分 /etc/redis/6379.conf

# Examples:
# 注释掉此项允许远程访问
# bind 192.168.1.100 10.0.0.1
# bind 127.0.0.1

#  protected = yes
protected = no

Redis 主要修改如下部分:

# Examples:
# 注释掉此项允许远程访问
# bind 192.168.1.100 10.0.0.1
# bind 127.0.0.1

#  protected = yes
protected = no

# slaveof <masterip> <masterport>
slaveof 43.241.222.110 6379

查看安装运行版本号

redis-server -v

配置文件

/etc/redis/redis.conf

重启 Redis 服务器.

sudo /etc/init.d/redis-server restart

hiredisredis 的一个调用接口,它提供了一些简单的redis的操作,它 的安装和下载过程如下:

sudo git clone https://github.com/redis/hiredis.git
cd hiredis
make
sudo make install

Comments

Cor-Ethan, the beverage → www.iirii.com