277 lines
5.7 KiB
Markdown
277 lines
5.7 KiB
Markdown
# 🔴 Redis Fix Guide untuk Laravel Docker
|
|
|
|
## 🎯 Masalah yang Diselesaikan
|
|
|
|
**Error yang dialami:**
|
|
```
|
|
Class "Redis" not found
|
|
```
|
|
|
|
**Root Cause:**
|
|
- PHP Redis extension tidak terinstall di container
|
|
- Laravel dikonfigurasi untuk menggunakan Redis tetapi extension tidak tersedia
|
|
- Container perlu rebuild untuk install Redis extension
|
|
|
|
## 🚀 Solusi yang Diimplementasi
|
|
|
|
### **1. Updated Dockerfiles**
|
|
|
|
**Production (Dockerfile):**
|
|
```dockerfile
|
|
# Install Redis extension
|
|
RUN pecl install redis \
|
|
&& docker-php-ext-enable redis
|
|
```
|
|
|
|
**Development (Dockerfile.dev):**
|
|
```dockerfile
|
|
# Install Redis and Xdebug for development
|
|
RUN pecl install redis xdebug \
|
|
&& docker-php-ext-enable redis xdebug
|
|
```
|
|
|
|
### **2. Fix Steps yang Dijalankan**
|
|
|
|
```bash
|
|
# 1. Update Dockerfile dengan Redis extension
|
|
# 2. Rebuild container
|
|
docker-compose build --no-cache app
|
|
|
|
# 3. Restart container dengan image baru
|
|
docker-compose up -d app
|
|
|
|
# 4. Verify Redis extension installed
|
|
docker-compose exec app php -m | grep redis
|
|
|
|
# 5. Test Redis connection
|
|
docker-compose exec app php -r "
|
|
\$redis = new Redis();
|
|
\$redis->connect('redis', 6379);
|
|
echo 'Redis connected successfully';
|
|
"
|
|
|
|
# 6. Clear Laravel cache
|
|
docker-compose exec app php artisan config:clear
|
|
docker-compose exec app php artisan cache:clear
|
|
```
|
|
|
|
## ✅ Verifikasi Fix Berhasil
|
|
|
|
### **1. PHP Redis Extension**
|
|
```bash
|
|
# Cek extension terinstall
|
|
docker-compose exec app php -m | grep redis
|
|
# Output: redis
|
|
```
|
|
|
|
### **2. Redis Connection Test**
|
|
```bash
|
|
# Test koneksi Redis
|
|
./docker-test-redis.sh dev
|
|
```
|
|
|
|
**Expected Output:**
|
|
```
|
|
[SUCCESS] PHP Redis extension is installed
|
|
[SUCCESS] Redis server is responding
|
|
[SUCCESS] PHP Redis connection working
|
|
[SUCCESS] Laravel cache operations working
|
|
```
|
|
|
|
### **3. Web Application**
|
|
```bash
|
|
# Test web response
|
|
curl -s -o /dev/null -w "%{http_code}" http://localhost:8000/
|
|
# Output: 302 (redirect ke login page)
|
|
```
|
|
|
|
### **4. Laravel Cache Operations**
|
|
```bash
|
|
# Test Laravel cache dengan Redis
|
|
docker-compose exec app php artisan tinker --execute="
|
|
Cache::put('test', 'redis-working', 60);
|
|
echo Cache::get('test');
|
|
"
|
|
# Output: redis-working
|
|
```
|
|
|
|
## 🛠️ Tools dan Scripts
|
|
|
|
### **`docker-test-redis.sh`**
|
|
Comprehensive Redis testing script:
|
|
- ✅ Test PHP Redis extension
|
|
- ✅ Test Redis server connection
|
|
- ✅ Test Laravel cache operations
|
|
- ✅ Show Redis configuration
|
|
- ✅ Show server information
|
|
|
|
**Usage:**
|
|
```bash
|
|
# Test development environment
|
|
./docker-test-redis.sh dev
|
|
|
|
# Test production environment
|
|
./docker-test-redis.sh prod
|
|
```
|
|
|
|
### **`docker-rebuild.sh`**
|
|
Updated untuk include Redis testing:
|
|
- ✅ Test Redis extension di build process
|
|
- ✅ Verify Redis connection setelah rebuild
|
|
- ✅ Comprehensive testing semua extensions
|
|
|
|
## 🔧 Laravel Configuration
|
|
|
|
### **Environment Variables (.env)**
|
|
```env
|
|
# Redis Configuration
|
|
REDIS_HOST=redis
|
|
REDIS_PASSWORD=null
|
|
REDIS_PORT=6379
|
|
|
|
# Cache using Redis
|
|
CACHE_DRIVER=redis
|
|
|
|
# Sessions using Redis
|
|
SESSION_DRIVER=redis
|
|
|
|
# Queue using Redis
|
|
QUEUE_CONNECTION=redis
|
|
```
|
|
|
|
### **Config Files**
|
|
Laravel otomatis membaca konfigurasi dari environment variables untuk:
|
|
- `config/cache.php` - Cache driver
|
|
- `config/session.php` - Session driver
|
|
- `config/queue.php` - Queue driver
|
|
- `config/database.php` - Redis connection
|
|
|
|
## 🚨 Common Issues & Solutions
|
|
|
|
### **1. Redis Extension Missing**
|
|
**Symptoms:** `Class "Redis" not found`
|
|
**Solution:**
|
|
```bash
|
|
# Rebuild containers
|
|
./docker-rebuild.sh dev
|
|
```
|
|
|
|
### **2. Redis Connection Failed**
|
|
**Symptoms:** `Connection refused`
|
|
**Solution:**
|
|
```bash
|
|
# Check Redis container
|
|
docker-compose ps | grep redis
|
|
|
|
# Restart Redis
|
|
docker-compose restart redis
|
|
|
|
# Test connection
|
|
./docker-test-redis.sh dev
|
|
```
|
|
|
|
### **3. Laravel Config Not Loading**
|
|
**Symptoms:** Cache/session tidak menggunakan Redis
|
|
**Solution:**
|
|
```bash
|
|
# Clear Laravel cache
|
|
docker-compose exec app php artisan config:clear
|
|
docker-compose exec app php artisan cache:clear
|
|
docker-compose exec app php artisan view:clear
|
|
```
|
|
|
|
### **4. Permission Issues with Redis**
|
|
**Symptoms:** Cannot write to cache
|
|
**Solution:**
|
|
```bash
|
|
# Fix permissions
|
|
./docker-fix-permissions.sh dev
|
|
|
|
# Clear cache
|
|
docker-compose exec app php artisan cache:clear
|
|
```
|
|
|
|
## 📋 Best Practices
|
|
|
|
### **1. Container Management**
|
|
- Always rebuild containers setelah update Dockerfile
|
|
- Use scripts untuk consistent operations
|
|
- Test functionality setelah changes
|
|
|
|
### **2. Development Workflow**
|
|
```bash
|
|
# Complete setup dengan Redis
|
|
./docker-quick-setup.sh dev
|
|
|
|
# Test semua functionality
|
|
./docker-test-redis.sh dev
|
|
|
|
# Fix jika ada issues
|
|
./docker-fix-permissions.sh dev
|
|
```
|
|
|
|
### **3. Production Deployment**
|
|
```bash
|
|
# Build production containers
|
|
./docker-rebuild.sh prod
|
|
|
|
# Verify Redis working
|
|
./docker-test-redis.sh prod
|
|
|
|
# Import database
|
|
./docker-import-db.sh prod
|
|
```
|
|
|
|
## 🔍 Monitoring & Debugging
|
|
|
|
### **Redis Monitoring**
|
|
```bash
|
|
# Redis logs
|
|
docker-compose logs redis
|
|
|
|
# Redis CLI access
|
|
docker-compose exec redis redis-cli
|
|
|
|
# Redis info
|
|
docker-compose exec redis redis-cli info
|
|
|
|
# Monitor Redis commands
|
|
docker-compose exec redis redis-cli monitor
|
|
```
|
|
|
|
### **Laravel Debugging**
|
|
```bash
|
|
# Check Laravel logs
|
|
docker-compose exec app tail -f storage/logs/laravel.log
|
|
|
|
# Check cache status
|
|
docker-compose exec app php artisan cache:table
|
|
|
|
# Test cache manually
|
|
docker-compose exec app php artisan tinker
|
|
# Cache::put('test', 'value', 60);
|
|
# Cache::get('test');
|
|
```
|
|
|
|
## 📈 Performance Tips
|
|
|
|
### **1. Redis Optimization**
|
|
- Use appropriate data types
|
|
- Set proper expiration times
|
|
- Monitor memory usage
|
|
|
|
### **2. Laravel Cache Strategy**
|
|
```bash
|
|
# Cache configuration
|
|
php artisan config:cache
|
|
|
|
# Cache routes
|
|
php artisan route:cache
|
|
|
|
# Cache views
|
|
php artisan view:cache
|
|
```
|
|
|
|
---
|
|
|
|
**✅ Dengan implementasi fix ini, masalah "Class Redis not found" sudah teratasi dan aplikasi Laravel berjalan normal dengan Redis.** |