Skip to content

Instantly share code, notes, and snippets.

@krabello
Created February 14, 2018 16:49
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save krabello/4d49670b042ad26dabe8b2e05493d269 to your computer and use it in GitHub Desktop.
Configure Redis on Ubuntu
#!/bin/bash
###############################################################################
######################### Install and Configure Redis #########################
###############################################################################
###############################################################################
################### Install the Build and Test Dependencies ###################
###############################################################################
# In order to get the latest version of Redis, we will be compiling and
# installing the software from source. Before we download the code, we need to
# satisfy the build dependencies so that we can compile the software.
sudo apt-get update -y
sudo apt-get install build-essential tcl -y
###############################################################################
#################### Download, Compile, and Install Redis #####################
###############################################################################
# Download and Extract the Source Code
cd /tmp
# Download the latest stable version of Redis
curl -O http://download.redis.io/redis-stable.tar.gz
# Unpack the tarball
tar xzvf redis-stable.tar.gz
# Move into the Redis source directory structure
cd redis-stable
###############################################################################
########################### Build and Install Redis ###########################
###############################################################################
# Compile the Redis binaries
make
# install the binaries onto the system
sudo make install
###############################################################################
############################## Configure Redis ################################
###############################################################################
# create a configuration directory
sudo mkdir /etc/redis
# copy over the sample Redis configuration file included in the Redis source
sudo cp /tmp/redis-stable/redis.conf /etc/redis
# change the supervised directive to 'supervised systemd'
sed -i -e 's/supervised no/supervised systemd/g' /etc/redis/redis.conf
# change the dir directive to 'dir /var/lib/redis'
sed -i -e 's/dir .\//dir \/var\/lib\/redis/g' /etc/redis/redis.conf
###############################################################################
###################### Create a Redis systemd Unit File #######################
###############################################################################
# create a systemd unit file so that the init system can manage the
# Redis process
echo "
[Unit]
Description=Redis In-Memory Data Store
After=network.target
[Service]
User=redis
Group=redis
ExecStart=/usr/local/bin/redis-server /etc/redis/redis.conf
ExecStop=/usr/local/bin/redis-cli shutdown
Restart=always
[Install]
WantedBy=multi-user.target" > /etc/systemd/system/redis.service
###############################################################################
################# Create the Redis User, Group and Directories ################
###############################################################################
# create the redis user and group
sudo adduser --system --group --no-create-home redis
# create the redis directory
sudo mkdir /var/lib/redis
# give the redis user and group ownership over this directory
sudo chown redis:redis /var/lib/redis
#Adjust the permissions so that regular users cannot access this location
sudo chmod 770 /var/lib/redis
###############################################################################
############################ Start and Test Redis #############################
###############################################################################
sudo systemctl start redis
echo 'done - you might want to test by $sudo systemctl status redis'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment