CentOS 7.6にRedis 6.2.6を導入する際、ソースコードからのビルド(wgetでの取得およびmake)が完了した後のステップとして、システムサービスへの登録方法には大きく分けて2つのアプローチがあります。本記事では、付属のユーティリティスクリプトを利用する方法と、推奨されるsystemdユニットファイルを手動で作成する方法について解説します。
1. 付属スクリプト「install_server.sh」を利用したサービス登録
Redisのソースディレクトリ内にある utils/install_server.sh を使用すると、対話形式でポート番号やパスを設定し、サービスとして登録できます。ただし、CentOS 7以降のsystemd環境では、デフォルトでこのスクリプトの実行が制限されています。
スクリプトの修正
systemd環境での実行を許可するため、utils/install_server.sh 内のチェック処理をコメントアウトします。
# systemdの検出による強制終了を無効化
#_pid_1_exe="$(readlink -f /proc/1/exe)"
#if [ "${_pid_1_exe##*/}" = systemd ]
#then
# echo "This systems seems to use systemd."
# echo "Please take a look at the provided example service unit files in this directory, and adapt and install them. Sorry!"
# exit 1
#fi
スクリプトの実行と設定例
修正後、スクリプトを実行して各パラメータを指定します。以下は実行時の入出力例です(パスやポートは環境に合わせて変更してください)。
[root@server utils]# ./install_server.sh
Welcome to the redis service installer
Please select the redis port for this instance: [6379] 10010
Please select the redis config file name [/etc/redis/10010.conf] /opt/redis-6.2.6/redis.conf
Please select the redis log file name [/var/log/redis_10010.log]
Please select the data directory for this instance [/var/lib/redis/10010] /opt/redis-6.2.6/data
Please select the redis executable path [] /usr/local/bin/redis-server
Selected config:
Port : 10010
Config file : /opt/redis-6.2.6/redis.conf
Log file : /var/log/redis_10010.log
Data dir : /opt/redis-6.2.6/data
Executable : /usr/local/bin/redis-server
Cli Executable : /usr/local/bin/redis-cli
Is this ok? Then press ENTER to go on or Ctrl-C to abort.
Installing service...
Successfully added to chkconfig!
Successfully added to runlevels 345!
Starting Redis server...
Installation successful!
この方法では、SysV initスクリプトとして登録されますが、systemd経由でも systemctl status redis_10010 のように管理可能です。
2. systemdによるネイティブなサービス管理(推奨)
現代的なLinux環境では、SysV initよりもsystemdユニットファイルを使用する方法が推奨されます。Redis 6.2.6には systemd-redis_server.service というテンプレートが含まれています。
redis.confの設定変更
systemdで適切に管理するために、設定ファイル(redis.conf)で以下のパラメータを調整します。これにより、Redisプロセスとsystemdの間で適切な通知が行われるようになります。
# redis.conf
# systemdを使用する場合、daemonizeは 'no' でも構いません
daemonize no
# systemdとの連携を有効化
supervised systemd
systemdユニットファイルの作成
/etc/systemd/system/redis.service を作成または編集し、実行バイナリと設定ファイルのパスを正しく指定します。
[Unit]
Description=Redis Data Store Server
After=network.target
[Service]
# インストールしたパスに合わせて変更
ExecStart=/usr/local/bin/redis-server /opt/redis-6.2.6/redis.conf
LimitNOFILE=65535
Type=notify
User=root
Group=root
TimeoutStartSec=infinity
TimeoutStopSec=infinity
[Install]
WantedBy=multi-user.target
サービスの起動とログの確認
設定完了後、ユニットファイルを再読み込みしてサービスを起動します。
# 設定の反映
systemctl daemon-reload
# サービスの有効化と起動
systemctl enable redis
systemctl start redis
# 状態確認
systemctl status redis
正常に起動すると、ログファイル(設定した /var/log/redis_10010.log 等)に「Ready to accept connections」と出力され、redis-cli を用いて接続テストを行う準備が整います。