Debian Linux下Redis安装教程

Linux大全评论1.3K views阅读模式

在Debian Linux下其实可以用apt-get一键安装Redis,但是安装的版本不是最新的。为了安装最新的Redis版本,需要从官方下载源码编译安装,过程也比较简单。

目前Redis最新的stable版本是4.0.6,源码下载地址是:http://download.redis.io/releases/redis-4.0.6.tar.gz

首先把源码下载回来,在Linux终端输入下面的命令:

wget http://download.redis.io/releases/redis-4.0.6.tar.gz

下载完后解压:

tar -zxvf redis-4.0.6.tar.gz

解压好后进入redis-4.0.6目录进行编译安装:

cd redis-4.0.6
make
sudo make install

编译安装结束对Redis实例进行设置:

cd utils
sudo ./install_server.sh

运行install_server.sh后会提示你配置Redis实例的一些参数

端口:默认为6379,可以更改为别的端口,能增加安全性

Please select the redis port for this instance: [6379]

 

配置文件:/etc/redis/端口号.conf
Please select the redis config file name [/etc/redis/6379.conf]

 

日志文件:默认为/var/log/redis_端口号.log
Please select the redis log file name [/var/log/redis_6379.log] /var/log/redis/redis.log

 

数据目录:默认为/var/lib/redis/端口号
Please select the data directory for this instance [/var/lib/redis/6379] /data/redis

 

最后是redis执行目录:默认为/usr/local/bin/redis-server
Please select the redis executable path [/usr/local/bin/redis-server]

参数输入完成后或让你确认设置是否正确,如果没问题按enter键进行确认,然后redis会安装服务并启动服务。

 

注册为系统服务

在目录 /etc/systemd/system 下新建文件:redis.service

输入以下内容:

[Unit]
Description=Redis
After=syslog.target network.target remote-fs.target nss-lookup.target

[Service]
Type=forking
PIDFile=/usr/local/redis/redis.pid
ExecStart=/usr/local/redis/src/redis-server /usr/local/redis/redis.conf
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true

[Install]
WantedBy=multi-user.target

然后重新加载systemctl服务:

sudo systemctl daemon-reload

 

就可以用下面的命令启动Redis服务了

sudo systemctl start redis

 

 

下面对Redis进行一些其他的配置

Redis默认的系统服务为 /etc/init.d/redis_6379,6379是端口号,是前面配置的端口

为了好记,把它更名为/etc/inti.d/reids-server

sudo mv /etc/init.d/redis_6379 /etc/init.d/redis-server

 然后重启服务:

sudo service redis-server restart

 

设置Redis可被远程访问
默认情况下,为了安全Redis服务器不允许远程访问,只允许本机访问。

如果需要远程访问的功能,需要进行设置。

用vi打开Redis服务器的配置文件redis.conf

sudo vi /etc/redis/redis.conf

 

找到 bind 127.0.0.1 配置项,在前面加上#,注释掉bind配置

#bind 127.0.0.1

保存文件后,重启Redis服务。

sudo service redis-server restart

 

这样就可以远程访问Redis服务了。

当然这样设置并不安全,如果是生成环境,最好设置为服务器IP

 

设置访问密码
默认情况下,访问Redis服务器是不需要密码的

如果开启了远程访问,这样是极不安全滴

为了安全需要设置Redis的访问密码

假如设置访问密码为 R7i8V51hK4。

用vi打开Redis服务器的配置文件redis.conf

sudo vi /etc/redis/redis.conf

 

找到 # requirepass foobared配置项,把前面的#去掉,修改foobared为想要设置的密码R7i8V51hK4

requirepass R7i8V51hK4

 

 保存文件后,重启Redis服务。

sudo service redis-server restart

这样访问就需要密码了

登录测试一下,如果是本地测试的话,输入以下命令:

redis-cli -h 127.0.0.1 -p 6379 -a R7i8V51hK4

 

-h 指定主机,-p指定端口,-a 指定密码

回车,人品好的话,屏幕上应该会显示:

127.0.0.1:6379>

表示成功连接上Redis服务了

来ping-pong一下,输入ping

ping

 

回车会返回 PONG 表示Redis服务运行正常啦

到此Redis就安装成功了。

下面关于Redis的文章您也可能喜欢,不妨参考下:

Ubuntu 14.04下Redis安装及简单测试 http://www.linuxidc.com/Linux/2014-05/101544.htm

Redis主从复制基本配置 http://www.linuxidc.com/Linux/2015-03/115610.htm

Redis集群搭建与简单使用  http://www.linuxidc.com/Linux/2017-03/142210.htm

CentOS 7下Redis的安装与配置 http://www.linuxidc.com/Linux/2017-02/140363.htm

Ubuntu 14.04安装Redis与简单配置 http://www.linuxidc.com/Linux/2017-01/139075.htm

Ubuntu 16.04环境中安装PHP7.0 Redis扩展 http://www.linuxidc.com/Linux/2016-09/135631.htm

Redis 单机&集群离线安装部署 http://www.linuxidc.com/Linux/2017-03/141403.htm

CentOS 7.0 安装Redis 3.2.1详细过程和使用常见问题 http://www.linuxidc.com/Linux/2016-09/135071.htm

Ubuntu 16.04环境中安装PHP7.0 Redis扩展 http://www.linuxidc.com/Linux/2016-09/135631.htm

Ubuntu 15.10下Redis集群部署文档 http://www.linuxidc.com/Linux/2016-06/132340.htm

Redis实战 中文PDF http://www.linuxidc.com/Linux/2016-04/129932.htm

Redis热迁移实战总结  http://www.linuxidc.com/Linux/2017-02/141083.htm

企鹅博客
  • 本文由 发表于 2019年9月6日 18:52:58
  • 转载请务必保留本文链接:https://www.qieseo.com/194695.html

发表评论