# Deploying The Reverse Times (cPanel)

## How the live site is set up

| What | Where |
|---|---|
| cPanel → Setup Python App | `thereversetimes.com/` · Python 3.9 · startup file `passenger_wsgi.py` · entry point `application` |
| App folder (also the website's public folder) | `/home/sazzadb1/thereversetimes.com` |
| Virtualenv | `source /home/sazzadb1/virtualenv/thereversetimes.com/3.9/bin/activate` |
| Live data — **never upload over these** | `db.sqlite3`, `media/`, `private_media/`, `.env` |
| Backups made by every update | `backups/<date-time>/` (database, uploads, previous code) |

Because the app folder is also the public web folder, `.htaccess` blocks the database, `.env`,
Python files, logs and backups. Don't remove the block marked "The Reverse Times: never serve private files".

## Updating the live site (every time)

Your users, orders, articles, uploads and settings stay exactly as they are — an update only replaces code.

1. **On your computer**, in the project folder:
   ```bash
   bash deploy/make_release.sh
   ```
   It runs the tests and builds `release/thereversetimes-<date>.zip` (code only — it refuses to pack the
   database, uploads or `.env`).
2. **cPanel → File Manager** → open `thereversetimes.com` → **Upload** the zip.
3. **cPanel → Terminal**:
   ```bash
   source /home/sazzadb1/virtualenv/thereversetimes.com/3.9/bin/activate && cd /home/sazzadb1/thereversetimes.com
   bash deploy/update_live.sh thereversetimes-<date>.zip
   ```
   It backs up the database and uploads, swaps in the new code, installs packages, updates the database
   (adds new tables/columns, never deletes your data), collects static files and restarts the site.
   If anything fails it puts the previous version back automatically.
4. Open https://thereversetimes.com, the admin, and one magazine page to check.

### Never do these on the live site
- Never upload your local `db.sqlite3`, `media/` or `.env` — that would replace live users and orders.
- Never run `manage.py flush`, `populate.py`, or delete migration files.
- Don't edit code directly on the server; change it on your computer and deploy a new release.

### Rolling back by hand
```bash
cd /home/sazzadb1/thereversetimes.com
ls backups/                                   # pick the folder from before the bad update
gunzip -c backups/<date>/db.sqlite3.gz > db.sqlite3   # only if the database must go back too
rm -rf apps reverse_times templates static locale && tar -xzf backups/<date>/code.tar.gz
touch tmp/restart.txt
```

### Working with live data on your computer
Download a copy (File Manager → `backups/<date>/db.sqlite3`) and use it locally. Never upload it back.

## The `.env` file on the server
Lives next to `manage.py`; see `.env.example` for every setting. Edit it in File Manager, then restart the
app (Setup Python App → Restart). It must contain at least `DEBUG=False`, `SECRET_KEY`, `ALLOWED_HOSTS`,
`EMAIL_HOST_PASSWORD`, `TELEGRAM_BOT_TOKEN`, `TELEGRAM_CHAT_ID`.

## Files Apache serves directly
- `/media/…` → `media/`
- `/static/…` → `staticfiles/` (filled by `collectstatic` during each update)
