Installing and Configuring Redis on Linux
Installing Redis from Source on Linux
To install Redis on distributions like CentOS 7 or Ubuntu 18.04:
-
Download the source archive:
wget http://download.redis.io/releases/redis-4.0.0.tar.gz -
Extract the archive:
tar -xvf redis-4.0.0.tar.gz -
Compile the source:
cd redis-4.0.0 makeIf compilation fails due to missing dependencies:
- Install
make:apt install make - Install build tools:
apt-get install build-essential tcl - If encountering
jemallocerrrors, compile with:make CFLAGS="-DUSE_JEMALLOC=0" install
- Install
-
Install binaries:
make install -
Start the server:
redis-server -
Connect via client in another terminal:
redis-cli
Starting Redis with Custom Ports
Redis can be started on non-default ports:
-
Start server on port 6380:
redis-server --port 6380 -
Connect client to that port:
redis-cli -p 6380
Using Configuration Files
Instead of command-line flags, use a config file for persistent settings.
-
Prepare a minimal config (
redis-6379.conf):port 6379 daemonize yes logfile "6379.log" dir /usr/local/redis/redis-4.0.0/dataGenerate it by stripping comments and blanks from the default
redis.conf:cat redis.conf | grep -v "^#" | grep -v "^$" > redis-6379.conf -
Start Redis using the config:
redis-server redis-6379.conf -
Verify the process:
ps -ef | grep redis-server
Managing Multiple Redis Instances
Organize configs in a dedicated directory for multi-instance setups.
-
Create a
confdirectory and populate it:mkdir conf mv redis-6379.conf conf/ cp conf/redis-6379.conf conf/redis-6380.conf -
Edit
conf/redis-6380.confto change:port 6380 logfile "6380.log" -
Launch both instances:
redis-server conf/redis-6379.conf redis-server conf/redis-6380.conf -
Test connectivity:
redis-cli -p 6379 # Set and get key in instance 6379 redis-cli -p 6380 # Set and get key in instance 6380
Stopping Redis Instances
To stop a running instance:
-
Find its PID:
ps -ef | grep redis-server -
Terminate it:
kill -9 <PID>