# Deploy truck-me to server (e.g. /var/www/html)

These steps are for a fresh Ubuntu/Debian server where the project will live at `/var/www/html/truck-me`. Adjust paths if you use a different directory (e.g. `/var/www/html` as project root).

## 1. Copy project to server

**Option A – Clone on the server (recommended):**

```bash
cd /var/www/html
sudo git clone https://github.com/Darab-Khan-1/truck-me.git truck-me
cd truck-me
```

**Option B – From your local machine with rsync** (exclude venv and db):

```bash
rsync -avz --exclude 'venv' --exclude '__pycache__' --exclude '*.pyc' \
  --exclude 'db.sqlite3' --exclude '.git' \
  ./ root@vmi3089030:/var/www/html/truck-me/
```

## 2. On the server: install system dependencies

```bash
sudo apt update
sudo apt install -y python3 python3-venv python3-pip apache2 libapache2-mod-wsgi-py3
sudo a2enmod wsgi
```

## 3. Create virtualenv and install Python packages

**Use a venv created on the server.** Do not use a `venv/` folder committed from another machine (it will fail with "cannot execute: required file not found" because paths differ).

```bash
cd /var/www/html/truck-me
# If venv already exists but pip fails, remove it and recreate:
# deactivate 2>/dev/null; rm -rf venv
python3 -m venv venv
source venv/bin/activate
pip install -r requirements.txt
deactivate
```

## 4. Environment variables for production

```bash
cd /var/www/html/truck-me
cp deploy/.env.example .env
nano .env   # or vim
```

Set at least:

- `DJANGO_SECRET_KEY` – long random string (e.g. `python3 -c "import secrets; print(secrets.token_urlsafe(50))"`)
- `DJANGO_DEBUG=False`
- `DJANGO_ALLOWED_HOSTS` – your domain(s) and/or server IP, comma-separated (e.g. `example.com,www.example.com,123.45.67.89`)

## 5. Collect static files and permissions

```bash
cd /var/www/html/truck-me
source venv/bin/activate
python manage.py collectstatic --noinput
python manage.py migrate --noinput
deactivate

sudo chown -R www-data:www-data /var/www/html/truck-me
```

## 6. Apache virtual host

```bash
sudo cp /var/www/html/truck-me/deploy/apache-loadboard.conf /etc/apache2/sites-available/truck-me.conf

# Edit ServerName to your domain or server IP:
sudo nano /etc/apache2/sites-available/truck-me.conf

sudo a2ensite truck-me.conf

# Disable Apache default site if it conflicts:
sudo a2dissite 000-default.conf

sudo apache2ctl configtest
sudo systemctl reload apache2
sudo systemctl status apache2
```

## 7. Open firewall (if needed)

```bash
sudo ufw allow 'Apache Full'
sudo ufw enable
```

---

## If project root is /var/www/html (no `truck-me` subdir)

- Put the project files directly in `/var/www/html` (`manage.py`, `Loadboard/`, `accounts/`, `core/`, `static/`, `requirements.txt`, `deploy/`, `.env`).
- In Apache config: set `DocumentRoot /var/www/html`, static/media aliases to `/var/www/html/staticfiles` and `/var/www/html/media`, `WSGIDaemonProcess ... python-home=/var/www/html/venv python-path=/var/www/html`, and `WSGIScriptAlias / /var/www/html/Loadboard/wsgi.py`.

---

## Useful commands after deploy

- Restart app: `sudo systemctl restart apache2`
- Apache reload: `sudo systemctl reload apache2`
- Apache config test: `sudo apache2ctl configtest`
- Apache logs: `sudo tail -f /var/log/apache2/error.log`
- **Update from repo:** `cd /var/www/html/truck-me && sudo git pull && source venv/bin/activate && pip install -r requirements.txt && python manage.py collectstatic --noinput && python manage.py migrate && deactivate && sudo systemctl restart apache2`
