Compile latest redis on Amazon Linux 2023 and configure with systemd
因為AL2023 默認repo內只有redis6 沒有redis7,所以很多人都希望可以自行安裝redis7,但編譯安裝後主要的問題是無法自啟動,本文主要解決自啟動的問題。
獲取並安裝必要的組件:
dnf install openssl-devel gcc tcl systemd-devel -y
wget http://download.redis.io/redis-stable.tar.gz
tar zxf redis-stable.tar.gz
cd redis-stable
compile 時需要加入systemd的支持:
make BUILD_TLS=yes USE_SYSTEMD=yes
make test
make install
編輯一下默認的配置文件:
vi redis.conf
pidfile /var/run/redis/redis_6379.pid
dir /var/db/redis
cp redis.conf /etc/
添加必要的OS level user
groupadd redis
useradd -g redis redis
edit the amazon linux version redis6 startup service file and copy to /etc/systemd/system/redis.service:
—我是分割線—
[Unit]
Description=Redis persistent key-value database
After=network.target
After=network-online.target
Wants=network-online.target
[Service]
ExecStart=/usr/local/bin/redis-server /etc/redis.conf --daemonize no --supervised systemd
ExecStop=/usr/libexec/redis-shutdown
Type=notify
User=redis
Group=redis
RuntimeDirectory=redis
RuntimeDirectoryMode=0755
[Install]
WantedBy=multi-user.target
—我是分割線—
edit the amazon linux version redis6 shutdown script and copy to /usr/libexec/redis-shutdown:
—我是分割線—
#!/usr/bin/bash
#
# Wrapper to close properly redis and sentinel
test x"$REDIS_DEBUG" != x && set -x
REDIS_CLI=/usr/local/bin/redis-cli
# Retrieve service name
SERVICE_NAME="$1"
if [ -z "$SERVICE_NAME" ]; then
SERVICE_NAME=redis
fi
# Get the proper config file based on service name
CONFIG_FILE="/etc/$SERVICE_NAME.conf"
# Use awk to retrieve host, port from config file
HOST=`awk '/^[[:blank:]]*bind/ { print $2 }' $CONFIG_FILE | tail -n1`
PORT=`awk '/^[[:blank:]]*port/ { print $2 }' $CONFIG_FILE | tail -n1`
PASS=`awk '/^[[:blank:]]*requirepass/ { print $2 }' $CONFIG_FILE | tail -n1`
SOCK=`awk '/^[[:blank:]]*unixsocket\s/ { print $2 }' $CONFIG_FILE | tail -n1`
# Just in case, use default host, port
HOST=${HOST:-127.0.0.1}
if [ "$SERVICE_NAME" = redis ]; then
PORT=${PORT:-6379}
else
PORT=${PORT:-26739}
fi
# Setup additional parameters
# e.g password-protected redis instances
[ -z "$PASS" ] || ADDITIONAL_PARAMS="-a $PASS"
# shutdown the service properly
if [ -e "$SOCK" ] ; then
$REDIS_CLI -s $SOCK $ADDITIONAL_PARAMS shutdown
else
$REDIS_CLI -h $HOST -p $PORT $ADDITIONAL_PARAMS shutdown
fi
—我是分割線—
chmod 755 /usr/libexec/redis-shutdown
reboot instance to test and enjoy!
![](https://bbken.org/wp-content/uploads/2024/07/img_5315-1-744x1024.png)