# Installation Guide

## Prerequisites

- Ubuntu 24.04/26.04 LTS
- Root or sudo access
- Domain name (for production)
- Minimum 2GB RAM, 20GB storage

## 1. System Setup

```bash
# Update system
sudo apt update && sudo apt upgrade -y

# Install essential packages
sudo apt install -y software-properties-common curl git unzip
```

## 2. PHP 8.3 Installation

```bash
# Add PHP repository
sudo add-apt-repository ppa:ondrej/php -y
sudo apt update

# Install PHP and extensions
sudo apt install -y php8.3 php8.3-fpm php8.3-mysql php8.3-redis \
    php8.3-mbstring php8.3-xml php8.3-curl php8.3-gd php8.3-zip \
    php8.3-bcmath php8.3-intl php8.3-imagick php8.3-readline php8.3-dom
```

## 3. MySQL Setup

```bash
# Install MySQL
sudo apt install -y mysql-server

# Secure installation
sudo mysql_secure_installation

# Create database and user
sudo mysql -e "CREATE DATABASE exim_erp CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;"
sudo mysql -e "CREATE USER 'exim_erp_user'@'localhost' IDENTIFIED BY 'YourSecurePassword';"
sudo mysql -e "GRANT ALL PRIVILEGES ON exim_erp.* TO 'exim_erp_user'@'localhost';"
sudo mysql -e "FLUSH PRIVILEGES;"
```

## 4. Redis Setup

```bash
# Install Redis
sudo apt install -y redis-server

# Configure Redis
sudo sed -i 's/^supervised no/supervised systemd/' /etc/redis/redis.conf
sudo sed -i 's/^# maxmemory <bytes>/maxmemory 256mb/' /etc/redis/redis.conf
sudo sed -i 's/^# maxmemory-policy noeviction/maxmemory-policy allkeys-lru/' /etc/redis/redis.conf

# Start Redis
sudo systemctl restart redis-server
sudo systemctl enable redis-server
```

## 5. Nginx Setup

```bash
# Install Nginx
sudo apt install -y nginx

# Create web directory
sudo mkdir -p /var/www/html/exim-erp
```

## 6. SSL Setup

```bash
# Install Certbot
sudo apt install -y certbot python3-certbot-nginx

# Obtain certificate (replace with your domain)
sudo certbot --nginx -d your-domain.com

# Auto-renewal is set up automatically
sudo certbot renew --dry-run
```

## 7. Application Installation

```bash
# Clone repository
cd /var/www/html
sudo git clone https://github.com/your-org/exim-erp.git
cd exim-erp

# Set permissions
sudo chown -R www-data:www-data /var/www/html/exim-erp
sudo chmod -R 755 /var/www/html/exim-erp
sudo chmod -R 775 storage bootstrap/cache
```

## 8. Laravel Configuration

```bash
# Install Composer
curl -sS https://getcomposer.org/installer | php
sudo mv composer.phar /usr/local/bin/composer

# Install dependencies
composer install --no-dev --optimize-autoloader

# Configure environment
cp .env.example .env
php artisan key:generate

# Update .env with your settings
nano .env
```

### Environment Variables

```env
APP_NAME="EXIM ERP"
APP_ENV=production
APP_KEY=base64:your-generated-key
APP_URL=https://your-domain.com

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=exim_erp
DB_USERNAME=exim_erp_user
DB_PASSWORD=YourSecurePassword

REDIS_HOST=127.0.0.1
REDIS_PASSWORD=null
REDIS_PORT=6379

CACHE_DRIVER=redis
QUEUE_CONNECTION=redis
SESSION_DRIVER=redis
```

## 9. Database Migration

```bash
# Run migrations
php artisan migrate --force

# Seed database
php artisan db:seed --force
```

## 10. Supervisor Setup

```bash
# Copy configuration
sudo cp deployment/supervisor/*.conf /etc/supervisor/conf.d/

# Restart Supervisor
sudo supervisorctl reread
sudo supervisorctl update
```

## 11. Nginx Configuration

```bash
# Copy Nginx config
sudo cp deployment/nginx/exim-erp.conf /etc/nginx/sites-available/exim-erp
sudo ln -sf /etc/nginx/sites-available/exim-erp /etc/nginx/sites-enabled/
sudo rm -f /etc/nginx/sites-enabled/default

# Test and restart
sudo nginx -t
sudo systemctl restart nginx
```

## 12. PHP-FPM Configuration

```bash
# Copy PHP settings
sudo cp deployment/php/99-exim-erp.ini /etc/php/8.3/fpm/conf.d/99-exim-erp.ini
sudo cp deployment/php/php-fpm-pool.conf /etc/php/8.3/fpm/pool.d/exim-erp.conf

# Restart PHP-FPM
sudo systemctl restart php8.3-fpm
```

## 13. Backup Setup

```bash
# Make scripts executable
sudo chmod +x deployment/scripts/backup.sh
sudo chmod +x deployment/scripts/monitor.sh

# Create backup directory
sudo mkdir -p /var/backups/exim-erp

# Install cron jobs
sudo cp deployment/cron/exim-erp /etc/cron.d/
sudo chmod 644 /etc/cron.d/exim-erp
```

## 14. Laravel Optimization

```bash
# Cache configuration
php artisan config:cache
php artisan route:cache
php artisan view:cache
php artisan event:cache
php artisan optimize
```

## 15. Final Verification

```bash
# Check services
sudo systemctl status php8.3-fpm
sudo systemctl status nginx
sudo systemctl status mysql
sudo systemctl status redis-server
sudo supervisorctl status

# Test application
curl -I https://your-domain.com
```

## Default Login

- **Email**: admin@exim-erp.com
- **Password**: password

## Post-Installation

1. Change default password immediately
2. Configure email settings in `.env`
3. Set up automated backups
4. Configure monitoring alerts
5. Review security settings

## Troubleshooting

### Common Issues

**502 Bad Gateway**
```bash
# Check PHP-FPM
sudo systemctl status php8.3-fpm
sudo tail -f /var/log/php8.3-fpm.log
```

**Permission Denied**
```bash
sudo chown -R www-data:www-data /var/www/html/exim-erp
sudo chmod -R 775 storage bootstrap/cache
```

**Database Connection Failed**
```bash
# Check MySQL
sudo systemctl status mysql
mysql -u exim_erp_user -p exim_erp
```

**Redis Connection Failed**
```bash
# Check Redis
sudo systemctl status redis-server
redis-cli ping
```

**Queue Workers Not Running**
```bash
sudo supervisorctl status
sudo supervisorctl restart exim-erp-worker:*
```
