apt-get、yum、homebrew 这些包管理工具为我们安装软件带来许多便利,然而也有一些弊端:

  • 不同的包管理工具规则不一,比如安装路径、配置文件路径、启动方式不同;而很多情况下,开发者对包管理工具只是停留在使用的级别,并不能完全掌握各工具的规则;于是就会导致软件扩展不便
  • 另一方面,软件包版本问题:软件官方一般可以提供最新版本,包管理工具的镜像源存在一些滞后,于是有时候未必能下载到开发者所需要的版本。

于是,为了使各大规则可控,有时候需要使用手动安装的方式;为了方便 Redis 的相关扩展(如新建端口),下面采取手动安装的方式。

安装和初始配置

1
2
# 前置准备
sudo apt install gcc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
## 下载并编译
## 截止到 2018-07-13,最新版本为 v4.0.10
wget http://download.redis.io/redis-stable.tar.gz
tar xvzf redis-stable.tar.gz
cd redis-stable
make

## Redis 的核心文件就这两个
cp src/redis-server /usr/local/bin/
cp src/redis-cli /usr/local/bin/

## 此目录用于保存 6379 端口相关的各类文件
mkdir -p /usr/local/etc/redis/6379

## 端口6379的配置文件
cp redis.conf /usr/local/etc/redis/6379/redis.conf

## =================================== ##
## 编辑配置文件
vi /usr/local/etc/redis/6379/redis.conf

#### 修改以下字段 ####
port => 6379
daemonize => yes
pidfile => /usr/local/etc/redis/6379/redis.pid
logfile => /usr/local/etc/redis/6379/redis.log
dir => /usr/local/etc/redis/6379
# 必要时调整 protected-mode
protected-mode => no

#### 考虑安全性,添加以下记录 ####
rename-command FLUSHALL ""
rename-command FLUSHDB ""
rename-command CONFIG ""
rename-command EVAL ""
## =================================== ##

启动脚本

1
vi ./redis_6379
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#!/bin/sh
### BEGIN INIT INFO
# Provides: redis_6379
# Required-Start: $syslog $remote_fs
# Required-Stop: $syslog $remote_fs
# Should-Start: $local_fs
# Should-Stop: $local_fs
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: redis_6379 - Persistent key-value db
# Description: redis_6379 - Persistent key-value db
### END INIT INFO

REDISPORT=6379
EXEC=/usr/local/bin/redis-server
CLIEXEC=/usr/local/bin/redis-cli

PIDFILE="/usr/local/etc/redis/${REDISPORT}/redis.pid"
CONF="/usr/local/etc/redis/${REDISPORT}/redis.conf"

case "$1" in
start)
if [ -f "${PIDFILE}" ]
then
echo "$PIDFILE exists, process is already running or crashed"
else
echo "Starting Redis server..."
$EXEC $CONF
fi
;;
stop)
if [ ! -f "${PIDFILE}" ]
then
echo "$PIDFILE does not exist, process is not running"
else
PID=$(cat "${PIDFILE}")
echo "Stopping ..."
$CLIEXEC -p $REDISPORT shutdown
rm -f "${PIDFILE}"
echo "Redis stopped"
fi
;;
status)
PID=$(cat $PIDFILE)

if ! kill -0 ${PID} > /dev/null 2>&1; then
echo 'Redis is not running'
else
echo "Redis is running ($PID)"
fi
;;
restart)
$0 stop
$0 start
;;
*)
echo "Please use start, stop, restart or status as first argument"
;;
esac
1
2
3
4
5
6
7
chmod +x ./redis_6379

## For Linux
sudo cp ./redis_6379 /etc/init.d/

## For OS X,因为没有 init.d 目录,故放在 /etc/redis 下
cp ./redis_6379 /usr/local/etc/redis/redis_6379

Linux

开机启动

1
2
3
4
5
## For CentOS
echo "/etc/init.d/redis_6379 start" | sudo tee --append /etc/rc.d/rc.local

## For Ubuntu
sudo update-rc.d redis_6379 defaults

运行和停止

1
2
3
4
5
## 启动 redis_6379
sudo /etc/init.d/redis_6379 start

## 停止 redis_6379
sudo /etc/init.d/redis_6379 stop

OS X

开机启动

1
vi ~/Library/LaunchAgents/io.redis.6379.plist
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Label</key>
<string>io.redis.6379</string>
<key>ProgramArguments</key>
<array>
<string>/usr/local/etc/redis/redis_6379</string>
<string>start</string>
</array>
<key>RunAtLoad</key>
<true/>
</dict>
</plist>

启动项

1
2
3
4
5
## 添加启动项
launchctl load -w ~/Library/LaunchAgents/io.redis.6379.plist

## 删除启动项
launchctl unload -w ~/Library/LaunchAgents/io.redis.6379.plist

运行和停止

1
2
3
4
5
## 启动 redis_6379
/usr/local/etc/redis/redis_6379 start

## 停止 redis_6379
/usr/local/etc/redis/redis_6379 stop

其他使用

参考链接