# Government CRM System

A comprehensive CRM system designed for government agencies.

## VPS Deployment Guide

### System Requirements

- Ubuntu 20.04 LTS or newer
- Node.js 16+ and npm
- PostgreSQL 12+
- Nginx
- PM2 (for process management)

### Initial Server Setup

1. Update your system:
```bash
sudo apt update && sudo apt upgrade -y
```

2. Install Node.js and npm:
```bash
curl -fsSL https://deb.nodesource.com/setup_16.x | sudo -E bash -
sudo apt install -y nodejs
```

3. Install PostgreSQL:
```bash
sudo apt install -y postgresql postgresql-contrib
```

4. Install Nginx:
```bash
sudo apt install -y nginx
```

### Database Setup

1. Create database and user:
```bash
sudo -u postgres psql
CREATE DATABASE gov_crm;
CREATE USER your_user WITH ENCRYPTED PASSWORD 'your_password';
GRANT ALL PRIVILEGES ON DATABASE gov_crm TO your_user;
\q
```

2. Configure database connection:
```bash
cp backend/.env.example backend/.env
# Edit .env with your database credentials
```

### Application Deployment

1. Clone the repository:
```bash
git clone [your-repo-url]
cd gov-crm
```

2. Install PM2 globally:
```bash
sudo npm install -g pm2
```

3. Run the deployment script:
```bash
chmod +x scripts/deploy.sh
./scripts/deploy.sh
```

### Manual Deployment Steps

If you prefer to deploy manually:

#### Backend Setup

1. Install dependencies:
```bash
cd backend
npm install
```

2. Run database migrations:
```bash
npm run migrate
```

3. Start the backend service:
```bash
pm2 start src/index.js --name gov-crm-backend
pm2 save
```

#### Frontend Setup

1. Install dependencies:
```bash
cd frontend
npm install
```

2. Build the frontend:
```bash
npm run build
```

3. Copy built files to Nginx directory:
```bash
sudo cp -r dist/* /var/www/gov-crm/
```

### Maintenance

#### Database Backups

1. Make the backup script executable:
```bash
chmod +x scripts/db-backup.sh
```

2. Set up daily backups with cron:
```bash
sudo crontab -e
# Add this line:
0 0 * * * /path/to/gov-crm/scripts/db-backup.sh
```

### SSL Configuration

1. Install Certbot:
```bash
sudo apt install -y certbot python3-certbot-nginx
```

2. Obtain SSL certificate:
```bash
sudo certbot --nginx -d your-domain.com
```

### Monitoring

1. Monitor backend service:
```bash
pm2 status
pm2 logs gov-crm-backend
```

2. Monitor Nginx:
```bash
sudo systemctl status nginx
sudo tail -f /var/log/nginx/error.log
```

### Troubleshooting

- Check Nginx logs: `/var/log/nginx/`
- Check PM2 logs: `pm2 logs`
- Check application logs: `/var/log/gov-crm/`
- Database logs: `/var/log/postgresql/`

### Security Considerations

1. Configure firewall:
```bash
sudo ufw allow 80/tcp
sudo ufw allow 443/tcp
sudo ufw allow 22/tcp
sudo ufw enable
```

2. Set up fail2ban:
```bash
sudo apt install -y fail2ban
```

3. Regular updates:
```bash
sudo apt update && sudo apt upgrade -y
```

For additional security measures, please refer to the security documentation.
