# Server Troubleshooting Guide - Blank Page Issue

## Quick Fix (Run on Server)

### Step 1: Upload Files to Server
```bash
# Upload semua file ke /var/www/html/laravel-crm/
# Pastikan .env sudah terupload dengan settings production
```

### Step 2: Run Quick Fix Script
```bash
cd /var/www/html/laravel-crm
chmod +x fix-server-issues.sh
./fix-server-issues.sh
```

### Step 3: Enable Debug Mode (Temporary)
```bash
chmod +x enable-debug.sh
./enable-debug.sh
```

### Step 4: Check Logs
```bash
chmod +x check-server-logs.sh
./check-server-logs.sh
```

## Manual Troubleshooting

### 1. Check Permissions
```bash
cd /var/www/html/laravel-crm

# Set ownership (choose based on your web server)
chown -R apache:apache .           # For Apache/CentOS
# OR
chown -R www-data:www-data .       # For Apache/Ubuntu
# OR
chown -R nginx:nginx .             # For Nginx

# Set permissions
chmod -R 775 storage bootstrap/cache
chmod -R 755 public
```

### 2. Check Laravel Logs
```bash
# View latest errors
tail -f storage/logs/laravel.log

# View last 100 lines
tail -n 100 storage/logs/laravel.log

# Search for errors
grep -i "error" storage/logs/laravel.log | tail -n 20
```

### 3. Check Apache/Web Server Logs
```bash
# CentOS/RHEL Apache
tail -f /var/log/httpd/error_log

# Ubuntu Apache
tail -f /var/log/apache2/error.log

# Nginx
tail -f /var/log/nginx/error.log
```

### 4. Check PHP Configuration
```bash
# Check PHP version
php -v

# Check required extensions
php -m | grep -E "(pdo|mysql|mbstring|openssl|json|xml)"

# Check PHP-FPM status (if using)
systemctl status php-fpm
```

### 5. Clear All Caches
```bash
php artisan config:clear
php artisan cache:clear
php artisan route:clear
php artisan view:clear
php artisan optimize:clear
```

### 6. Check .env File
```bash
# Verify .env exists
ls -la .env

# Check critical settings
cat .env | grep -E "(APP_KEY|APP_DEBUG|DB_HOST|DB_DATABASE)"
```

### 7. Generate Application Key (if missing)
```bash
php artisan key:generate
```

### 8. Test Database Connection
```bash
php artisan tinker
>>> DB::connection()->getPdo();
>>> exit
```

## Common Issues & Solutions

### Issue 1: Blank White Page
**Cause**: PHP error with display_errors=Off

**Solution**:
```bash
# Enable debug mode
sed -i 's/APP_DEBUG=false/APP_DEBUG=true/g' .env
php artisan config:clear

# Check logs
tail -f storage/logs/laravel.log
```

### Issue 2: 500 Internal Server Error
**Cause**: Permission issues or missing .htaccess

**Solution**:
```bash
# Fix permissions
chmod -R 775 storage bootstrap/cache

# Check .htaccess exists
ls -la public/.htaccess

# Enable mod_rewrite (Apache)
a2enmod rewrite
systemctl restart apache2
```

### Issue 3: "No application encryption key"
**Cause**: APP_KEY not set in .env

**Solution**:
```bash
php artisan key:generate
php artisan config:clear
```

### Issue 4: Database Connection Error
**Cause**: Wrong credentials or server not accessible

**Solution**:
```bash
# Test connection manually
mysql -h 172.16.2.97 -u dinopts_live_tes -p sales_service1

# Check .env settings
cat .env | grep DB_

# Update if needed
nano .env
```

### Issue 5: Class Not Found
**Cause**: Composer dependencies not installed

**Solution**:
```bash
composer install --no-dev --optimize-autoloader
php artisan optimize
```

### Issue 6: Storage Link Missing
**Cause**: Symbolic link not created

**Solution**:
```bash
php artisan storage:link
```

## Production Optimization

After fixing issues, optimize for production:

```bash
# 1. Disable debug
sed -i 's/APP_DEBUG=true/APP_DEBUG=false/g' .env
sed -i 's/APP_ENV=local/APP_ENV=production/g' .env

# 2. Clear and cache
php artisan config:clear
php artisan cache:clear
php artisan config:cache
php artisan route:cache
php artisan view:cache

# 3. Optimize autoloader
composer dump-autoload --optimize

# 4. Set final permissions
chmod -R 755 .
chmod -R 775 storage bootstrap/cache
```

## Apache Virtual Host Configuration

Ensure your VirtualHost is correct:

```apache
<VirtualHost *:900>
    DocumentRoot "/var/www/html/laravel-crm/public/"
    
    <Directory "/var/www/html/laravel-crm/public/">
        Options FollowSymLinks
        AllowOverride All
        Require all granted
    </Directory>
    
    <FilesMatch \.php$>
        SetHandler "proxy:fcgi://127.0.0.1:9016"
    </FilesMatch>
    
    ErrorLog /var/log/httpd/laravel-error.log
    CustomLog /var/log/httpd/laravel-access.log combined
</VirtualHost>
```

## PHP-FPM Configuration

If using PHP-FPM, check:

```bash
# Check PHP-FPM is running
systemctl status php-fpm

# Check PHP-FPM pool config
cat /etc/php-fpm.d/www.conf | grep -E "(listen|user|group)"

# Restart PHP-FPM
systemctl restart php-fpm
```

## SELinux Issues (CentOS/RHEL)

If SELinux is enabled:

```bash
# Check SELinux status
getenforce

# Set proper context
chcon -R -t httpd_sys_rw_content_t storage bootstrap/cache

# Or disable SELinux temporarily (not recommended for production)
setenforce 0
```

## Firewall

Ensure port 900 is open:

```bash
# Check if port is listening
netstat -tlnp | grep :900

# Open port in firewall
firewall-cmd --permanent --add-port=900/tcp
firewall-cmd --reload
```

## Quick Diagnostic Commands

```bash
# One-liner to check everything
echo "=== PHP ===" && php -v && \
echo "=== Permissions ===" && ls -la storage/ && \
echo "=== .env ===" && cat .env | grep -E "(APP_|DB_)" && \
echo "=== Laravel Log ===" && tail -n 20 storage/logs/laravel.log && \
echo "=== Apache Log ===" && tail -n 20 /var/log/httpd/error_log
```

## Getting Help

If still having issues, collect this information:

```bash
# Create diagnostic report
echo "=== System Info ===" > diagnostic.txt
uname -a >> diagnostic.txt
php -v >> diagnostic.txt
echo "=== Laravel Log ===" >> diagnostic.txt
tail -n 100 storage/logs/laravel.log >> diagnostic.txt
echo "=== Apache Log ===" >> diagnostic.txt
tail -n 100 /var/log/httpd/error_log >> diagnostic.txt
echo "=== Permissions ===" >> diagnostic.txt
ls -laR storage/ >> diagnostic.txt

# View report
cat diagnostic.txt
```

## Contact

Jika masih ada masalah, kirim output dari:
1. `./check-server-logs.sh`
2. `tail -n 100 storage/logs/laravel.log`
3. `tail -n 100 /var/log/httpd/error_log`
