# Deployment Guide for Postaci

This guide outlines how to deploy the Postaci application on a Ubuntu server.

## Prerequisites

- Ubuntu server (preferably 20.04 LTS or newer)
- Root or sudo access
- Domain name (optional but recommended for production)

## Manual Deployment Steps

### 1. Server Setup

Connect to your server:

```bash
ssh root@your-server-ip
```

Update the system:

```bash
apt update && apt upgrade -y
```

Install required packages:

```bash
# Add NodeJS repository
curl -fsSL https://deb.nodesource.com/setup_18.x | bash -

# Install dependencies
apt install -y nodejs postgresql postgresql-contrib nginx git
```

### 2. Database Setup

Start and enable PostgreSQL:

```bash
systemctl start postgresql
systemctl enable postgresql
```

Create database and user:

```bash
sudo -u postgres psql -c "CREATE USER postaci WITH PASSWORD 'your_secure_password';"
sudo -u postgres psql -c "CREATE DATABASE postaci OWNER postaci;"
sudo -u postgres psql -c "GRANT ALL PRIVILEGES ON DATABASE postaci TO postaci;"
```

### 3. Application Setup

Clone the repository:

```bash
git clone https://gitea.oezdag.io/m3mo/POSTERAPP_V1.git /opt/postaci
cd /opt/postaci
```

Install dependencies:

```bash
npm install
cd validation-service && npm install && cd ..
```

Set up environment variables:

```bash
# Create production env file
cat > .env.production << EOF
# Next Auth
NEXTAUTH_SECRET=$(openssl rand -base64 32)
NEXTAUTH_URL=http://your-server-ip

# Database
DATABASE_URL="postgresql://postaci:your_secure_password@localhost:5432/postaci"

# API Services
API_URL="http://localhost:8000/api"
EOF
```

Initialize the database:

```bash
npx prisma generate
npx prisma migrate deploy
```

Build the Next.js application:

```bash
npm run build
```

### 4. Service Setup

Create a service for the validation service:

```bash
cat > /etc/systemd/system/validation-service.service << EOF
[Unit]
Description=Address Validation Service for Postaci
After=network.target

[Service]
Type=simple
User=root
WorkingDirectory=/opt/postaci/validation-service
ExecStart=/usr/bin/node server.js
Restart=on-failure
Environment=NODE_ENV=production

[Install]
WantedBy=multi-user.target
EOF
```

Create a service for the Next.js application:

```bash
cat > /etc/systemd/system/nextjs.service << EOF
[Unit]
Description=Next.js Application for Postaci
After=network.target

[Service]
Type=simple
User=root
WorkingDirectory=/opt/postaci
ExecStart=/usr/bin/npm start
Restart=on-failure
Environment=NODE_ENV=production

[Install]
WantedBy=multi-user.target
EOF
```

Start and enable the services:

```bash
systemctl enable validation-service
systemctl start validation-service
systemctl enable nextjs
systemctl start nextjs
```

### 5. Nginx Setup

Create an Nginx configuration:

```bash
cat > /etc/nginx/sites-available/postaci << EOF
server {
    listen 80;
    server_name your-server-ip;  # Replace with your domain if available

    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;
    }
}
EOF
```

Enable the configuration:

```bash
ln -s /etc/nginx/sites-available/postaci /etc/nginx/sites-enabled/
rm /etc/nginx/sites-enabled/default
systemctl restart nginx
```

### 6. Firewall Setup

Configure the firewall:

```bash
ufw allow 22
ufw allow 80
ufw allow 443
ufw enable
```

## Troubleshooting

### Check Service Status

```bash
systemctl status nextjs
systemctl status validation-service
```

### View Logs

```bash
journalctl -u nextjs -f
journalctl -u validation-service -f
```

### Nginx Logs

```bash
tail -f /var/log/nginx/access.log
tail -f /var/log/nginx/error.log
```

## Security Recommendations

1. Set up SSL/TLS with Let's Encrypt
2. Create a non-root user for running the application
3. Regularly update dependencies with `npm audit fix`
4. Configure server firewall properly
5. Set up regular backups of the database 