Database Intermediate

Redis Installation and Configuration

Installs Redis cache server, configures and applies security settings.

Published: April 15, 2024

Detailed Information

This script installs, configures, and applies security settings for Redis cache server. Redis is a high-performance in-memory data structure store used for caching, session management, and real-time applications.

What Does This Script Do?

This script automates Redis installation and configuration:

  • Installs Redis server
  • Creates security password
  • Sets memory limit
  • Sets memory management policy
  • Starts and enables service

Why Should You Use It?

Redis is a critical component for modern web applications:

  • High Performance: In-memory data access
  • Cache Solution: Reduces database load
  • Session Management: Distributed session storage

How to Use

Step-by-Step Usage Guide

1. Run Script

sudo chmod +x redis_install.sh
sudo ./redis_install.sh

2. Save Password

Save the password shown in script output.

3. Test Connection

redis-cli -a  ping

Requirements

Requirements

  • Root Privileges: Script must be run as root
  • Ubuntu/Debian: Script optimized for Ubuntu/Debian

Use Cases

Use Cases

1. Web Application Cache

Use as cache layer for your web applications.

2. Session Management

Use for distributed session management.

Examples

Usage Examples

Example 1: Basic Usage

sudo ./redis_install.sh

Code

#!/bin/bash

# Redis Installation Script

if [ "$EUID" -ne 0 ]; then 
    echo "Please run as root"
    exit 1
fi

echo "Installing Redis..."

apt-get update
apt-get install -y redis-server

sed -i "s/bind 127.0.0.1/bind 127.0.0.1/" /etc/redis/redis.conf
sed -i "s/# requirepass foobared/requirepass $(openssl rand -base64 32)/" /etc/redis/redis.conf
sed -i "s/# maxmemory <bytes>/maxmemory 256mb/" /etc/redis/redis.conf
sed -i "s/# maxmemory-policy noeviction/maxmemory-policy allkeys-lru/" /etc/redis/redis.conf

systemctl enable redis-server
systemctl restart redis-server

PASSWORD=$(grep "^requirepass" /etc/redis/redis.conf | cut -d" " -f2)

echo "✓ Redis installed and configured!"
echo ""
echo "Password: $PASSWORD"
echo ""
echo "Test connection:"
echo "redis-cli -a $PASSWORD ping"

Usage

sudo chmod +x redis_install.sh
sudo ./redis_install.sh

# Test connection
redis-cli -a <password> ping

Troubleshooting

Troubleshooting

Problem: Redis not starting

Solution: Check service:

sudo systemctl status redis-server
sudo systemctl restart redis-server

Tags

redis cache nosql database