# Installation

**Prerequisites**

* VPS with Ubuntu 20.04 or higher
* Node.js 18.x
* MongoDB 4.4
* Domain pointed to your server

#### Installation Guide

**1. Initial Server Setup**

Update your system packages to ensure you have the latest security updates:

```sh
sudo apt update && sudo apt upgrade -y
```

**2. Install Node.js**

Install Node.js 18.x and build essentials:

```sh
curl -fsSL https://deb.nodesource.com/setup_18.x | sudo -E bash -
sudo apt install -y nodejs
sudo apt install -y build-essential
```

**3. Setup MongoDB**

* Create an account at [MongoDB Atlas](https://www.mongodb.com/atlas)
* Create a new cluster
* Get your connection string
* Replace the `MONGODB_URI` in your `.env` file with the Atlas connection string

**4. Install and Configure Nginx**

Set up Nginx as a reverse proxy:

```sh
sudo apt install -y nginx
sudo systemctl enable nginx
sudo systemctl start nginx
```

**5. Application Setup**

Clone and configure the application:

```sh
mkdir -p /var/www/pastify
cd /var/www
```

Add the zip file contents to the directory `/var/www/pastify`

Create a `.env` file with the following values:

```sh
MONGODB_URI=MONGODB_ATLAS_URL_HERE
PORT=3000
ADMIN_USERNAME=your_admin_username
ADMIN_PASSWORD=your_secure_password
SESSION_SECRET=your_random_session_secret
```

**6. Create Nginx Configuration**

Create a new Nginx configuration file:

```sh
sudo nano /etc/nginx/sites-available/pastify
```

Add the following configuration:

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

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

Enable the site and restart Nginx:

```sh
sudo ln -s /etc/nginx/sites-available/pastify /etc/nginx/sites-enabled/
sudo nginx -t
sudo systemctl restart nginx
```

**7. Install PM2 Globally**

```sh
sudo npm install -g pm2
```

**8. Start the Application**

```sh
cd /var/www/pastify
pm2 start server/server.js --name "pastify"
```

**9. Enable PM2 Startup Script**

```sh
pm2 startup
pm2 save
```
