# Production deployment: security & file permissions

Use this checklist when moving **LucknowOne** (Laravel) to a production or hardened UAT server.

## Quick incident runbook: login loop

Symptom: user logs in (OTP/password), gets redirected to dashboard, then lands back on `/login`.

1. Confirm current env values: `APP_URL`, `SESSION_DRIVER`, `SESSION_DOMAIN`, `SESSION_SECURE_COOKIE`, `SESSION_SAME_SITE`.
2. For single-node deployments, set `SESSION_DRIVER=file` first and re-test.
3. If using load balancer / multiple nodes, verify same `APP_KEY` everywhere and shared session backend (`redis`/`database`) is healthy.
4. Ensure HTTPS/proxy forwarding is correct (`X-Forwarded-Proto`) and trusted proxies are configured.
5. Clear runtime caches after any env/config change:
   ```bash
   php artisan optimize:clear
   ```
6. Re-test login flow in a new private/incognito window.
7. If still failing, capture request sequence + session cookie presence for `/login/verify-otp` and next `/dashboard` request.

## Environment (`.env`)

| Variable | Production recommendation |
|----------|---------------------------|
| `APP_ENV` | `production` |
| `APP_DEBUG` | `false` |
| `APP_URL` | `https://your-domain.com` (HTTPS) |
| `APP_KEY` | Set (`php artisan key:generate`); never commit real keys |
| `ALLOW_UAT_DEFAULT_PASSWORD` | `false` (ignored when `APP_ENV=production` anyway) |
| `UAT_DEFAULT_PASSWORD` | N/A in production |
| `LOG_OTP_CODES` | `false` — OTPs are never written to logs in production (`config/auth.php`) |
| `SESSION_DRIVER` | Prefer `file` unless you have a validated shared session backend (`redis`/`database`) |
| `SESSION_SECURE_COOKIE` | `true` when the site is HTTPS-only |
| `SANCTUM_STATEFUL_DOMAINS` | Your real frontend/API domains (comma-separated) |
| Database credentials | Strong passwords; restricted DB user privileges |

After changing `.env`, run:

```bash
php artisan config:cache
php artisan route:cache
php artisan view:cache
```

## HTTPS

The app calls `URL::forceScheme('https')` when `APP_ENV` is `production`. Terminate TLS at your load balancer or web server and forward `X-Forwarded-Proto` / `X-Forwarded-For` if needed. Configure **trusted proxies** (Laravel `TrustProxies` / `bootstrap/app.php`) so IP-based rate limits and logs stay correct.

## Session stability (important)

If users can log in successfully but are redirected back to `/login` on the next request, treat this as a **session persistence** issue first.

Recommended order:

1. Keep `SESSION_DRIVER=file` for single-node deployments.
2. Only move to `database` / `redis` when all app nodes share the same backend and same `APP_KEY`.
3. Ensure `APP_URL`, `SESSION_DOMAIN`, `SESSION_SECURE_COOKIE=true`, and proxy HTTPS forwarding are consistent.
4. After any session-related change, run:

```bash
php artisan optimize:clear
```

## Rate limiting (built-in)

Rough limits per IP (or user where noted):

- **OTP send** (`/login/send-otp`, password-reset OTP): 10/minute  
- **OTP verify**: 30/minute  
- **Password login**: 15/minute (plus in-controller lockout after failures)  
- **Public lead forms** (contact, education, service leads): 20/minute  
- **Listing interest** POST: 30/minute (keyed by user id or IP)  
- **API** `/api/v1/*`: 120/minute  

Tune in `AppServiceProvider::configureRateLimiting()` if needed.

## File system permissions (Linux)

Typical layout: project owned by deploy user; web server user (e.g. `www-data`) needs read + write only where required.

```bash
# From project root (adjust user/group for your host)
sudo chown -R deploy:www-data /var/www/lucknowone
sudo find /var/www/lucknowone -type f -exec chmod 644 {} \;
sudo find /var/www/lucknowone -type d -exec chmod 755 {} \;

# Writable by web server
sudo chown -R www-data:www-data /var/www/lucknowone/storage /var/www/lucknowone/bootstrap/cache
sudo chmod -R ug+rwx /var/www/lucknowone/storage /var/www/lucknowone/bootstrap/cache

# Lock down sensitive files
sudo chmod 600 /var/www/lucknowone/.env
```

**Never** make the whole project `777`. Do not serve `.env`, `storage/logs`, or `vendor` as static files from the document root.

## Web server document root

Point the vhost **document root** to `public/`, not the repository root.

## Secrets & storage

- **Fast2SMS / admin JSON settings** live under `storage/app/` — keep disk permissions tight and backups encrypted.  
- Rotate API keys if they are ever exposed.  
- Review `storage/logs/laravel.log` permissions; restrict read access to operators only.

## Application security features already in place

- CSRF protection on web forms.  
- Admin routes: `auth`, `admin.role`, granular `admin.can` permissions.  
- API admin routes: `auth:sanctum` + `admin.api`.  
- Listing updates: ownership / role checks in API and customer property controller.  
- Logo static file route: allowlist of filenames only.  
- UAT shared password: **disabled** when `APP_ENV=production` (config + runtime check).

## Ongoing practices

- Keep Laravel and Composer dependencies updated (`composer update` in a controlled process).  
- Monitor failed logins and OTP abuse via logs and rate-limit responses.  
- Use a WAF or reverse-proxy rules for DDoS and bot traffic if exposed to the public internet.
