# Deployment

Target: any host that runs PHP 8.2+ and MySQL 8 (XAMPP, Laragon, WAMP, MAMP, Linux LAMP, etc.). No build step, no package manager required.

## 1. Prerequisites

- PHP 8.2+ with these extensions enabled: `pdo_mysql`, `zip`, `mbstring`, `openssl`, `fileinfo`, `intl`, `curl`
- MySQL 8.0+
- Apache (or any web server that serves PHP and reads `.htaccess`)

On the dev machine the included `enable-php-extensions.bat` turns the optional extensions on for XAMPP / Laragon.

## 2. Install (Windows + XAMPP — same flow the dev box uses)

```text
1. Copy/clone the project to:   C:\Users\Admin\Desktop\Software\medmecs
2. Double-click setup.bat                     (drops + recreates database, imports schema, ingests sample data)
3. Double-click create-htdocs-link.bat        (junctions C:\xampp\htdocs\medmecs -> the project)
4. Open http://localhost/medmecs/login.php
```

## 3. Install (Linux production sketch)

```bash
# As root (one-time)
apt update && apt install -y nginx mysql-server \
    php8.2-fpm php8.2-cli php8.2-mysql php8.2-mbstring \
    php8.2-xml php8.2-zip php8.2-bcmath php8.2-gd \
    php8.2-curl php8.2-intl certbot python3-certbot-nginx

# Database
mysql -e "CREATE DATABASE medmecs_mis DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;"
mysql medmecs_mis < /var/www/medmecs/sql/schema.sql

# Web user permissions
chown -R www-data:www-data /var/www/medmecs/storage
chmod -R 775 /var/www/medmecs/storage

# Edit credentials
nano /var/www/medmecs/includes/config.php   # set DB_PASS, APP_BASE_URL, SMTP_*
```

## 4. Nginx (sketch)

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

server {
    listen 443 ssl http2;
    server_name mis.example.com;
    root /var/www/medmecs;
    index index.php;

    ssl_certificate     /etc/letsencrypt/live/mis.example.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/mis.example.com/privkey.pem;

    add_header Strict-Transport-Security "max-age=31536000; includeSubDomains" always;
    add_header X-Frame-Options DENY always;
    add_header X-Content-Type-Options nosniff always;

    client_max_body_size 110M;

    location ~ ^/(includes|sql|docs|bin|storage)/ { deny all; }
    location / { try_files $uri $uri/ /index.php?$query_string; }

    location ~ \.php$ {
        fastcgi_pass unix:/run/php/php8.2-fpm.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
        include fastcgi_params;
    }
}
```

```bash
certbot --nginx -d mis.example.com
```

Apache + the bundled `.htaccess` files give equivalent protection — no extra config needed in XAMPP.

## 5. Scheduled reports (cron)

Add this line to crontab (Linux) or Windows Task Scheduler (Windows):

```text
* * * * * www-data /usr/bin/php /var/www/medmecs/bin/run-schedules.php >> /var/log/medmecs-sched.log 2>&1
```

Windows Task Scheduler:

```text
"C:\xampp\php\php.exe" "C:\Users\Admin\Desktop\Software\medmecs\bin\run-schedules.php"
```

The runner reads `scheduled_reports`, evaluates each cron expression, and sends matching ones via PHP's `mail()` function (configure SMTP in `php.ini` or replace with PHPMailer in the future).

## 6. Backups

Daily mysqldump from cron:

```text
0 2 * * * root mysqldump --single-transaction medmecs_mis | gzip > /var/backups/medmecs/medmecs_$(date +\%Y\%m\%d).sql.gz
0 3 * * * root find /var/backups/medmecs -mtime +30 -delete
```

Restore is `gunzip -c file.sql.gz | mysql medmecs_mis`.

## 7. Post-deploy smoke test

1. Open `/login.php`, sign in as `admin@medmecs.local`, change password.
2. Open the seeded Blue Central report - the dashboard should already be populated (setup.bat auto-ingests `Sample Report.xlsx`).
3. Click `Reports → Reports browser → eye icon` on the Blue Central report - 4 tabs render.
4. Open `/admin/notifications.php` - empty inbox, no errors.
5. Open `/admin/reference.php → Pages` - should list every PHP file in the project.
