Ubuntu 16.04 安装和配置 Redis

因为发现之前手动安装的 redis 与现有的教程不一样,所以总结统一一下安装的标准步骤。

安装依赖项

为了获取最新版本的 Redis,我们将从源代码进行编译和安装。下载源代码之前,需要先安装一些编译所需要的软件。我们安装 build-essential 包用于编译功能,安装 tcl 包用于测试编译后的二进制文件。执行下面的命令来安装依赖:

sudo apt-get update
sudo apt-get install build-essential tcl

下载、编译和安装 Redis

下载并解压源代码

现在,我们下载最新的稳定版 Redis:

wget http://download.redis.io/redis-stable.tar.gz

解压 tar.gz 文件:

tar -zxvf redis-stable.tar.gz

进入刚刚解压的 Redis 源代码根目录

cd redis-stable

编译并安装 Redis

现在,我们编译 Redis:

make

编译完成后,运行测试来确保编译成功完成:

make test

这将运行一段时间,测试完成后,就可以安装 Redis 到系统了:

sudo make install

配置 Redis

Redis 安装好之后,就可以对它进行配置。

首先,我们需要创建一个配置目录,通常来说这个目录是 /etc/redis 目录,输入下面的命令来创建:

sudo mkdir /etc/redis

然后,复制之前解压的源代码中的配置文件到配置目录中:

sudo cp redis-stable/redis.conf /etc/redis

下一步,打开这个文件,并做一些修改:

sudo vim /etc/redis/redis.conf

在这个文件中,找到 supervised 指令。当前,它的值是 no。由于我们的操作系统使用的是 systemd 初始化系统,所以我们把这个值修改为 systemd

# If you run Redis from upstart or systemd, Redis can interact with your
# supervision tree. Options:
#   supervised no      - no supervision interaction
#   supervised upstart - signal upstart by putting Redis into SIGSTOP mode
#   supervised systemd - signal systemd by writing READY=1 to $NOTIFY_SOCKET
#   supervised auto    - detect upstart or systemd method based on
#                        UPSTART_JOB or NOTIFY_SOCKET environment variables
# Note: these supervision methods only signal "process is ready."
#       They do not enable continuous liveness pings back to your supervisor.
supervised systemd

然后,找到 dir 指令。这个设置指定了 redis 用来存储持久化数据的目录。我们需要选择一个能够让 Redis 写入权限并且一般用户没有查看权限的目录。这里,我们使用 /var/lib/redis 目录,稍后我们将创建该目录:

# The working directory.
#
# The DB will be written inside this directory, with the filename specified
# above using the 'dbfilename' configuration directive.
#
# The Append Only File will also be created inside this directory.
#
# Note that you must specify a directory here, not a file name.
dir /var/lib/redis

最后保存并关闭该文件。

创建一个 Redis systemd unit 文件

下一步,我们创建一个 systemd unit 文件这样初始化系统便能管理 Redis 进程:

创建 /etc/systemd/system/redis.service 文件:

sudo vim /etc/systemd/system/redis.service

打开这个文件,以 [Unit] 为开头,添加一些描述和定义一个需求(启动此服务之前网络必须可用):

[Unit]
Description=Redis In-Memory Data Store
After=network.target

然后在 [Service] 部分,指定服务的行为。为了安全考虑,不应该以 root 权限运行该服务。我们应该使用一个特定的用户和用户组,比如,这里使用 redis 用户和用户组,稍后我们将创建他们。

为了启动服务,需要在配置中指定要调用的 redis-server 程序,为了停止它,我们可以使用 Redis shutdown 命令,它将由 redis-cli 程序执行。有时,我们需要让 Redis 尽可能从失败中恢复,所以设置 Restart 值为 "always":

[Unit]
Description=Redis In-Memory Data Store
After=network.target

[Service]
User=redis
Group=redis
ExecStart=/usr/local/bin/redis-server /etc/redis/redis.conf
ExecStop=/usr/local/bin/redis-cli shutdown
Restart=always

最后,在 [Install] 部分,我们可以定义服务应该附加到的systemd目标(如果已启用)(配置为在启动时启动):

[Unit]
Description=Redis In-Memory Data Store
After=network.target

[Service]
User=redis
Group=redis
ExecStart=/usr/local/bin/redis-server /etc/redis/redis.conf
ExecStop=/usr/local/bin/redis-cli shutdown
Restart=always

[Install]
WantedBy=multi-user.target

保存并关闭该文件。

创建 Redis 用户,用户组和目录

现在,我们创建在上一节中提到的目录、 redis 用户和用户组:

sudo adduser --system --group --no-create-home redis

创建 /var/lib/redis 目录:

sudo mkdir /var/lib/redis

赋予 redis 用户和组该目录的权限:

sudo chown redis:redis /var/lib/redis

调整权限,让一般用户无法访问该目录:

sudo chmod 770 /var/lib/redis

启动和测试 Redis

启动 Redis 服务

sudo systemctl start redis

检查服务是否有错误:

sudo systemctl status redis

然后,你将会看到这些输出:

● redis.service - Redis In-Memory Data Store
   Loaded: loaded (/etc/systemd/system/redis.service; disabled; vendor preset: enabled)
   Active: active (running) since 二 2018-07-17 21:20:10 CST; 10s ago
  Process: 23191 ExecStop=/usr/local/bin/redis-cli shutdown (code=exited, status=1/FAILURE)
 Main PID: 23195 (redis-server)
   CGroup: /system.slice/redis.service
           └─23195 /usr/local/bin/redis-server 127.0.0.1:6379

测试 Redis 实例的功能

为测试服务功能是否正确可用,使用命令行客户端链接 Redis 服务器:

redis-cli

输入命令,检查连通性:

127.0.0.1:6379> ping
PONG

输出 PONG 则表示连通,正常。

检查是否可以设置一个键:

127.0.0.1:6379> set test "It's working!"
OK

然后,获取该键的值:

127.0.0.1:6379> get test
"It's working!"

退出 Redis 命令行:

$ redis-cli
127.0.0.1:6379> exit

然后再连接一次,获取 test 键的值,再退出:

127.0.0.1:6379> get test
"It's working!"
127.0.0.1:6379> exit

设置 Redis 开机启动

$ sudo systemctl enable redis
Created symlink from /etc/systemd/system/multi-user.target.wants/redis.service to /etc/systemd/system/redis.service.

参考:How To Install and Configure Redis on Ubuntu 16.04 | DigitalOcean

PS - 个人博客原文:Ubuntu 16.04 安装 Redis

posted @ 2018-07-18 09:54  feiffy  阅读(933)  评论(0编辑  收藏  举报