Skip to content

Instantly share code, notes, and snippets.

@ffabreti
Forked from four43/install-redis.sh
Last active April 6, 2016 01:57
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ffabreti/0512fb5705926ae7f5ee34536a0e28db to your computer and use it in GitHub Desktop.
Save ffabreti/0512fb5705926ae7f5ee34536a0e28db to your computer and use it in GitHub Desktop.
Install Redis
#!/bin/bash
# from here: http://www.codingsteps.com/install-redis-2-6-on-amazon-ec2-linux-ami-or-centos/
# and here: https://raw.github.com/gist/257849/9f1e627e0b7dbe68882fa2b7bdb1b2b263522004/redis-server
###############################################
# To use:
# wget https://gist.githubusercontent.com/ffabreti/0512fb5705926ae7f5ee34536a0e28db/raw/install-redis.sh
# chmod 777 install-redis.sh
# ./install-redis.sh
###############################################
function die {
echo "Error: $1" 1>&2
exit 1
}
echo "*****************************************"
echo " 1. Prerequisites: Install updates, set time zones, install GCC and make"
echo "*****************************************"
sudo yum -y install gcc gcc-c++ make wget || die 'yum install'
echo "*****************************************"
echo " 2. Test if exists, Download, Untar and Make Redis 2.6"
echo "*****************************************"
if [ ! -f /usr/local/bin/redis-server ]; then
echo "redis-server executable not found! Downloading..."
cd /usr/local/src || die 'entering /usr/local/src'
sudo wget http://download.redis.io/redis-stable.tar.gz || die 'wget'
sudo tar xzf redis-stable.tar.gz || die 'untar'
sudo rm redis-stable.tar.gz -f
cd redis-stable || die 'entering redis-stable'
sudo chown -R `id -u -n` * || die 'altering redis-stable files owner'
sudo chown -R `id -u -n` . || die 'altering redis-stable dir owner'
sudo make || die 'making binaries'
echo "*****************************************"
echo " 3. Create Directories and Copy Redis Files"
echo "*****************************************"
sudo mkdir /etc/redis /var/lib/redis
sudo cp -f src/redis-server src/redis-cli /usr/local/bin || die 'copying binaries'
else
cp /etc/redis/redis.conf . || die 'copying redis.conf'
echo "*****************************************"
echo " 3. redis-server executable found on /usr/local/bin. Continuing..."
echo "*****************************************"
fi
echo "*****************************************"
echo " 4. Configure Redis.Conf"
echo "*****************************************"
echo " Editing redis.conf as follows:"
echo " 1: ... daemonize yes"
echo " 2: ... bind 127.0.0.1"
echo " 3: ... dir /var/lib/redis"
echo " 4: ... loglevel notice"
echo " 5: ... logfile /var/log/redis.log"
echo "*****************************************"
sudo sed -e "s/^daemonize no$/daemonize yes/" -e "s/^# bind 127.0.0.1$/bind 127.0.0.1/" -e "s/^dir .*/dir \/var\/lib\/redis\//" -e "s/^loglevel .*/loglevel notice/" -e "s/^logfile .*/logfile \/var\/log\/redis.log/" redis.conf > new.redis.conf || die 'altering redis.conf'
sudo cp -f new.redis.conf /etc/redis/redis.conf
echo "*****************************************"
echo " 5. Writing init Script"
echo "*****************************************"
# ---------------------------- definition of serverScript variable --------------------------------
cat > ./redis-server-init-script <<"INITSCRIPT"
#!/bin/sh
#
# redis - this script starts and stops the redis-server daemon
# Originally from - https://raw.github.com/gist/257849/9f1e627e0b7dbe68882fa2b7bdb1b2b263522004/redis-server
# From - http://www.codingsteps.com/install-redis-2-6-on-amazon-ec2-linux-ami-or-centos/
# - https://github.com/saxenap/install-redis-amazon-linux-centos/blob/master/redis-server
#
# chkconfig: 2345 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
INITSCRIPT
# ---------------------------- end of serverScript variable definition --------------------------------
echo "*****************************************"
echo " 6. Move and Configure Redis-Server"
echo "*****************************************"
sudo mv redis-server-init-script /etc/init.d/redis-server || die 'moving init-script'
sudo chmod 755 /etc/init.d/redis-server || die 'chmoding init-script'
echo "*****************************************"
echo " 7. Auto-Enable Redis-Server"
echo "*****************************************"
sudo chkconfig --add redis-server || die 'chkconfig --add'
sudo chkconfig --level 345 redis-server on || die 'chkconfig --level'
echo "*****************************************"
echo " 8. Start Redis Server"
echo "*****************************************"
sudo service redis-server start || die 'service start'
echo "*****************************************"
echo " Complete!"
echo " You can test your redis installation using the redis console:"
echo " $ src/redis-cli"
echo " redis> set foo bar"
echo " OK"
echo " redis> get foo"
echo " bar"
echo "*****************************************"
@ffabreti
Copy link
Author

ffabreti commented Apr 1, 2016

This script execs perfectly on Amazon Linux AMI ID
aws-elasticbeanstalk-amzn-2015.03.0.x86_64-ruby-hvm-201509160407 (ami-5997e03c)
Execution result: https://gist.github.com/ffabreti/60074444bee558171fc31162209d232e

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment