Skip to content

Instantly share code, notes, and snippets.

@iamhabib
Last active January 19, 2025 19:09
Show Gist options
  • Select an option

  • Save iamhabib/efcf34aabddee2f063daa529a4ee7a87 to your computer and use it in GitHub Desktop.

Select an option

Save iamhabib/efcf34aabddee2f063daa529a4ee7a87 to your computer and use it in GitHub Desktop.
#!/bin/bash
# Exit immediately if a command exits with a non-zero status
set -e
# Variables
REDIS_PASSWORD=$(openssl rand -base64 37 | cut -c1-49)
echo -e "Password: =>\033[1;32m$REDIS_PASSWORD\033[0m<="
REDIS_CONFIG_PATH="/etc/redis/redis.conf"
REDIS_LOG_PATH="/var/log/redis"
REDIS_DATA_PATH="/var/lib/redis"
# Function to check if Redis is already installed
check_redis_installed() {
if command -v redis-server > /dev/null 2>&1; then
echo "Redis is already installed."
read -p "Do you want to reinstall Redis? (yes/no): " choice
if [[ "$choice" != "yes" ]]; then
echo "Skipping Redis installation."
exit 0
else
echo "Reinstalling Redis..."
sudo systemctl stop redis-server || true
sudo apt-get remove --purge -y redis-server
sudo rm -rf /var/lib/redis /etc/redis /var/log/redis
fi
fi
}
# Install Redis via APT
install_redis() {
echo "Installing Redis from APT..."
sudo apt update
sudo apt install -y redis-server
}
# Set up directories and permissions
setup_directories() {
echo "Setting up Redis directories..."
# Ensure Redis user exists
if ! id -u redis > /dev/null 2>&1; then
echo "Redis user does not exist. Creating it..."
sudo adduser --system --group --no-create-home redis
fi
# Create directories
sudo mkdir -p $REDIS_LOG_PATH
sudo mkdir -p $REDIS_DATA_PATH
sudo chown redis:redis $REDIS_DATA_PATH
sudo chmod 770 $REDIS_DATA_PATH
}
# Configure Redis as a systemd service
setup_systemd_service() {
echo "Configuring Redis as a systemd service..."
sudo cp /etc/redis/redis.conf /etc/redis/redis.conf.backup || true
sudo tee /etc/systemd/system/redis-server.service > /dev/null <<EOL
[Unit]
Description=Redis In-Memory Data Store
After=network.target
[Service]
User=redis
Group=redis
ExecStart=/usr/bin/redis-server $REDIS_CONFIG_PATH
ExecStop=/usr/bin/redis-cli shutdown
Restart=always
[Install]
WantedBy=multi-user.target
EOL
sudo systemctl daemon-reload
}
# Configure Redis security and settings
configure_redis() {
echo "Configuring Redis security settings..."
sudo tee $REDIS_CONFIG_PATH > /dev/null <<EOL
# Redis Configuration File
# Bind to all interfaces
bind 0.0.0.0
# Require password authentication
requirepass $REDIS_PASSWORD
# Protect against data loss: Append-only mode
appendonly yes
# Set maximum memory usage and eviction policy
maxmemory 512mb
maxmemory-policy allkeys-lru
# Disable dangerous commands
rename-command FLUSHALL ""
rename-command FLUSHDB ""
rename-command DEBUG ""
# Logging
logfile $REDIS_LOG_PATH/redis.log
# Set data directory
dir $REDIS_DATA_PATH
# Enable TCP Keepalive
tcp-keepalive 60
# Limit the number of client connections
maxclients 1000
# Protected mode
protected-mode yes
EOL
# Set up permissions for Redis files
sudo chown redis:redis $REDIS_CONFIG_PATH
sudo chmod 640 $REDIS_CONFIG_PATH
# Set up permissions for Redis log
sudo touch $REDIS_LOG_PATH/redis.log
sudo chown redis:redis $REDIS_LOG_PATH/redis.log
}
# Start Redis and enable the service
start_redis() {
echo "Starting Redis server..."
sudo systemctl enable redis-server
sleep 5
if ! sudo systemctl start redis-server; then
echo "Failed to start Redis server. Check /var/log/redis/redis.log for errors."
exit 1
fi
echo "Verifying Redis installation..."
if systemctl status redis-server | grep -q "active (running)"; then
echo "Redis is successfully installed and running!"
echo "You can connect to the Redis server with the following details:"
echo "Host: $(hostname -I | awk '{print $1}')"
echo "Port: 6379"
echo -e "Password: =>\033[1;32m$REDIS_PASSWORD\033[0m<="
echo "Please save this password securely."
else
echo "Redis installation or startup failed. Check the logs for details."
exit 1
fi
}
# Main script execution
main() {
check_redis_installed
install_redis
setup_directories
setup_systemd_service
configure_redis
start_redis
}
# Execute the script
main
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment