# Deploying ISPM ERP on Ubuntu 22.04 VPS

This guide explains how to deploy ISPM ERP independently on a Linux VPS using Node.js 20, Nginx, PM2, Prisma, and SQLite.

## System requirements

- Ubuntu 22.04
- Node.js 20
- npm
- Git
- Nginx
- PM2
- Certbot
- Open ports: `80`, `443`, and optionally SSH `22`

## 1. Update the server

```bash
sudo apt update && sudo apt upgrade -y
sudo apt install -y curl git nginx ufw
```

## 2. Install Node.js 20 with NVM

```bash
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.7/install.sh | bash
export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"
nvm install 20
nvm use 20
node -v
npm -v
```

Install PM2 globally:

```bash
npm install -g pm2
```

## 3. Clone the repository

```bash
git clone https://github.com/yrachdy/ispm-erp.git
cd ispm-erp
```

## 4. Install dependencies

Install frontend dependencies:

```bash
npm install
```

Install backend dependencies:

```bash
cd server
npm install
cd ..
```

## 5. Configure environment files

Create the frontend environment file in the project root:

```bash
cat > .env << 'EOF'
VITE_API_URL=https://your-domain.com/api
EOF
```

Create the backend environment file:

```bash
cat > server/.env << 'EOF'
DATABASE_URL=file:./dev.db
JWT_SECRET=replace-with-a-long-random-secret
PORT=3001
EOF
```

## 6. Run database migrations and seed data

From `server/`:

```bash
cd server
npx prisma migrate deploy
npx prisma db seed
cd ..
```

If this is the first deployment and the SQLite database does not exist yet, you can use:

```bash
cd server
npx prisma migrate dev
npx prisma db seed
cd ..
```

## 7. Build the application

Build the frontend:

```bash
npm run build
```

Build the backend:

```bash
cd server
npm run build
cd ..
```

## 8. Configure PM2 for the backend

Create `ecosystem.config.js` in the project root:

```js
module.exports = {
  apps: [
    {
      name: 'ispm-erp-api',
      cwd: '/var/www/ispm-erp/server',
      script: 'dist/index.js',
      env: {
        NODE_ENV: 'production',
        PORT: 3001
      }
    }
  ]
}
```

Start the backend with PM2:

```bash
pm2 start ecosystem.config.js
pm2 save
pm2 startup
```

## 9. Configure Nginx

Create an Nginx site configuration:

```bash
sudo nano /etc/nginx/sites-available/ispm-erp
```

Example configuration:

```nginx
server {
    listen 80;
    server_name your-domain.com www.your-domain.com;

    root /var/www/ispm-erp/dist;
    index index.html;

    location / {
        try_files $uri $uri/ /index.html;
    }

    location /api/ {
        proxy_pass http://127.0.0.1:3001/api/;
        proxy_http_version 1.1;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
    }
}
```

Enable the site:

```bash
sudo ln -s /etc/nginx/sites-available/ispm-erp /etc/nginx/sites-enabled/ispm-erp
sudo nginx -t
sudo systemctl reload nginx
```

## 10. Enable SSL with Certbot

Install Certbot:

```bash
sudo apt install -y certbot python3-certbot-nginx
```

Request and configure the certificate:

```bash
sudo certbot --nginx -d your-domain.com -d www.your-domain.com
```

Test automatic renewal:

```bash
sudo certbot renew --dry-run
```

## 11. Configure the firewall

```bash
sudo ufw allow OpenSSH
sudo ufw allow 'Nginx Full'
sudo ufw enable
sudo ufw status
```

## 12. Verify the deployment

- Frontend: `https://your-domain.com`
- API: `https://your-domain.com/api`
- Default admin login:
  - Email: `admin@ispm.ma`
  - Password: `admin123`

## Updating the application

When deploying a new version:

```bash
cd /var/www/ispm-erp
git pull
npm install
cd server
npm install
npx prisma migrate deploy
npm run build
cd ..
npm run build
pm2 restart ispm-erp-api
sudo systemctl reload nginx
```

## Notes for production

- SQLite is suitable for small deployments and simple backups.
- For higher concurrency and easier operations, consider migrating Prisma to PostgreSQL.
- Store regular backups of `server/dev.db` and your `.env` files securely.