# Using Redis

### Prerequisites

Before installing Redis, ensure your Linux server is up to date:

```bash
sudo apt update
sudo apt upgrade -y
```

### Install from Official Repositories (Recommended)

**Ubuntu/Debian**

```bash
sudo apt install redis-server -y
```

**CentOS/RHEL/Fedora**

```bash
sudo yum install redis -y
# or for newer versions
sudo dnf install redis -y
```

### Configuration

#### Basic Configuration

The main Redis configuration file is typically located at `/etc/redis/redis.conf`.

```bash
# Open configuration file
sudo nano /etc/redis/redis.conf
```

Key configuration settings to consider:

```conf
# Bind to specific IP (use 127.0.0.1 for local only)
bind 127.0.0.1

# Set a password for Redis
requirepass your_strong_password_here

# Set maximum memory limit
maxmemory 256mb
maxmemory-policy allkeys-lru

# Enable persistence (RDB snapshots)
save 900 1
save 300 10
save 60 10000

# Append-only file persistence
appendonly yes
appendfilename "appendonly.aof"
```

#### Configure Redis as a Service

If you installed from source, create a systemd service file:

```bash
sudo nano /etc/systemd/system/redis.service
```

Add the following content:

```ini
[Unit]
Description=Redis In-Memory Data Store
After=network.target

[Service]
User=redis
Group=redis
ExecStart=/usr/local/bin/redis-server /etc/redis/redis.conf
ExecStop=/usr/local/bin/redis-cli shutdown
Restart=always

[Install]
WantedBy=multi-user.target
```

Create Redis user and directories:

```bash
sudo adduser --system --group --no-create-home redis
sudo mkdir -p /var/lib/redis
sudo chown redis:redis /var/lib/redis
sudo chmod 770 /var/lib/redis
```

### Managing Redis Service

#### Start Redis

```bash
sudo systemctl start redis
# or for older systems
sudo service redis start
```

#### Enable Redis to start on boot

```bash
sudo systemctl enable redis
```

#### Check Redis status

```bash
sudo systemctl status redis
```

#### Stop Redis

```bash
sudo systemctl stop redis
```

#### Restart Redis

```bash
sudo systemctl restart redis
```

### Verify Installation

Test if Redis is working properly:

```bash
# Connect to Redis CLI
redis-cli

# If you set a password
redis-cli -a your_password

# Test with ping command
127.0.0.1:6379> ping
# Should return: PONG

# Set and get a test value
127.0.0.1:6379> set test "Hello Redis"
127.0.0.1:6379> get test
# Should return: "Hello Redis"

# Exit CLI
127.0.0.1:6379> exit
```

### Security Recommendations

1. **Set a strong password** in the configuration file using `requirepass`
2. **Bind to localhost** if Redis is only used locally:

   ```conf
   bind 127.0.0.1
   ```
3. **Configure firewall** to restrict access:

   ```bash
   # Allow only specific IP
   sudo ufw allow from trusted_ip to any port 6379
   ```
4. **Disable dangerous commands** in production:

   ```conf
   rename-command FLUSHDB ""
   rename-command FLUSHALL ""
   rename-command CONFIG ""
   ```
5. **Run Redis as non-root user** (handled automatically when installed via package manager)

### Laravel Integration

If you're using Redis with Laravel, install the required PHP extension:

```bash
# Install PHP Redis extension
sudo apt install php-redis -y

# Or via PECL
sudo pecl install redis
```

Update your Laravel `.env` file:

```env
REDIS_HOST=127.0.0.1
REDIS_PASSWORD=your_password
REDIS_PORT=6379
CACHE_DRIVER=redis
SESSION_DRIVER=redis
QUEUE_CONNECTION=redis
```

### Troubleshooting

#### Check Redis logs

```bash
sudo tail -f /var/log/redis/redis-server.log
```

#### Test Redis connection

```bash
redis-cli ping
```

#### Check Redis memory usage

```bash
redis-cli info memory
```

#### Monitor Redis in real-time

```bash
redis-cli monitor
```

### Additional Resources

* Official Redis Documentation: <https://redis.io/documentation>
* Redis Commands Reference: <https://redis.io/commands>
* Redis Security Guide: <https://redis.io/topics/security>
