# Server Deployment Guide

This project contains two runtime apps:

- API server: `@workspace/api-server`
- Frontend dashboard: `@workspace/linkedin-bot`

The API listens on `API_PORT`. The frontend listens on `WEB_PORT` and proxies `/api` requests to `API_PROXY_TARGET`.

## 1. Server Requirements

Use Ubuntu 22.04/24.04 or another Linux server with:

- Node.js 20 or newer
- pnpm
- pm2
- unzip

Install Node.js 20 and tools:

```bash
sudo apt update
sudo apt install -y curl unzip
curl -fsSL https://deb.nodesource.com/setup_20.x | sudo -E bash -
sudo apt install -y nodejs
sudo corepack enable
corepack prepare pnpm@10.30.2 --activate
sudo npm install -g pm2
```

Check versions:

```bash
node -v
pnpm -v
pm2 -v
```

## 2. Upload And Extract

Upload the deployment ZIP to your server, then run:

```bash
mkdir -p /var/www/linkedin-publisher-bot
unzip LinkedIn-PublisherBot21May-server-deploy.zip -d /var/www/linkedin-publisher-bot
cd /var/www/linkedin-publisher-bot
```

If your ZIP extracts into a nested folder, `cd` into that extracted project folder before continuing.

## 3. Create Environment File

```bash
cp .env.example .env
nano .env
```

Recommended local-server values:

```env
WEB_PORT=21324
API_PORT=8081
BASE_PATH=/
API_PROXY_TARGET=http://localhost:8081
NODE_ENV=production
LOG_LEVEL=info
DATABASE_URL=pglite://.local/pglite
LINKEDIN_ACCESS_TOKEN=
TELEGRAM_BOT_TOKEN=
GROQ_API_KEY=
```

Add real integration keys only if you need those features.

## 4. Install Dependencies

```bash
pnpm install --ignore-scripts --no-frozen-lockfile
```

## 5. Build The API And Frontend

```bash
pnpm --filter @workspace/api-server run build
pnpm --filter @workspace/linkedin-bot run build
```

Optional type check:

```bash
pnpm run typecheck
```

## 6. Start With PM2

Start the API:

```bash
pm2 start pnpm --name linkedin-api -- --filter @workspace/api-server run start
```

Start the frontend preview server:

```bash
pm2 start pnpm --name linkedin-web -- --filter @workspace/linkedin-bot run serve
```

Save PM2 process list and enable startup:

```bash
pm2 save
pm2 startup
```

The `pm2 startup` command prints another command. Copy and run that printed command with `sudo`.

## 7. Open The App

Frontend:

```text
http://YOUR_SERVER_IP:21324
```

API health check:

```text
http://YOUR_SERVER_IP:8081/api/healthz
```

Frontend proxy health check:

```text
http://YOUR_SERVER_IP:21324/api/healthz
```

## 8. Useful PM2 Commands

```bash
pm2 status
pm2 logs linkedin-api
pm2 logs linkedin-web
pm2 restart linkedin-api
pm2 restart linkedin-web
pm2 stop linkedin-api
pm2 stop linkedin-web
```

## 9. Update Deployment

After uploading a new ZIP:

```bash
cd /var/www/linkedin-publisher-bot
pnpm install --ignore-scripts --no-frozen-lockfile
pnpm --filter @workspace/api-server run build
pnpm --filter @workspace/linkedin-bot run build
pm2 restart linkedin-api
pm2 restart linkedin-web
```

## 10. Notes

- Do not upload your real `.env` to public repos or shared ZIPs.
- The local PGlite database is stored in `.local/pglite`.
- To reset the local database, stop the API, delete `.local/pglite`, then start the API again.
- For a custom domain, point Nginx or Apache to the frontend port `21324` and proxy `/api` to `8081`.
