Skip to content

Instantly share code, notes, and snippets.

@emmanuelnk
Last active March 27, 2023 09:26
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save emmanuelnk/d698e8acc7058af8a3cbe460fd32987a to your computer and use it in GitHub Desktop.
Save emmanuelnk/d698e8acc7058af8a3cbe460fd32987a to your computer and use it in GitHub Desktop.
Install Redis Server on Amazon Linux AMI EC2
#!/bin/sh
# From - http://www.codingsteps.com/install-redis-2-6-on-amazon-ec2-linux-ami-or-centos/
#
# redis - this script starts and stops the redis-server daemon
#
# chkconfig: - 85 15
# description: Redis is a persistent key-value database
# processname: redis-server
# config: /etc/redis/redis.conf
# config: /etc/sysconfig/redis
# pidfile: /var/run/redis.pid
# Source function library.
. /etc/rc.d/init.d/functions
# Source networking configuration.
. /etc/sysconfig/network
# Check that networking is up.
[ "$NETWORKING" = "no" ] && exit 0
redis="/usr/local/bin/redis-server"
prog=$(basename $redis)
REDIS_CONF_FILE="/etc/redis/redis.conf"
[ -f /etc/sysconfig/redis ] && . /etc/sysconfig/redis
lockfile=/var/lock/subsys/redis
start() {
[ -x $redis ] || exit 5
[ -f $REDIS_CONF_FILE ] || exit 6
echo -n $"Starting $prog: "
daemon $redis $REDIS_CONF_FILE
retval=$?
echo
[ $retval -eq 0 ] && touch $lockfile
return $retval
}
stop() {
echo -n $"Stopping $prog: "
killproc $prog -QUIT
retval=$?
echo
[ $retval -eq 0 ] && rm -f $lockfile
return $retval
}
restart() {
stop
start
}
reload() {
echo -n $"Reloading $prog: "
killproc $redis -HUP
RETVAL=$?
echo
}
force_reload() {
restart
}
rh_status() {
status $prog
}
rh_status_q() {
rh_status >/dev/null 2>&1
}
case "$1" in
start)
rh_status_q && exit 0
$1
;;
stop)
rh_status_q || exit 0
$1
;;
restart|configtest)
$1
;;
reload)
rh_status_q || exit 7
$1
;;
force-reload)
force_reload
;;
status)
rh_status
;;
condrestart|try-restart)
rh_status_q || exit 0
;;
*)
echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload}"
exit 2
esac
sudo yum -y update
sudo ln -sf /usr/share/zoneinfo/UTC /etc/localtime
sudo yum -y install gcc make tcl
wget http://download.redis.io/releases/redis-5.0.10.tar.gz -O /tmp/redis-5.0.10.tar.gz
cd /tmp && sudo tar xzf redis-5.0.10.tar.gz && cd redis-5.0.10 && sudo make
# sudo make test
# Create Directories and Copy Redis Files
sudo mkdir -p /etc/redis && sudo mkdir -p /var/lib/redis
sudo cp src/redis-server src/redis-cli /usr/local/bin/
sudo cp redis.conf /etc/redis/
# Configure redis.conf
# replace: daemonize no --> daemonize yes
sudo sed -i '/\#/! s/daemonize no/daemonize yes/' /etc/redis/redis.conf
# replace: dir ./ --> dir /var/lib/redis
sudo sed -i '/\#/! s/dir .\//dir \/var\/lib\/redis/' /etc/redis/redis.conf
# CAUTION: If you do the following ensure the instance is not easily accessible on public internet
# replace: protected-mode yes --> protected-mode no
sudo sed -i 's/protected-mode yes/protected-mode no/' /etc/redis/redis.conf
# replace: bind 127.0.0.1 --> # bind 127.0.0.1. THIS WILL EXPOSE REDIS TO ALL NETWORK INTERFACES
sudo sed -i '/\#/! s/bind 127.0.0.1/bind 0.0.0.0/' /etc/redis/redis.conf
# if redis exceeds the memory of the instance it will eject old keys by default. Do we want this?
# sudo sed -i '/\#/! s/maxmemory <bytes>/maxmemory 800mb/' /etc/redis/redis.conf
# sudo sed -i 's/\# maxmemory-policy noeviction/maxmemory-policy volatile-lru/' /etc/redis/redis.conf
# Download init Script
wget https://gist.githubusercontent.com/emmanuelnk/d698e8acc7058af8a3cbe460fd32987a/raw/init_script?o=k -O /tmp/redis-server
# Move and Configure Redis-Server
# Note: The redis-server to be moved below is the one downloaded above.
sudo mv /tmp/redis-server /etc/init.d
sudo chmod 755 /etc/init.d/redis-server
# Auto-Enable Redis-Server
sudo chkconfig --add redis-server
sudo chkconfig --level 345 redis-server on
# Start Redis Server
sudo service redis-server start
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment