# M1 ERP Server Deployment Checklist

Quick reference for deploying M1 ERP to a production server.  
📘 **Full Guide**: See `DEPLOYMENT_GUIDE.md` for detailed instructions.

---

## Pre-Deployment

- [ ] **GitHub Access Token** created (https://github.com/settings/tokens)
  - Scope: `repo` (full private repository access)
  - Expiration: 1 year
  - Token saved securely

- [ ] **Server Access** confirmed
  - SSH access: `ssh user@server-ip`
  - Root/sudo privileges available
  
- [ ] **Domain/IP** ready
  - Domain name (or IP address): `_______________`
  - DNS configured (if using domain)

- [ ] **Credentials Prepared**
  - Database password: `_______________` (strong, random)
  - Email SMTP password: `_______________`
  - Admin login password: `_______________`

---

## Phase 1: Server Setup (30-45 min)

### Software Installation

- [ ] System updated: `sudo apt update && sudo apt upgrade -y`
- [ ] Apache installed: `sudo apt install apache2 -y`
- [ ] PHP 8.1 installed: `sudo apt install php8.1 php8.1-cli php8.1-mysql ...`
- [ ] MariaDB installed: `sudo apt install mariadb-server -y`
- [ ] Git installed: `sudo apt install git -y`
- [ ] MariaDB secured: `sudo mysql_secure_installation`

### Verification

- [ ] Apache running: `sudo systemctl status apache2`
- [ ] PHP version: `php -v` (should be 8.1.x)
- [ ] MariaDB running: `sudo systemctl status mariadb`

---

## Phase 2: Code Deployment (10-15 min)

### Clone Repository

- [ ] Directories created:
  ```bash
  sudo mkdir -p /var/www/m1_erp
  sudo mkdir -p /var/www/m1_erp_config
  ```

- [ ] Repository cloned:
  ```bash
  cd /var/www/m1_erp
  sudo git clone https://github.com/merkuriddg/m1_erp.git .
  ```

- [ ] Files verified: `ls -la` shows `public/`, `core/`, `controllers/`, etc.

---

## Phase 3: Database Setup (15-20 min)

### Create Database

- [ ] Database created: `CREATE DATABASE m1_erp;`
- [ ] User created: `CREATE USER 'm1_erp_user'@'localhost' IDENTIFIED BY '...';`
- [ ] Privileges granted: `GRANT ALL PRIVILEGES ON m1_erp.* TO 'm1_erp_user'@'localhost';`

### Import Schema

- [ ] Migrations checked: `cd /var/www/m1_erp/database/migrations && ls -1 *.sql | sort`
- [ ] Base schema imported (if exists)
- [ ] All numbered migrations run in order
- [ ] Tables verified: `mysql -u m1_erp_user -p m1_erp -e "SHOW TABLES;"`

### Optional: Import Data

- [ ] Local backup created: `mysqldump -u rpmbbu brickwal_m1_ds > export.sql`
- [ ] Backup transferred: `scp export.sql user@server:/tmp/`
- [ ] Data imported: `mysql -u m1_erp_user -p m1_erp < /tmp/export.sql`

---

## Phase 4: Configuration (10-15 min)

### Create Config File

- [ ] Config file created: `sudo nano /var/www/m1_erp_config/config.php`
- [ ] Values updated:
  - `DB_PASS` = `_______________`
  - `BASE_URL` = `_______________`
  - `APP_KEY` = `_______________` (random 32 chars)
  - `ENCRYPTION_KEY` = `_______________` (random 32 chars)
  - `SMTP_USERNAME` = `_______________`
  - `SMTP_PASSWORD` = `_______________`

- [ ] Random keys generated:
  ```bash
  openssl rand -base64 32  # For APP_KEY
  openssl rand -base64 32  # For ENCRYPTION_KEY
  ```

- [ ] Config secured:
  ```bash
  sudo chmod 640 /var/www/m1_erp_config/config.php
  sudo chgrp www-data /var/www/m1_erp_config/config.php
  ```

---

## Phase 5: Web Server (10-15 min)

### Apache Configuration

- [ ] Virtual host created: `sudo nano /etc/apache2/sites-available/m1_erp.conf`
- [ ] ServerName updated in config
- [ ] mod_rewrite enabled: `sudo a2enmod rewrite`
- [ ] Site enabled: `sudo a2ensite m1_erp.conf`
- [ ] Default site disabled: `sudo a2dissite 000-default.conf`
- [ ] Config tested: `sudo apache2ctl configtest` (should say "Syntax OK")
- [ ] Apache restarted: `sudo systemctl restart apache2`

---

## Phase 6: File Permissions (5 min)

- [ ] Ownership set: `sudo chown -R www-data:www-data /var/www/m1_erp`
- [ ] Directory perms: `sudo find /var/www/m1_erp -type d -exec chmod 755 {} \;`
- [ ] File perms: `sudo find /var/www/m1_erp -type f -exec chmod 644 {} \;`
- [ ] Uploads writable: `sudo chmod 775 /var/www/m1_erp/uploads`
- [ ] Debug writable: `sudo chmod 775 /var/www/m1_erp/debug`
- [ ] Scripts executable: `sudo chmod +x /var/www/m1_erp/scripts/*.sh`

---

## Phase 7: Security (15-20 min)

### Firewall

- [ ] UFW installed: `sudo apt install ufw -y`
- [ ] SSH allowed: `sudo ufw allow OpenSSH`
- [ ] HTTP allowed: `sudo ufw allow 80/tcp`
- [ ] HTTPS allowed: `sudo ufw allow 443/tcp`
- [ ] Firewall enabled: `sudo ufw enable`
- [ ] Status checked: `sudo ufw status`

### SSL (Optional but Recommended)

- [ ] Certbot installed: `sudo apt install certbot python3-certbot-apache -y`
- [ ] Certificate obtained: `sudo certbot --apache -d your-domain.com`
- [ ] Auto-renewal tested: `sudo certbot renew --dry-run`
- [ ] BASE_URL updated in config.php to `https://...`

---

## Phase 8: Backups (10 min)

### Setup Automated Backups

- [ ] Backup directory created: `sudo mkdir -p /var/backups/m1_erp/database`
- [ ] Backup script created: `/var/www/m1_erp/scripts/production_backup.sh`
- [ ] DB password updated in script
- [ ] Script made executable: `sudo chmod +x /var/www/m1_erp/scripts/production_backup.sh`
- [ ] Cron job added: `sudo crontab -e` → `0 2 * * * /var/www/m1_erp/scripts/production_backup.sh`
  
**OR** systemd timer:
- [ ] Service created: `/etc/systemd/system/m1_erp_backup.service`
- [ ] Timer created: `/etc/systemd/system/m1_erp_backup.timer`
- [ ] Timer enabled: `sudo systemctl enable m1_erp_backup.timer`
- [ ] Timer started: `sudo systemctl start m1_erp_backup.timer`

---

## Phase 9: Testing (15-20 min)

### Basic Tests

- [ ] Web access: `curl -I http://your-domain.com` (returns 200 or 302)
- [ ] Login page loads in browser
- [ ] Database test file works (then deleted!)

### Admin User

- [ ] Admin user created (if needed):
  ```sql
  INSERT INTO users (username, email, password, role, is_active, created_at)
  VALUES ('admin', 'admin@company.com', '$2y$10$92IXUNpkjO0rOQ5byMi.Ye4oKoEa3Ro9llC/.og/at2.uheWG/igi', 'admin', 1, NOW());
  ```
- [ ] Can log in with admin/admin123
- [ ] **Password changed immediately** after first login!

### Functionality Tests

- [ ] Dashboard loads
- [ ] Can navigate between modules
- [ ] Can create/edit records
- [ ] File upload works
- [ ] No PHP errors in logs

### Logs Verification

- [ ] Apache error log checked: `sudo tail -f /var/log/apache2/m1_erp_error.log`
- [ ] PHP error log checked: `sudo tail -f /var/www/m1_erp/debug/php_errors.log`
- [ ] No critical errors

---

## Phase 10: Final Steps

### Security Hardening

- [ ] All default passwords changed
- [ ] Test database file deleted
- [ ] PHP display_errors = Off in production
- [ ] Config file permissions verified (640)

### Documentation

- [ ] Server IP documented: `_______________`
- [ ] Database credentials saved securely
- [ ] Admin credentials saved securely
- [ ] GitHub PAT saved securely (for future updates)

### Backups

- [ ] Manual backup tested: `sudo /var/www/m1_erp/scripts/production_backup.sh`
- [ ] Backup files exist: `ls -lh /var/backups/m1_erp/database/`
- [ ] Backup restore tested (optional)

---

## Post-Deployment Monitoring

### Daily/Weekly

- [ ] Check error logs for issues
- [ ] Verify backups are running
- [ ] Monitor disk space: `df -h`
- [ ] Check Apache status: `sudo systemctl status apache2`

### Monthly

- [ ] Apply system updates: `sudo apt update && sudo apt upgrade -y`
- [ ] Review user access and permissions
- [ ] Test backup restoration

### As Needed

- [ ] Update code from GitHub: `cd /var/www/m1_erp && sudo git pull origin main`
- [ ] Run new migrations
- [ ] Restart services after updates

---

## Quick Reference

| What | Where |
|------|-------|
| **Application** | `/var/www/m1_erp/` |
| **Config** | `/var/www/m1_erp_config/config.php` |
| **Web Root** | `/var/www/m1_erp/public/` |
| **Backups** | `/var/backups/m1_erp/` |
| **Logs** | `/var/log/apache2/` and `/var/www/m1_erp/debug/` |
| **Apache Config** | `/etc/apache2/sites-available/m1_erp.conf` |

---

## Emergency Contacts

- **Technical Lead**: `_______________`
- **Server Provider**: `_______________`
- **Domain Registrar**: `_______________`

---

## Common Issues

### "Configuration file not found"
- Check `/var/www/m1_erp/public/index.php` loads config from `/var/www/m1_erp_config/`

### White screen
- Check logs: `sudo tail -f /var/www/m1_erp/debug/php_errors.log`
- Temporarily enable display_errors in config.php

### Database connection failed
- Test: `mysql -u m1_erp_user -p m1_erp -e "SELECT 1;"`
- Check credentials in config.php

### 404 on all routes
- Enable mod_rewrite: `sudo a2enmod rewrite && sudo systemctl restart apache2`
- Check Apache config has `AllowOverride All`

### Upload failures
- Check permissions: `ls -la /var/www/m1_erp/uploads/` (should be www-data)
- Check PHP settings: `php -i | grep upload_max_filesize`

---

**Deployment Time Estimate**: 2-3 hours total

**Full Documentation**: `DEPLOYMENT_GUIDE.md` (1000+ lines)

**Last Updated**: 2025-11-17
