# Deploy to Linode (Linux) — Nginx + PHP-FPM

This project is a plain PHP app (no build step). You deploy it by putting the `public/` directory behind Nginx and configuring PHP-FPM.

## Assumptions

- A Linode Nanode running an **apt-based** distro (Ubuntu or Debian).
- You have a domain name pointing at the Linode.
- You want clean URLs like `/books` (handled by Nginx `try_files`).

If you’re on a non-apt distro (Alpine/CentOS), the structure is the same but package/service names differ.

---

## 1) One-time server setup

### 1.1 Install packages

Ubuntu/Debian:

```bash
sudo apt update
sudo apt -y install nginx php-fpm php-cli
```

(Optional, recommended for SSL):

```bash
sudo apt -y install certbot python3-certbot-nginx
```

### 1.2 Allow HTTP/HTTPS

If you use `ufw`:

```bash
sudo ufw allow OpenSSH
sudo ufw allow 'Nginx Full'
sudo ufw enable
sudo ufw status
```

---

## 2) Deploy the app

### 2.1 Create a folder

Pick a consistent layout. Example:

```bash
sudo mkdir -p /var/www/author-site
sudo chown -R $USER:$USER /var/www/author-site
```

### 2.2 Put the code on the server

If this repo is on GitHub:

```bash
cd /var/www/author-site
git clone <YOUR_REPO_URL> current
```

If you are copying files manually instead, copy the whole `web/` folder contents into `/var/www/author-site/current/`.

### 2.3 Configure author content

Edit:
- `storage/site.json`
- `storage/content/*.json`

Example:

```bash
cd /var/www/author-site/current
nano storage/site.json
```

---

## 3) Configure Nginx

Create a server block (replace `example.com`):

```bash
sudo nano /etc/nginx/sites-available/author-site
```

Paste:

```nginx
server {
    listen 80;
    server_name example.com www.example.com;

    root /var/www/author-site/current/public;
    index index.php;

    # Clean URLs: route everything through index.php when not a real file/dir
    location / {
        try_files $uri $uri/ /index.php?$query_string;
    }

    location ~ \\.php$ {
        include snippets/fastcgi-php.conf;

        # IMPORTANT: set this to the socket that exists on your server.
        # Common examples:
        # - /run/php/php8.2-fpm.sock
        # - /run/php/php8.3-fpm.sock
        fastcgi_pass unix:/run/php/php8.2-fpm.sock;
    }

    location ~ /\. {
        deny all;
    }
}
```

Enable it:

```bash
sudo ln -sf /etc/nginx/sites-available/author-site /etc/nginx/sites-enabled/author-site
sudo nginx -t
sudo systemctl reload nginx
```

### 3.1 Fix the PHP-FPM socket if needed

If `nginx -t` passes but PHP pages download or 502, your socket path likely differs.

Find the socket:

```bash
ls -la /run/php/
```

Then update `fastcgi_pass` accordingly, and reload:

```bash
sudo nginx -t
sudo systemctl reload nginx
```

---

## 4) Add HTTPS (Let’s Encrypt)

Once the HTTP site is working:

```bash
sudo certbot --nginx -d example.com -d www.example.com
```

Certbot will edit your Nginx config and set up renewal.

If you are using Apache instead of Nginx, use ACME via:

```bash
sudo certbot --apache -d example.com -d www.example.com
```

Standard certificate files issued by Let’s Encrypt are:

```text
/etc/letsencrypt/live/example.com/fullchain.pem
/etc/letsencrypt/live/example.com/privkey.pem
```

---

## 4.1 Admin-Managed Domains and Aliases

The Admin Dashboard now includes **Domains, HTTPS, and Alias Routing**.

Use it to configure:

- `Primary Domain` (maps to server `ServerName`)
- Alias rows like `book1.example.com => /books/my-book` (maps to `ServerAlias` plus landing-path override)
- HTTPS enforcement (app-level redirect to `https://`)
- Canonical mode:
    - `Always use primary domain`
    - `Keep original request domain`

Important behavior:

- When a request arrives on an alias domain at `/`, the app internally routes it to the alias target path.
- All canonical tags and OpenGraph URLs are generated from the canonical mode you select.
- If HTTPS force is enabled, all `http://` requests are redirected to `https://` before route handling.

---

## 4.2 Self-Signed Certificate for All Configured Domains

The dashboard shows a ready-to-run command. The equivalent manual flow is:

```bash
sudo mkdir -p /etc/ssl/lizardman-pages
sudo openssl req -x509 -nodes -newkey rsa:4096 -sha256 -days 825 \
    -keyout /etc/ssl/lizardman-pages/lizardman-pages.key \
    -out /etc/ssl/lizardman-pages/lizardman-pages.crt \
    -subj "/CN=example.com" \
    -addext "subjectAltName=DNS:example.com,DNS:www.example.com,DNS:book1.example.com"
```

Use the exact domain list shown in the dashboard output so SAN names match your aliases.

---

## 4.3 Nginx Server Names (Alias Hosts)

Ensure your vhost includes every host:

```nginx
server_name example.com www.example.com book1.example.com;
```

If using self-signed certs in Nginx TLS blocks, point to the generated cert/key:

```nginx
ssl_certificate /etc/ssl/lizardman-pages/lizardman-pages.crt;
ssl_certificate_key /etc/ssl/lizardman-pages/lizardman-pages.key;
```

Then reload Nginx:

```bash
sudo nginx -t
sudo systemctl reload nginx
```

---

## 5) Updates

If you deployed via git:

```bash
cd /var/www/author-site/current
git pull
sudo systemctl reload nginx
```

No DB migrations needed for this MVP.

---

## 6) Rolling out to another author (repeatable)

You have two common options:

### Option A: One Linode, multiple domains

- Copy the folder:

```bash
sudo mkdir -p /var/www/author2
sudo chown -R $USER:$USER /var/www/author2
cp -a /var/www/author-site/current /var/www/author2/current
```

- Edit `/var/www/author2/current/storage/site.json` + content files.
- Create a second Nginx server block pointing `root` at `/var/www/author2/current/public`.
- Run certbot for the new domain.

### Option B: One Linode per author

Same steps, but each server has a single site.

---

## Notes

- The file `public/router.php` is only for the built-in PHP dev server; Nginx uses `try_files` to route requests.
- If you later add a database/admin panel, you’ll add environment variables (e.g., `.env`) and PHP extensions (pdo_mysql, etc.).
