Install Redis and run as a service

To install and run Redis as a service in Ubuntu, following these steps:

1. Download and build Redis:

$ wget
$ tar xvzf redis-stable.tar.gz
$ cd redis-stable
$ make
$ make test

After the compilation the src directory inside the Redis distribution is populated with the different executables that are part of Redis:

  • redis-server is the Redis Server itself.
  • redis-cli is the command line interface utility to talk with Redis.
  • redis-benchmark is used to check Redis performances.
  • redis-check-aof and redis-check-dump are useful in the rare event of corrupted data files.

It is a good idea to copy both the Redis server than the command line interface in proper places using the following commands:

$ sudo cp redis-server /usr/local/bin/
$ sudo cp redis-cli /usr/local/bin/

2. Install Redis:

Create a directory where to store your Redis config files and your data:

$ sudo mkdir /etc/redis
$ sudo mkdir /var/redis

Copy the init script that you'll find in the Redis distribution under the utils directory into /etc/init.d. We suggest calling it with the name of the port where you are running this instance of Redis. For example:

$ sudo cp utils/redis_init_script /etc/init.d/redis_6379

Edit the init script.

$ sudo vi /etc/init.d/redis_6379

Make sure to modify REDIS_PORT accordingly to the port you are using. Both the pid file path and the configuration file name depend on the port number.

Copy the template configuration file you'll find in the root directory of the Redis distribution into /etc/redis/ using the port number as name, for instance:

$ sudo cp redis.conf /etc/redis/6379.conf

Create a directory inside /var/redis that will work as data and working directory for this Redis instance:

$ sudo mkdir /var/redis/6379

Edit the configuration file, making sure to perform the following changes:
Set daemonize to yes (by default it is set to no).
Set the pidfile to /var/run/ (modify the port if needed).
Change the port accordingly. In our example it is not needed as the default port is already 6379.
Set your preferred loglevel.
Set the logfile to /var/log/redis_6379.log
Set the dir to /var/redis/6379 (very important step!)

Finally add the new Redis init script to all the default runlevels using the following command:

$ sudo update-rc.d redis_6379 defaults

You are done! Now you can try running your instance with:

$ sudo /etc/init.d/redis_6379 start

Make sure that everything is working as expected:

  • Try pinging your instance with redis-cli.
  • Do a test save with redis-cli save and check that the dump file is correctly stored into /var/redis/6379/ (you should find a file called dump.rdb).
  • Check that your Redis instance is correctly logging in the log file.
  • If it's a new machine where you can try it without problems make sure that after a reboot everything is still working.