# Finance Tracker Pro - Self-Hosting Deployment Guide

## Prerequisites

- Web hosting with Node.js support (18+ recommended)
- PostgreSQL database access
- SSH access (recommended) or File Manager via cPanel
- Domain: finance.tirtomarwi.com

## Step 1: Download Project Files

Download all files from your Replit project:
- All files in the root directory
- `client/` folder (React frontend)
- `server/` folder (Express backend)
- `shared/` folder (TypeScript schemas)
- `package.json` and configuration files

## Step 2: Database Setup

### Option A: Use Hosting Provider's PostgreSQL
1. Create a PostgreSQL database via cPanel
2. Note the connection details:
   - Host: usually `localhost` or provided hostname
   - Database name
   - Username and password
   - Port (usually 5432)

### Option B: External PostgreSQL (Recommended)
Use a managed PostgreSQL service:
- **Neon** (Free tier available): https://neon.tech
- **Supabase** (Free tier): https://supabase.com
- **Railway**: https://railway.app
- **Render**: https://render.com

## Step 3: Environment Configuration

Create a `.env` file in the root directory:

```bash
# Database Configuration
DATABASE_URL=postgresql://username:password@host:port/database

# Application Configuration
NODE_ENV=production
PORT=3000

# Optional: If using external database
PGHOST=your-db-host
PGDATABASE=your-db-name
PGUSER=your-username
PGPASSWORD=your-password
PGPORT=5432
```

## Step 4: Upload Files

### Via SSH (Recommended):
```bash
# Upload files using scp or rsync
scp -r ./finance-tracker-pro/ user@yourserver:/path/to/domain/
```

### Via cPanel File Manager:
1. Compress project into a ZIP file
2. Upload ZIP to your domain's public folder
3. Extract files using File Manager

## Step 5: Install Dependencies

```bash
# Navigate to project directory
cd /path/to/your/domain/

# Install Node.js dependencies
npm install

# Build the application
npm run build
```

## Step 6: Database Migration

```bash
# Push database schema (creates tables)
npm run db:push
```

## Step 7: Application Configuration

### Update server/index.ts for production:
- Ensure port binding uses `process.env.PORT || 3000`
- Confirm static file serving for built frontend
- Verify CORS settings for your domain

### Domain Configuration:
1. Point `finance.tirtomarwi.com` to your server IP
2. Configure Apache/Nginx to proxy Node.js app
3. Set up SSL certificate (Let's Encrypt recommended)

## Step 8: Process Management

### Option A: PM2 (Recommended)
```bash
# Install PM2 globally
npm install -g pm2

# Start application
pm2 start npm --name "finance-tracker" -- start

# Save PM2 configuration
pm2 save
pm2 startup
```

### Option B: Node.js directly
```bash
# Start application
npm start
```

## Step 9: Web Server Configuration

### Apache (.htaccess in domain root):
```apache
RewriteEngine On
RewriteRule ^api/(.*)$ http://localhost:3000/api/$1 [P,L]
RewriteRule ^(?!api).*$ http://localhost:3000/$1 [P,L]
```

### Nginx (in server block):
```nginx
location / {
    proxy_pass http://localhost:3000;
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection 'upgrade';
    proxy_set_header Host $host;
    proxy_cache_bypass $http_upgrade;
}
```

## Step 10: SSL Certificate

### Using Let's Encrypt:
```bash
# Install certbot
sudo apt install certbot

# Generate certificate
sudo certbot --apache -d finance.tirtomarwi.com
```

## Troubleshooting

### Common Issues:

1. **Port conflicts**: Change PORT in .env file
2. **Database connection**: Verify DATABASE_URL format
3. **Permission errors**: Check file ownership and permissions
4. **Build failures**: Ensure all dependencies installed

### Log Locations:
- Application logs: PM2 logs or `npm start` output
- Web server logs: Apache/Nginx error logs
- Database logs: PostgreSQL logs

## Maintenance

### Updates:
```bash
# Pull latest changes
git pull origin main  # if using git

# Rebuild application
npm run build

# Restart application
pm2 restart finance-tracker
```

### Backups:
- Regular database backups
- File system backups
- Environment file backup

## Support

If you encounter issues:
1. Check application logs
2. Verify database connectivity
3. Confirm web server configuration
4. Test API endpoints manually

Your Finance Tracker Pro should now be accessible at https://finance.tirtomarwi.com