# Server Environment

## Scope

This document records the live environment for `llm.potthast.ruhr` as it exists on the remote Plesk server.

Goal:

- document the relevant LLM runtime facts
- make a local minimal reproduction possible
- keep live server facts separate from the local Docker setup

## Host And Deployment

- SSH target: `root@potthast.ruhr`
- Plesk vhost path: `/var/www/vhosts/potthast.ruhr/llm.potthast.ruhr`
- server host name reported by SSH: `tender-hellman.194-164-192-150.plesk.page`
- web root contains the application directly, not a nested build output

## Runtime

- PHP version used by the vhost: `8.4`
- per-vhost PHP config: `/var/www/vhosts/system/llm.potthast.ruhr/etc/php.ini`
- PHP session storage: `/var/lib/php/sessions`
- `open_basedir`: `/var/www/vhosts/potthast.ruhr/:/tmp/`

Relevant PHP settings from the live vhost:

- `memory_limit = 4G`
- `max_execution_time = 1200`
- `max_input_time = 1200`
- `post_max_size = 8M`
- `upload_max_filesize = 2M`
- `display_errors = Off`
- `log_errors = On`
- `session.use_cookies = 1`
- `session.use_only_cookies = 1`

## Vhost Layout

Plesk files found for the domain:

- `/var/www/vhosts/system/llm.potthast.ruhr/conf/httpd.conf`
- `/var/www/vhosts/system/llm.potthast.ruhr/conf/nginx.conf`
- `/var/www/vhosts/system/llm.potthast.ruhr/conf/vhost.conf`
- `/var/www/vhosts/system/llm.potthast.ruhr/conf/vhost_nginx.conf`
- `/var/www/vhosts/system/llm.potthast.ruhr/conf/vhost_ssl.conf`

Observed Nginx-related tuning:

- `fastcgi_buffer_size 32k`
- `fastcgi_buffers 16 32k`
- `fastcgi_busy_buffers_size 64k`

No custom SSL vhost content was present in the inspected file.

## Application Files

Files present in the live web root:

- `api.php`
- `app.php`
- `chat.php`
- `chat_settings.php`
- `docs/index.php`
- `includes/chat_api.php`
- `includes/chat_bootstrap.php`
- `includes/chat_prompt.php`
- `includes/chat_render.php`
- `index.php`
- `login.php`
- `openapi.json`
- `partials/cards.php`
- `partials/footer.php`
- `partials/header.php`
- `partials/nav.php`
- `robots.txt`
- `settings.php`

## Database

Database name: `llm_chat`

Tables in the live database:

- `llm_users`
- `llm_chat_settings`
- `llm_chat_sessions`
- `llm_chat_messages`

### `llm_users`

Live structure:

- `id` int unsigned, auto increment, primary key
- `username` varchar(64), unique
- `password_hash` varchar(255)
- `api_key` varchar(64), unique in the app logic
- `default_model` varchar(120), default `llama3.2:1b`
- `created_at` int unsigned
- `updated_at` int unsigned

Live data snapshot:

- one user exists
- username: `markus`
- default model: `llama3.2:1b`
- API key length: `48` characters

### `llm_chat_settings`

Live structure:

- `id` int unsigned, auto increment
- `user_id` int unsigned, unique
- `api_key` varchar(64)
- `default_model` varchar(120), default `llama3.2:3b`
- `system_prompt` text
- `temperature` decimal(4,2)
- `top_p` decimal(4,2)
- `max_history_entries` int unsigned
- `max_prompt_length` int unsigned
- `assistant_name` varchar(64)
- `user_name` varchar(64)
- `debug_prompt` tinyint(1)
- `created_at` int unsigned
- `updated_at` int unsigned

Live data snapshot:

- one settings row exists
- default model: `llama3.2:3b`
- temperature: `0.70`
- top_p: `0.90`
- max history entries: `12`
- max prompt length: `12000`
- assistant name: `Assistent`
- user name: `Benutzer`
- debug prompt: enabled

### `llm_chat_sessions`

Live structure:

- `id` int unsigned, auto increment
- `user_id` int unsigned
- `session_uuid` char(32), unique
- `title` varchar(120)
- `created_at` int unsigned
- `updated_at` int unsigned
- foreign key to `llm_users(id)`
- delete cascade on user deletion

Observed count:

- `5` chat sessions

### `llm_chat_messages`

Live structure:

- `id` bigint unsigned, auto increment
- `session_id` int unsigned
- `role` enum(`user`, `assistant`, `system`)
- `content` longtext
- `created_at` int unsigned
- `debug_enabled` tinyint(1)
- `debug_prompt` longtext nullable
- `debug_request` longtext nullable
- `debug_response` longtext nullable
- `debug_meta` longtext nullable
- foreign key to `llm_chat_sessions(id)`
- delete cascade on session deletion

Observed count:

- `230` messages

## LLM Flow

### Stateless API

`api.php` is the stateless model execution endpoint.

Behavior:

- accepts only `POST`
- expects JSON body
- validates `api_key`
- requires `model`
- requires `prompt`
- accepts optional `options`
- forwards the request to Ollama
- returns only the model response, model name, and elapsed time

Important runtime behavior:

- there is no session history inside `api.php`
- context is provided by the caller
- if the prompt explicitly asks for JSON, the request switches to Ollama `format = json`

### Ollama Connection

The application uses the environment variable `OLLAMA_HOST`.

Observed fallback logic:

- if `OLLAMA_HOST` is unset, the code falls back to `http://127.0.0.1:11434`

API path used:

- `/api/chat`

### Chat Context Building

`chat.php` uses the following flow:

- authenticate via PHP session
- select or create the active chat session
- load chat settings for the current user
- load message history from the database
- build a compact prompt from:
  - system prompt
  - recent messages
  - current user prompt
- call `api.php` with:
  - `api_key`
  - `model`
  - `prompt`
  - `options.temperature`
  - `options.top_p`

Context limits in the live code:

- recent messages default to `12`
- prompt length cap default to `12000` characters

## Authentication

The live app uses PHP sessions and a single local user table.

Relevant session keys:

- `llm_auth`
- `llm_user_id`
- `llm_user`
- `llm_api_key`
- `llm_last_activity`
- `active_chat_session_uuid`

Session timeout in code:

- `8 hours`

## Models

Observed default models in the live data:

- user default: `llama3.2:1b`
- chat default: `llama3.2:3b`

Model management happens in the UI via Ollama.

## Local Minimal Reproduction

To reproduce the live setup locally, the minimal pieces are:

- PHP 8.4
- MySQL or MariaDB with the four `llm_*` tables
- Ollama reachable on `OLLAMA_HOST`
- the PHP app files from this repo
- session storage for PHP
- a web server able to serve the app root and forward `.php` to PHP-FPM

The live setup is Plesk-based, so the local reproduction should keep:

- app root at the web root
- stateless `api.php`
- separate chat context handling in `chat.php`
- database-backed authentication and session storage

## Open Items

The following are not yet fully reconstructed from live access and should be documented later if needed:

- actual Plesk subscription and service plan settings
- exact Nginx/Apache routing beyond the inspected buffer settings
- installed Ollama models and host reachability status
- current backups, cron jobs, and system-wide hardening settings
- any external firewall or reverse-proxy rules

