Set up a Redis cluster with Nginx caching for high availability and improved performance. This configuration provides distributed caching with automatic failover and enhanced scalability for production web applications.
Prerequisites
- Root or sudo access
- At least 4GB RAM
- Basic understanding of Redis and Nginx
- Network connectivity between cluster nodes
What this solves
Redis cluster caching with Nginx provides distributed, fault-tolerant caching that automatically handles node failures while maintaining high performance. This setup eliminates single points of failure in your caching layer and scales horizontally to handle increased traffic loads. You need this when your application requires high availability caching that can survive individual Redis node failures without losing service availability.
Step-by-step installation
Update system packages
Start by updating your package manager to ensure you have the latest security patches and package versions.
sudo apt update && sudo apt upgrade -y
Install Redis server and development tools
Install Redis and the necessary development packages to build Nginx with Redis module support.
sudo apt install -y redis-server build-essential libpcre3-dev zlib1g-dev libssl-dev libgeoip-dev libhiredis-dev git
Download and compile Nginx with Redis module
Download Nginx source code and the Redis module, then compile them together for Redis caching support.
cd /tmp
wget http://nginx.org/download/nginx-1.24.0.tar.gz
tar -xzf nginx-1.24.0.tar.gz
git clone https://github.com/openresty/redis2-nginx-module.git
git clone https://github.com/openresty/set-misc-nginx-module.git
git clone https://github.com/openresty/echo-nginx-module.git
Configure and compile Nginx
Configure Nginx with the Redis module and other necessary modules, then compile and install it.
cd /tmp/nginx-1.24.0
./configure --prefix=/etc/nginx \
--sbin-path=/usr/sbin/nginx \
--modules-path=/usr/lib/nginx/modules \
--conf-path=/etc/nginx/nginx.conf \
--error-log-path=/var/log/nginx/error.log \
--http-log-path=/var/log/nginx/access.log \
--pid-path=/var/run/nginx.pid \
--lock-path=/var/run/nginx.lock \
--http-client-body-temp-path=/var/cache/nginx/client_temp \
--http-proxy-temp-path=/var/cache/nginx/proxy_temp \
--http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp \
--http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp \
--http-scgi-temp-path=/var/cache/nginx/scgi_temp \
--with-http_ssl_module \
--with-http_realip_module \
--with-http_addition_module \
--with-http_sub_module \
--with-http_dav_module \
--with-http_flv_module \
--with-http_mp4_module \
--with-http_gunzip_module \
--with-http_gzip_static_module \
--with-http_random_index_module \
--with-http_secure_link_module \
--with-http_stub_status_module \
--with-http_auth_request_module \
--with-threads \
--with-stream \
--with-stream_ssl_module \
--with-http_slice_module \
--with-http_v2_module \
--add-module=/tmp/redis2-nginx-module \
--add-module=/tmp/set-misc-nginx-module \
--add-module=/tmp/echo-nginx-module
make -j$(nproc)
sudo make install
Create Nginx user and directories
Create the nginx user account and necessary directories with proper permissions for security.
sudo useradd --system --home /var/cache/nginx --shell /sbin/nologin --comment "nginx user" --user-group nginx
sudo mkdir -p /var/cache/nginx/client_temp
sudo mkdir -p /var/cache/nginx/proxy_temp
sudo mkdir -p /var/cache/nginx/fastcgi_temp
sudo mkdir -p /var/cache/nginx/uwsgi_temp
sudo mkdir -p /var/cache/nginx/scgi_temp
sudo chown -R nginx:nginx /var/cache/nginx
sudo chmod 755 /var/cache/nginx
Create Nginx systemd service
Create a systemd service file to manage Nginx with proper service controls and security settings.
[Unit]
Description=The nginx HTTP and reverse proxy server
Documentation=http://nginx.org/en/docs/
After=network.target remote-fs.target nss-lookup.target
Wants=network-online.target
[Service]
Type=forking
PIDFile=/var/run/nginx.pid
ExecStartPre=/usr/sbin/nginx -t
ExecStart=/usr/sbin/nginx
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s QUIT $MAINPID
KillMode=mixed
PrivateTmp=true
User=nginx
Group=nginx
[Install]
WantedBy=multi-user.target
Configure Redis cluster nodes
Create Redis configuration files for a 6-node cluster (3 masters, 3 replicas) with cluster mode enabled.
sudo mkdir -p /etc/redis/cluster
sudo mkdir -p /var/lib/redis/cluster
for port in 7000 7001 7002 7003 7004 7005; do
sudo mkdir -p /var/lib/redis/cluster/$port
done
Create Redis cluster configuration
Generate configuration files for each Redis cluster node with appropriate settings for clustering and persistence.
for port in 7000 7001 7002 7003 7004 7005; do
sudo tee /etc/redis/cluster/redis-$port.conf > /dev/null <
Create Redis cluster systemd services
Create individual systemd service files for each Redis cluster node to manage them independently.
for port in 7000 7001 7002 7003 7004 7005; do
sudo tee /etc/systemd/system/redis-cluster-$port.service > /dev/null <
Set proper ownership and start Redis cluster
Configure file ownership and start all Redis cluster nodes with proper permissions.
sudo chown -R redis:redis /etc/redis/cluster
sudo chown -R redis:redis /var/lib/redis/cluster
sudo chmod 640 /etc/redis/cluster/*.conf
sudo chmod 755 /var/lib/redis/cluster/*
sudo systemctl daemon-reload
for port in 7000 7001 7002 7003 7004 7005; do
sudo systemctl enable redis-cluster-$port
sudo systemctl start redis-cluster-$port
done
Initialize Redis cluster
Create the Redis cluster by joining all nodes together and assigning slots for data distribution.
sleep 5
HOST_IP=$(hostname -I | awk '{print $1}')
redis-cli --cluster create \
$HOST_IP:7000 $HOST_IP:7001 $HOST_IP:7002 \
$HOST_IP:7003 $HOST_IP:7004 $HOST_IP:7005 \
--cluster-replicas 1 --cluster-yes
Configure Nginx with Redis cluster caching
Create the main Nginx configuration with upstream Redis cluster nodes and caching directives.
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
use epoll;
multi_accept on;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for" '
'rt=$request_time uct="$upstream_connect_time" '
'uht="$upstream_header_time" urt="$upstream_response_time"';
access_log /var/log/nginx/access.log main;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
client_max_body_size 16M;
# Gzip compression
gzip on;
gzip_vary on;
gzip_min_length 1024;
gzip_proxied any;
gzip_comp_level 6;
gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;
# Redis upstream cluster
upstream redis_cluster {
server 127.0.0.1:7000 max_fails=3 fail_timeout=30s;
server 127.0.0.1:7001 max_fails=3 fail_timeout=30s;
server 127.0.0.1:7002 max_fails=3 fail_timeout=30s;
keepalive 32;
}
# Cache zones
proxy_cache_path /var/cache/nginx/redis_cache levels=1:2 keys_zone=redis_cache:10m max_size=1g inactive=1h use_temp_path=off;
include /etc/nginx/conf.d/*.conf;
}
Create Redis cache configuration
Create a specific configuration file for Redis caching functionality with cache management and failover logic.
server {
listen 80;
server_name example.com www.example.com;
# Cache status endpoint
location /nginx_status {
stub_status on;
access_log off;
allow 127.0.0.1;
deny all;
}
# Redis cache endpoint
location /redis/ {
internal;
redis2_query auth $redis_password;
redis2_query $args;
redis2_pass redis_cluster;
redis2_connect_timeout 1s;
redis2_send_timeout 1s;
redis2_read_timeout 1s;
redis2_buffer_size 4k;
}
# Main application with Redis caching
location / {
set $redis_key "cache:$scheme:$host:$request_uri";
# Try to get from Redis first
redis2_query get $redis_key;
redis2_pass redis_cluster;
# If Redis fails or cache miss, go to backend
error_page 502 504 = @fallback;
# Add cache headers
add_header X-Cache-Status "HIT from Redis";
}
# Fallback to backend when Redis fails
location @fallback {
proxy_pass http://backend_servers;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
# Cache the response in Redis for future requests
access_by_lua_block {
local redis_key = ngx.var.redis_key
local response = ngx.var.upstream_response_body
if response then
-- Store in Redis with 1 hour expiration
local redis_store = ngx.location.capture("/redis/", {
args = "setex " .. redis_key .. " 3600 " .. response
})
end
}
add_header X-Cache-Status "MISS - Stored in Redis";
}
# Backend servers (replace with your actual backend)
location @backend {
proxy_pass http://127.0.0.1:8080;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_connect_timeout 5s;
proxy_send_timeout 5s;
proxy_read_timeout 5s;
}
}
Upstream backend servers
upstream backend_servers {
server 127.0.0.1:8080 max_fails=3 fail_timeout=30s;
# Add more backend servers as needed
# server 127.0.0.1:8081 max_fails=3 fail_timeout=30s;
keepalive 16;
}
Create cache directory and set permissions
Create the Nginx cache directory with proper ownership and permissions for secure cache storage.
sudo mkdir -p /var/cache/nginx/redis_cache
sudo chown -R nginx:nginx /var/cache/nginx
sudo chmod 755 /var/cache/nginx
sudo chmod 755 /var/cache/nginx/redis_cache
Enable and start services
Enable both Nginx and all Redis cluster services to start automatically on system boot.
sudo systemctl daemon-reload
sudo systemctl enable nginx
sudo systemctl start nginx
Verify all services are running
sudo systemctl status nginx
for port in 7000 7001 7002 7003 7004 7005; do
sudo systemctl status redis-cluster-$port
done
Configure Redis cluster monitoring script
Create a monitoring script to check cluster health and automatically handle failover scenarios.
#!/bin/bash
Redis cluster monitoring script
LOG_FILE="/var/log/redis-cluster-monitor.log"
HOST_IP=$(hostname -I | awk '{print $1}')
PORTS=(7000 7001 7002 7003 7004 7005)
log_message() {
echo "$(date '+%Y-%m-%d %H:%M:%S') - $1" >> $LOG_FILE
}
check_cluster_health() {
for port in "${PORTS[@]}"; do
if ! redis-cli -h $HOST_IP -p $port ping > /dev/null 2>&1; then
log_message "WARNING: Redis node $HOST_IP:$port is not responding"
# Attempt to restart the node
sudo systemctl restart redis-cluster-$port
log_message "INFO: Attempted to restart redis-cluster-$port"
else
log_message "INFO: Redis node $HOST_IP:$port is healthy"
fi
done
# Check cluster state
cluster_state=$(redis-cli -h $HOST_IP -p 7000 cluster info | grep cluster_state | cut -d: -f2)
if [[ "$cluster_state" != "ok" ]]; then
log_message "WARNING: Cluster state is not OK: $cluster_state"
else
log_message "INFO: Cluster state is healthy"
fi
}
Main execution
log_message "Starting Redis cluster health check"
check_cluster_health
log_message "Redis cluster health check completed"
Set up monitoring cron job
Configure automated cluster health monitoring to run every 5 minutes and handle failures automatically.
sudo chmod +x /usr/local/bin/redis-cluster-monitor.sh
sudo chown root:root /usr/local/bin/redis-cluster-monitor.sh
Add cron job for monitoring
echo "/5 * /usr/local/bin/redis-cluster-monitor.sh" | sudo crontab -
Configure performance optimization
Tune Redis cluster memory settings
Optimize Redis memory usage and eviction policies for better cache performance across the cluster.
for port in 7000 7001 7002 7003 7004 7005; do
redis-cli -h 127.0.0.1 -p $port CONFIG SET maxmemory-samples 5
redis-cli -h 127.0.0.1 -p $port CONFIG SET tcp-keepalive 300
redis-cli -h 127.0.0.1 -p $port CONFIG SET timeout 0
done
Optimize Nginx worker settings
Configure Nginx worker processes and connections for optimal Redis cluster communication performance.
# Performance optimizations
worker_rlimit_nofile 65535;
Upstream keepalive settings
upstream redis_cluster {
server 127.0.0.1:7000 max_fails=3 fail_timeout=30s weight=1;
server 127.0.0.1:7001 max_fails=3 fail_timeout=30s weight=1;
server 127.0.0.1:7002 max_fails=3 fail_timeout=30s weight=1;
keepalive 64;
keepalive_requests 1000;
keepalive_timeout 60s;
}
Cache performance settings
proxy_cache_use_stale error timeout invalid_header updating http_500 http_502 http_503 http_504;
proxy_cache_background_update on;
proxy_cache_lock on;
proxy_cache_lock_timeout 5s;
Verify your setup
Test the Redis cluster functionality and Nginx caching integration to ensure everything works correctly.
# Check Nginx status
sudo systemctl status nginx
nginx -t
Verify Redis cluster status
redis-cli -h 127.0.0.1 -p 7000 cluster info
redis-cli -h 127.0.0.1 -p 7000 cluster nodes
Test cache functionality
curl -I http://localhost/
curl -H "Host: example.com" http://localhost/
Check cache hit statistics
for port in 7000 7001 7002; do
echo "Node $port stats:"
redis-cli -h 127.0.0.1 -p $port info stats | grep keyspace
done
Monitor cache performance
tail -f /var/log/nginx/access.log
tail -f /var/log/redis-cluster-monitor.log
Common issues
| Symptom | Cause | Fix |
|---|---|---|
| Redis cluster fails to form | Network connectivity issues | Check firewall rules, ensure ports 7000-7005 are accessible |
| Nginx fails to connect to Redis | Module not compiled correctly | Verify redis2 module in nginx -V output |
| Cache misses are high | Insufficient memory or wrong eviction policy | Increase maxmemory and check eviction policy settings |
| Cluster nodes show as failed | Timeout settings too aggressive | Increase cluster-node-timeout in Redis config |
| Permission denied on cache directory | Wrong ownership or permissions | sudo chown -R nginx:nginx /var/cache/nginx && sudo chmod 755 /var/cache/nginx |
| Monitoring script not logging | Log file permissions | sudo touch /var/log/redis-cluster-monitor.log && sudo chmod 644 /var/log/redis-cluster-monitor.log |
Next steps
- Configure Redis Sentinel with SSL/TLS encryption and authentication for high availability
- Configure NGINX load balancing with health checks and automatic failover
- Optimize Nginx caching performance with Redis backend and memory tuning for high-traffic websites
- Set up Prometheus and Grafana monitoring stack with Docker compose
- Configure Nginx Redis caching with SSL authentication and security hardening
Automated install script
Run this to automate the entire setup
#!/usr/bin/env bash
set -euo pipefail
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color
# Configuration
NGINX_VERSION="1.24.0"
REDIS_NODES="${1:-3}"
CLUSTER_NAME="${2:-redis-cluster}"
# Usage message
usage() {
echo "Usage: $0 [redis_nodes] [cluster_name]"
echo " redis_nodes: Number of Redis nodes (default: 3)"
echo " cluster_name: Name for Redis cluster (default: redis-cluster)"
exit 1
}
# Error handling
cleanup() {
local exit_code=$?
if [ $exit_code -ne 0 ]; then
echo -e "${RED}[ERROR] Installation failed. Cleaning up...${NC}"
systemctl stop nginx redis 2>/dev/null || true
rm -rf /tmp/nginx-* /tmp/redis2-nginx-module /tmp/set-misc-nginx-module /tmp/echo-nginx-module 2>/dev/null || true
fi
exit $exit_code
}
trap cleanup ERR
# Check if running as root or with sudo
if [[ $EUID -ne 0 ]]; then
echo -e "${RED}[ERROR] This script must be run as root or with sudo${NC}"
exit 1
fi
# Detect distribution
if [ -f /etc/os-release ]; then
. /etc/os-release
case "$ID" in
ubuntu|debian)
PKG_MGR="apt"
PKG_INSTALL="apt install -y"
PKG_UPDATE="apt update && apt upgrade -y"
BUILD_DEPS="build-essential libpcre3-dev zlib1g-dev libssl-dev libgeoip-dev libhiredis-dev git wget"
REDIS_PKG="redis-server"
;;
almalinux|rocky|centos|rhel|ol|fedora)
PKG_MGR="dnf"
PKG_INSTALL="dnf install -y"
PKG_UPDATE="dnf update -y"
BUILD_DEPS="gcc gcc-c++ pcre-devel zlib-devel openssl-devel geoip-devel hiredis-devel git make wget"
REDIS_PKG="redis"
;;
amzn)
PKG_MGR="yum"
PKG_INSTALL="yum install -y"
PKG_UPDATE="yum update -y"
BUILD_DEPS="gcc gcc-c++ pcre-devel zlib-devel openssl-devel geoip-devel hiredis-devel git make wget"
REDIS_PKG="redis"
;;
*)
echo -e "${RED}[ERROR] Unsupported distribution: $ID${NC}"
exit 1
;;
esac
else
echo -e "${RED}[ERROR] Cannot detect distribution${NC}"
exit 1
fi
echo -e "${GREEN}[1/10] Updating system packages...${NC}"
$PKG_UPDATE
echo -e "${GREEN}[2/10] Installing Redis and development tools...${NC}"
$PKG_INSTALL $REDIS_PKG $BUILD_DEPS
echo -e "${GREEN}[3/10] Downloading Nginx source and Redis modules...${NC}"
cd /tmp
wget -q http://nginx.org/download/nginx-${NGINX_VERSION}.tar.gz
tar -xzf nginx-${NGINX_VERSION}.tar.gz
git clone --quiet https://github.com/openresty/redis2-nginx-module.git
git clone --quiet https://github.com/openresty/set-misc-nginx-module.git
git clone --quiet https://github.com/openresty/echo-nginx-module.git
echo -e "${GREEN}[4/10] Configuring and compiling Nginx with Redis modules...${NC}"
cd /tmp/nginx-${NGINX_VERSION}
./configure --prefix=/etc/nginx \
--sbin-path=/usr/sbin/nginx \
--modules-path=/usr/lib/nginx/modules \
--conf-path=/etc/nginx/nginx.conf \
--error-log-path=/var/log/nginx/error.log \
--http-log-path=/var/log/nginx/access.log \
--pid-path=/var/run/nginx.pid \
--lock-path=/var/run/nginx.lock \
--http-client-body-temp-path=/var/cache/nginx/client_temp \
--http-proxy-temp-path=/var/cache/nginx/proxy_temp \
--http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp \
--http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp \
--http-scgi-temp-path=/var/cache/nginx/scgi_temp \
--with-http_ssl_module \
--with-http_realip_module \
--with-http_addition_module \
--with-http_sub_module \
--with-http_dav_module \
--with-http_flv_module \
--with-http_mp4_module \
--with-http_gunzip_module \
--with-http_gzip_static_module \
--with-http_random_index_module \
--with-http_secure_link_module \
--with-http_stub_status_module \
--with-http_auth_request_module \
--with-threads \
--with-stream \
--with-stream_ssl_module \
--with-http_slice_module \
--with-http_v2_module \
--add-module=/tmp/redis2-nginx-module \
--add-module=/tmp/set-misc-nginx-module \
--add-module=/tmp/echo-nginx-module >/dev/null
make -j$(nproc) >/dev/null
make install >/dev/null
echo -e "${GREEN}[5/10] Creating nginx user and directories...${NC}"
if ! id nginx >/dev/null 2>&1; then
useradd --system --home /var/cache/nginx --shell /sbin/nologin --comment "nginx user" --user-group nginx
fi
mkdir -p /var/cache/nginx/{client_temp,proxy_temp,fastcgi_temp,uwsgi_temp,scgi_temp}
mkdir -p /etc/nginx/conf.d
chown -R nginx:nginx /var/cache/nginx
chmod 755 /var/cache/nginx
echo -e "${GREEN}[6/10] Creating Nginx systemd service...${NC}"
cat > /etc/systemd/system/nginx.service << 'EOF'
[Unit]
Description=The nginx HTTP and reverse proxy server
Documentation=http://nginx.org/en/docs/
After=network.target remote-fs.target nss-lookup.target
Wants=network-online.target
[Service]
Type=forking
PIDFile=/var/run/nginx.pid
ExecStartPre=/usr/sbin/nginx -t
ExecStart=/usr/sbin/nginx
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s QUIT $MAINPID
KillMode=mixed
PrivateTmp=true
User=nginx
Group=nginx
[Install]
WantedBy=multi-user.target
EOF
echo -e "${GREEN}[7/10] Configuring Redis cluster...${NC}"
mkdir -p /etc/redis/cluster
for i in $(seq 1 $REDIS_NODES); do
PORT=$((7000 + i - 1))
mkdir -p /var/lib/redis/node-${PORT}
cat > /etc/redis/cluster/redis-${PORT}.conf << EOF
port ${PORT}
cluster-enabled yes
cluster-config-file nodes-${PORT}.conf
cluster-node-timeout 5000
appendonly yes
dir /var/lib/redis/node-${PORT}
pidfile /var/run/redis_${PORT}.pid
logfile /var/log/redis/redis-${PORT}.log
EOF
chown redis:redis /var/lib/redis/node-${PORT}
chmod 755 /var/lib/redis/node-${PORT}
done
echo -e "${GREEN}[8/10] Creating Redis cluster service files...${NC}"
for i in $(seq 1 $REDIS_NODES); do
PORT=$((7000 + i - 1))
cat > /etc/systemd/system/redis-${PORT}.service << EOF
[Unit]
Description=Advanced key-value store (port ${PORT})
After=network.target
Documentation=http://redis.io/documentation, man:redis-server(1)
[Service]
Type=notify
ExecStart=/usr/bin/redis-server /etc/redis/cluster/redis-${PORT}.conf
TimeoutStopSec=0
Restart=always
User=redis
Group=redis
RuntimeDirectory=redis
RuntimeDirectoryMode=0755
[Install]
WantedBy=multi-user.target
EOF
done
echo -e "${GREEN}[9/10] Creating Nginx configuration with Redis caching...${NC}"
cat > /etc/nginx/nginx.conf << 'EOF'
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;
events {
worker_connections 1024;
}
http {
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
include /etc/nginx/mime.types;
default_type application/octet-stream;
upstream redis_cluster {
server 127.0.0.1:7000;
server 127.0.0.1:7001;
server 127.0.0.1:7002;
}
server {
listen 80 default_server;
server_name _;
root /usr/share/nginx/html;
location /redis-health {
redis2_query ping;
redis2_pass redis_cluster;
}
location / {
set $redis_key $uri$args;
redis2_query get $redis_key;
redis2_pass redis_cluster;
error_page 404 = @fallback;
}
location @fallback {
proxy_pass http://127.0.0.1:8080;
redis2_query setex $redis_key 3600 $upstream_response_body;
redis2_pass redis_cluster;
}
}
include /etc/nginx/conf.d/*.conf;
}
EOF
mkdir -p /usr/share/nginx/html
echo "<h1>Nginx Redis Cluster Cache Ready</h1>" > /usr/share/nginx/html/index.html
chown -R nginx:nginx /usr/share/nginx/html
chmod 644 /usr/share/nginx/html/index.html
echo -e "${GREEN}[10/10] Starting and enabling services...${NC}"
systemctl daemon-reload
for i in $(seq 1 $REDIS_NODES); do
PORT=$((7000 + i - 1))
systemctl enable redis-${PORT}
systemctl start redis-${PORT}
done
systemctl enable nginx
systemctl start nginx
# Verification
echo -e "${YELLOW}Verifying installation...${NC}"
sleep 3
if systemctl is-active --quiet nginx; then
echo -e "${GREEN}✓ Nginx is running${NC}"
else
echo -e "${RED}✗ Nginx failed to start${NC}"
exit 1
fi
REDIS_RUNNING=0
for i in $(seq 1 $REDIS_NODES); do
PORT=$((7000 + i - 1))
if systemctl is-active --quiet redis-${PORT}; then
echo -e "${GREEN}✓ Redis node ${PORT} is running${NC}"
((REDIS_RUNNING++))
else
echo -e "${RED}✗ Redis node ${PORT} failed to start${NC}"
fi
done
if [ $REDIS_RUNNING -eq $REDIS_NODES ]; then
echo -e "${GREEN}✓ All Redis nodes are running${NC}"
else
echo -e "${YELLOW}⚠ Only $REDIS_RUNNING of $REDIS_NODES Redis nodes are running${NC}"
fi
# Clean up build files
rm -rf /tmp/nginx-* /tmp/redis2-nginx-module /tmp/set-misc-nginx-module /tmp/echo-nginx-module
echo -e "${GREEN}Installation completed successfully!${NC}"
echo -e "${YELLOW}Next steps:${NC}"
echo "1. Test Redis health: curl http://localhost/redis-health"
echo "2. Configure your application backend on port 8080"
echo "3. Monitor logs: tail -f /var/log/nginx/error.log"
Review the script before running. Execute with: bash install.sh