> For the complete documentation index, see [llms.txt](https://docs.drakodevelopment.net/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.drakodevelopment.net/drako-tickets/getting-started/nginx-configuration.md).

# Nginx Configuration

{% hint style="info" %}
This guide uses `tickets.example.com` as the example domain and `/var/www/drako-tickets` as the installation directory. Replace both values where necessary.
{% endhint %}

## Prerequisites

Before continuing, make sure that:

* Drako Tickets has been uploaded to an Ubuntu or Debian server.
* You have a user with `sudo` access.
* Ports `80` and `443` are open in your server or hosting-provider firewall.
* You know the directory that contains `package.json`.

Check the application directory before using the commands in this guide:

```bash
cd /var/www
ls
```

Your directory may have a different name. Update `/var/www/drako-tickets` in the commands below if needed.

{% stepper %}
{% step %}

## Create the DNS Record

Create an **A record** with your DNS provider:

| Type | Host      | Value         |
| ---- | --------- | ------------- |
| A    | `tickets` | `<SERVER_IP>` |

This example makes Drako Tickets available at `tickets.example.com`.

If you use Cloudflare, set the record to **DNS only** until the SSL certificate has been issued. Remove any incorrect **AAAA record** unless your server is also configured for IPv6.

DNS changes can take time to propagate. Confirm that the domain resolves to your server before requesting an SSL certificate:

```bash
getent hosts tickets.example.com
```

{% endstep %}

{% step %}

## Install Nginx

```bash
sudo apt update
sudo apt install nginx curl -y
sudo systemctl enable --now nginx
```

If UFW is enabled, allow web traffic:

```bash
sudo ufw allow 'Nginx Full'
```

{% endstep %}

{% step %}

## Install Drako Tickets Dependencies

Change to the directory that contains the Drako Tickets `package.json` file:

```bash
cd /var/www/drako-tickets
npm c
```

If the application has not been configured yet, create its environment file:

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

Keep your existing licence, Supabase, database, and Discord values. Set the public application address as follows:

```dotenv
APP_PROTOCOL=https
APP_HOST=tickets.example.com
APP_PORT=443
```

`APP_HOST` must contain only the hostname. Do not include `http://`, `https://`, a path, or a trailing slash.

Press `CTRL + X`, then `Y`, then `Enter` to save the file.
{% endstep %}

{% step %}

## Run Drako Tickets with PM2

Install PM2 and start the application:

```bash
npm install -g pm2
cd /var/www/drako-tickets
pm2 start npm --name "drako-tickets" -- start
```

The production start command builds Drako Tickets and serves it on port `3000`. If `DISCORD_BOT_TOKEN` is present in `.env`, the same command also starts the Discord bot.

Wait for the build to finish, then check the service and test it locally:

```bash
pm2 status
pm2 logs drako-tickets --lines 100
curl -I http://127.0.0.1:3000
```

Press `CTRL + C` to exit the logs. A response from `curl` confirms that the application is listening; a redirect response is also normal.

Save the PM2 process list and configure it to start after a reboot:

```bash
pm2 save
pm2 startup
```

`pm2 startup` prints one additional command beginning with `sudo`. Copy and run that exact command, then run `pm2 save` once more.
{% endstep %}

{% step %}

## Create the Nginx Configuration

Create a site configuration for your domain:

```bash
sudo nano /etc/nginx/sites-available/tickets.example.com
```

Add the following configuration:

```nginx
server {
    listen 80;
    listen [::]:80;

    server_name tickets.example.com;

    client_max_body_size 12M;

    location / {
        proxy_pass http://127.0.0.1:3000;
        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;
        proxy_set_header X-Forwarded-Host $host;

        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
        proxy_cache_bypass $http_upgrade;
    }
}
```

Drako Tickets already supplies its application security headers, so they do not need to be duplicated in Nginx.

Press `CTRL + X`, then `Y`, then `Enter` to save the file.
{% endstep %}

{% step %}

## Enable the Site

Enable the configuration and test it before reloading Nginx:

```bash
sudo ln -s /etc/nginx/sites-available/tickets.example.com /etc/nginx/sites-enabled/tickets.example.com
sudo nginx -t
sudo systemctl reload nginx
```

Do not reload Nginx if `sudo nginx -t` reports an error. Correct the file first and test it again.

Open `http://tickets.example.com` in a browser to confirm that Nginx can reach Drako Tickets.
{% endstep %}

{% step %}

## Install the SSL Certificate

Install Certbot using the officially recommended snap package:

```bash
sudo apt install snapd -y
sudo snap install --classic certbot
sudo ln -sf /snap/bin/certbot /usr/local/bin/certbot
```

Request the certificate and let Certbot update the Nginx configuration:

```bash
sudo certbot --nginx -d tickets.example.com
```

When prompted, enable the redirect from HTTP to HTTPS. Then test automatic certificate renewal:

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

{% endstep %}

{% step %}

## Verify the Installation

Open the following address in your browser:

```
https://tickets.example.com
```

You can also check each service from the command line:

```bash
pm2 status
sudo systemctl status nginx --no-pager
curl -I https://tickets.example.com
```

Your Drako Tickets installation should now be available securely on your domain.
{% endstep %}

{% step %}

## Updating Drako Tickets

After replacing the application files with a newer release, reinstall the exact dependencies and restart the PM2 service:

```bash
cd /var/www/drako-tickets
npm ci
pm2 restart drako-tickets --update-env
pm2 save
```

The restart runs the production build before bringing the application back online.
{% endstep %}
{% endstepper %}

## Troubleshooting

### Nginx shows `502 Bad Gateway`

Confirm that Drako Tickets is running and listening on port `3000`:

```bash
pm2 status
pm2 logs drako-tickets --lines 100
curl -I http://127.0.0.1:3000
```

### The domain does not open

Check that the DNS record points to the correct public server IP and that ports `80` and `443` are open.

### Nginx configuration test fails

Inspect the error and verify the site configuration:

```bash
sudo nginx -t
sudo nano /etc/nginx/sites-available/tickets.example.com
```

### Emails, OAuth callbacks, or Discord links use the wrong URL

Confirm these values in `/var/www/drako-tickets/.env`, then restart the service:

```dotenv
APP_PROTOCOL=https
APP_HOST=tickets.example.com
APP_PORT=443
```

```bash
pm2 restart drako-tickets --update-env
```

### View application or Nginx logs

```bash
pm2 logs drako-tickets
sudo tail -f /var/log/nginx/error.log
```

## Useful References

* [NVM installation guide](https://github.com/nvm-sh/nvm#installing-and-updating)
* [Certbot instructions for Nginx](https://certbot.eff.org/instructions?ws=nginx\&os=snap)
