# Nutrientz — Deployment Guide for nutrientz.co.ke

This guide is for setting up the Nutrientz Django e-commerce site on a hosting provider (VPS, cPanel with Python support, or a platform like DigitalOcean / Render / Railway).

---

## What your hosting provider needs to support

- **Python 3.10+**
- **pip** (Python package manager)
- Ability to run a **WSGI application** (gunicorn)
- Either **nginx** or **Apache** as a reverse proxy (most VPS providers)

> If your provider offers cPanel + Softaculous, ask specifically for **Python App** support. Most Kenyan providers (Truehost, HostPinnacle, etc.) offer this.

---

## Step-by-step setup

### 1. Upload the project files

Upload the contents of this zip to your server, e.g. into:
```
/home/yourusername/nutrientz/
```

### 2. Create a Python virtual environment

```bash
cd /home/yourusername/nutrientz
python3 -m venv venv
source venv/bin/activate
pip install -r requirements.txt
```

### 3. Set environment variables

Create a file called `.env` in the project root (next to `manage.py`), or set these in your hosting panel:

```
SECRET_KEY=replace-this-with-a-long-random-string-50-plus-chars
DEBUG=False
ALLOWED_HOSTS=nutrientz.co.ke,www.nutrientz.co.ke
```

> **Generate a SECRET_KEY** by running:
> `python -c "from django.core.management.utils import get_random_secret_key; print(get_random_secret_key())"`

To make Django read `.env`, add this at the top of `store_project/settings.py` (after the imports):

```python
from dotenv import load_dotenv
load_dotenv()
```

And add `python-dotenv` to `requirements.txt`. Or simply export the variables directly in your server shell.

### 4. Run database migrations

```bash
cd /home/yourusername/nutrientz/"E-Commerce Website"
source ../venv/bin/activate
python manage.py migrate
```

### 5. Create a superuser (admin account)

```bash
python manage.py createsuperuser
```

Follow the prompts. This gives your client access to `/admin/` to manage products.

### 6. Collect static files

```bash
python manage.py collectstatic --noinput
```

This copies all CSS/JS into the `staticfiles/` folder, which whitenoise serves automatically.

### 7. Start the application with gunicorn

```bash
gunicorn store_project.wsgi:application --bind 0.0.0.0:8000 --workers 3
```

### 8. Configure nginx (recommended)

Ask your provider to point `nutrientz.co.ke` to port 8000, or configure nginx:

```nginx
server {
    listen 80;
    server_name nutrientz.co.ke www.nutrientz.co.ke;

    location /static/ {
        alias /home/yourusername/nutrientz/E-Commerce Website/staticfiles/;
    }

    location /media/ {
        alias /home/yourusername/nutrientz/E-Commerce Website/media/;
    }

    location / {
        proxy_pass http://127.0.0.1:8000;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
    }
}
```

### 9. Enable HTTPS (SSL)

Your domain registrar may include a free SSL certificate. Most providers support **Let's Encrypt**:

```bash
sudo certbot --nginx -d nutrientz.co.ke -d www.nutrientz.co.ke
```

---

## How the client manages the shop

The client logs in at:
> **https://nutrientz.co.ke/admin/**

From there she can:
- **Add / edit / delete products** (including uploading images)
- **Manage categories**
- **Set prices and stock levels**
- **Mark products as available or unavailable**

No coding required — everything is managed through the admin panel.

---

## Important notes

| Setting | Development | Production |
|---|---|---|
| `DEBUG` | `True` | **`False`** |
| `SECRET_KEY` | Insecure default | **Must be a new random key** |
| `ALLOWED_HOSTS` | `*` | `nutrientz.co.ke,www.nutrientz.co.ke` |
| Static files | Django dev server | Whitenoise (automatic) |
| Media files | Django dev server | nginx serves `/media/` |

---

## File structure overview

```
E-Commerce Website/
├── manage.py               ← Django CLI entry point
├── requirements.txt        ← All Python dependencies
├── db.sqlite3              ← Database (your products, accounts, etc.)
├── media/                  ← Uploaded product images
├── staticfiles/            ← Collected static files (run collectstatic)
├── store_project/          ← Project settings and root URLs
└── shop/                   ← Main app (models, views, templates, cart)
```
