内容纲要

检查 ubuntu 版本

[code]
$ cat /etc/*release
[/code]

输出

[code]
DISTRIB_ID=Ubuntu
DISTRIB_RELEASE=16.04
DISTRIB_CODENAME=xenial
DISTRIB_DESCRIPTION="Ubuntu 16.04.3 LTS"
NAME="Ubuntu"
VERSION="16.04.3 LTS (Xenial Xerus)"
ID=ubuntu
ID_LIKE=debian
PRETTY_NAME="Ubuntu 16.04.3 LTS"
VERSION_ID="16.04"
HOME_URL="http://www.ubuntu.com/"
SUPPORT_URL="http://help.ubuntu.com/"
BUG_REPORT_URL="http://bugs.launchpad.net/ubuntu/"
VERSION_CODENAME=xenial
UBUNTU_CODENAME=xenial
[/code]

安装 Redis

从源码构建最新版的 Redis

[code]
$ sudo apt-get update
$ sudo apt-get install build-essential tcl
[/code]

先卸载通过 apt-get 安装的 redis
[code]
$ sudo apt-get purge –auto-remove redis-server
[/code]

[code]
$ cd /tmp
$ curl -O http://download.redis.io/redis-stable.tar.gz
$ tar xzvf redis-stable.tar.gz
$ cd redis-stable
$ make
$ make test
$ sudo make install
[/code]

配置 Redis

[code]
$ sudo mkdir /etc/redis
$ sudo cp /tmp/redis-stable/redis.conf /etc/redis
$ sudo vi /etc/redis/redis.conf
[/code]

在文件中找到supervised指令。目前,这是设置为no。由于我们运行的是使用 systemd init 系统的操作系统,因此我们可以将其更改为systemd:

/etc/redis/redis.conf
. . .

# 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目录,我们将在稍后创建:

/etc/redis/redis.conf
. . .

# 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 单元文件

[code]
$ sudo vi /etc/systemd/system/redis.service
[/code]

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

在本[Service]节中,我们需要指定服务的行为。出于安全目的,我们不应该将服务作为root。我们应该使用一个专门的用户和组,我们会redis为了简单起见而打电话。我们将暂时创建这些。

要启动服务,我们只需要调用redis-server二进制文件,指向我们的配置。要停止它,我们可以使用 Redis shutdown命令,它可以用redis-cli二进制文件执行。另外,由于我们希望 Redis 尽可能从失败中恢复,因此我们将该Restart指令设置为 “始终”:

[code]
[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
[/code]

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

[code]
[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
[/code]

完成后保存并关闭文件。

创建 Redis 用户,组和目录

[code]
$ sudo adduser –system –group –no-create-home redis
$ sudo mkdir /var/lib/redis
$ sudo chown redis:redis /var/lib/redis
$ sudo chmod 770 /var/lib/redis
[/code]

启动测试 Redis

[code]
$ sudo ufw allow 6379
$ sudo systemctl start redis
$ sudo systemctl status redis
[/code]

测试 Redis 实例

[code]
$ redis-cli
127.0.0.1:6379> ping
[/code]

[code]
PONG
[/code]

[code]
127.0.0.1:6379> set test "It’s working!"
[/code]

[code]
OK
[/code]

[code]
127.0.0.1:6379> get test
[/code]

[code]
"It’s working!"
[/code]

退出
[code]
127.0.0.1:6379> exit
[/code]

重启 Redis

[code]
$ sudo systemctl restart redis
$ redis-cli
[/code]

[code]
127.0.0.1:6379> get test
[/code]

[code]
"It’s working!"
[/code]

返回到 shell
[code]
127.0.0.1:6379> exit
[/code]

配置 Redis 在重启时启动

[code]
$ sudo systemctl enable redis
[/code]

输出
[code]
Created symlink from /etc/systemd/system/multi-user.target.wants/redis.service to /etc/systemd/system/redis.service.
[/code]

发表评论

电子邮件地址不会被公开。 必填项已用*标注