# Deploying `mkblog` on a Remote Linux Host with nginx

`mkblog` is a static site generator. In production, nginx should serve the generated `public/` directory directly.

These instructions assume:

- You build the site on this machine.
- The production nginx host is a different Linux machine.
- Your domain and TLS are already configured on that remote host.
- You can SSH to the remote host.

## 1. Set Production Values

Before building, update `config.yaml` with your real public values.

Example:

```yaml
title: "/var/log"
hostname: "example.com"
baseURL: "https://example.com"
theme: "default"
description: "A blog about UNIX and programming."
author: "Your Name"
```

Notes:

- `hostname` is rendered in the templates, so it should match the real public hostname.
- `baseURL` should be the real `https://...` URL used in production.

## 2. Build the Site Locally

From the repo root:

```bash
./mkblog build
```

Or, if you prefer building from source:

```bash
go run ./cmd/mkblog build
```

This writes the static site to `public/`.

## 3. Copy the Built Site to the Remote Host

A simple publish target is:

```text
/var/www/mkblog/current/
```

Create it on the remote host if needed:

```bash
ssh user@example.com 'sudo mkdir -p /var/www/mkblog/current && sudo chown -R $USER:$USER /var/www/mkblog'
```

Then sync the local build output:

```bash
rsync -av --delete public/ user@example.com:/var/www/mkblog/current/
```

`--delete` keeps the remote directory aligned with the current local build.

nginx should serve those copied files directly. Do not run `mkblog serve` in production.

## 4. Configure nginx on the Remote Linux Host

Point nginx at the copied site directory and let it serve the generated static files.

Example HTTPS server block:

```nginx
server {
    listen 443 ssl http2;
    server_name example.com;

    root /var/www/mkblog/current;
    index index.html;

    location / {
        try_files $uri $uri/ =404;
    }

    error_page 404 /404.html;

    location = /404.html {
        internal;
    }

    # Your existing TLS directives stay here.
    # ssl_certificate ...
    # ssl_certificate_key ...
}
```

Why this works:

- `try_files $uri $uri/ =404;` serves real files, supports directory-style URLs like `/hello-world/`, and returns a real 404 when nothing matches.
- `error_page 404 /404.html;` tells nginx to show the generated `public/404.html` page for missing routes.
- `location = /404.html { internal; }` keeps `/404.html` from being browsed directly as a normal public page.

If you also terminate plain HTTP on nginx, add a redirect block:

```nginx
server {
    listen 80;
    server_name example.com;
    return 301 https://$host$request_uri;
}
```

## 5. Validate and Reload nginx

On the remote host:

```bash
sudo nginx -t
sudo systemctl reload nginx
```

If your system does not use `systemd`, reload nginx with the service manager used on that host.

## 6. Verify the Deployment

Check that valid pages and assets return `200`:

```bash
curl -I https://example.com/
curl -I https://example.com/hello-world/
curl -I https://example.com/assets/theme/style.css
```

Check that a missing page returns a real `404`:

```bash
curl -I https://example.com/does-not-exist/
```

Expected result:

- `/` returns `200`
- `/hello-world/` returns `200`
- `/assets/theme/style.css` returns `200`
- `/does-not-exist/` returns `404`

To confirm nginx is serving the custom page rather than its default error page, fetch the body:

```bash
curl https://example.com/does-not-exist/
```

That response should render the `mkblog` 404 page generated from `public/404.html`.

If you keep `location = /404.html { internal; }`, direct requests to `/404.html` will not behave like a normal public page. That is intentional.

## 7. Updating the Site Later

For each content update:

1. Rebuild locally.
2. Sync `public/` to the remote host with `rsync`.
3. Reload nginx only if you changed nginx configuration.

Typical update command:

```bash
./mkblog build && rsync -av --delete public/ user@example.com:/var/www/mkblog/current/
```
