takafumi blog

日々の勉強メモ

【Redis】 CentOS7にRedisの最新版をmakeインストール

環境  CentOS 7.0 Redis 3.0.6$nbsp;

yumのバージョンを確認してみる

とりあえずyumを確認。

$ sudo yum list redis
sudo yum list redis
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
 * base: ftp.iij.ad.jp
 * epel: ftp.riken.jp
 * extras: ftp.iij.ad.jp
 * updates: ftp.iij.ad.jp
Available Packages
redis.x86_64                        2.8.19-2.el7                         epel

epelリポジトリからインストールできる模様。
ただバージョンが2系。

せっかくなので、3系をインストールしてみる。


最新版をmakeインストール

$ # ソースを取得
$ sudo wget http://download.redis.io/releases/redis-3.0.6.tar.gz

$ # 展開
$ sudo tar zxvf redis-3.0.6.tar.gz
$ cd redis-3.0.6

$ # インストール
$ sudo make
$ sudo make install

これでインストール完了。
インストールを確認。

$ . ~/.bash_profile
$ which redis-cli
/usr/local/bin/redis-cli

OK。 ただ設定ファイルredis.confが無い。
yumだと勝手に作られるが、tarボールだと手動で作る必要がある。

$ sudo mkdir /etc/redis/
$ sudo cp redis.conf /etc/redis/

設定ファイルを編集する。
とりあえず、デーモンモードに変更と、ログを出力しておく。

$ diff -u redis.conf.ORI redis.conf
--- redis.conf.ORI      2016-01-10 17:12:20.898328665 +0900
+++ redis.conf  2016-01-10 17:15:13.952769838 +0900
@@ -39,7 +39,7 @@

 # By default Redis does not run as a daemon. Use 'yes' if you need it.
 # Note that Redis will write a pid file in /var/run/redis.pid when daemonized.
-daemonize no
+daemonize yes

 # When running daemonized, Redis writes a pid file in /var/run/redis.pid by
 # default. You can specify a custom pid file location here.
@@ -105,7 +105,7 @@
 # Specify the log file name. Also the empty string can be used to force
 # Redis to log on the standard output. Note that if you use standard
 # output for logging but daemonize, logs will be sent to /dev/null
-logfile ""
+logfile "/var/log/redis.log"

 # To enable logging to the system logger, just set 'syslog-enabled' to yes,
 # and optionally update the other syslog parameters to suit your needs.

サービスを起動

$ sudo /usr/local/bin/redis-server /etc/redis/redis.conf

接続を確認する。

$ redis-cli ping
PONG

接続OK。

毎度、サービス起動するときにコマンドを叩かなくても済むよう、.serviceファイルを作る。

$ vi /etc/systemd/system/redis.service
[Unit]
Description=Load redis daemon.

[Service]
ExecStart=/usr/local/bin/redis-server /etc/redis/redis.conf
RemainAfterExit=yes

[Install]
WantedBy=multi-user.target

ちなみにファイルの書き方は適当。

Linux - systemdの*.serviceファイルの書き方 - Qiita

こちらを参考にさせてもらいました。
本当はredisユーザーとか作って、User,Groupも指定するべきかも。

サービスの起動と、自動起動を設定。

$ # 起動
$ sudo systemctl start redis.service

$ # 自動起動設定
$ sudo systemctl enable redis
sudo systemctl status redis.service
● redis.service - Load redis daemon.
   Loaded: loaded (/etc/systemd/system/redis.service; enabled; vendor preset: disabled)
   Active: active (exited) since Sun 2016-01-10 17:38:28 JST; 4min 14s ago
 Main PID: 7473 (code=exited, status=0/SUCCESS)
   CGroup: /system.slice/redis.service
           └─7474 /usr/local/bin/redis-server *:6379

Jan 10 17:38:28 localhost.localdomain systemd[1]: Started Load redis daemon..
Jan 10 17:38:28 localhost.localdomain systemd[1]: Starting Load redis daemon....

$ # 接続確認
$ redis-cli ping
PONG

とりあえず動いている。