# 🚀 How to Deploy a Django Project on cPanel (Step-by-Step Guide)

Deploying a Django project on **cPanel** is a great way to get your app online without needing a VPS or cloud server. Whether your code is hosted on **GitHub** or you have the files on your local machine, this step-by-step guide will help you deploy it easily.

---

## 🧰 Prerequisites

Before starting, make sure you have:

* ✅ A working Django project
    
* ✅ cPanel account with Python App support
    
* ✅ Domain or subdomain ready
    
* ✅ `requirements.txt` file in your project
    
* ✅ Correct `ALLOWED_HOSTS` & production settings
    

---

## ⚙️ Step 1: Prepare Your Django Project

Before uploading or cloning:

1. Collect static files:
    
    ```bash
    python manage.py collectstatic
    ```
    
2. Update `ALLOWED_HOSTS` in [`settings.py`](http://settings.py):
    
    ```bash
    ALLOWED_HOSTS = ['yourdomain.com', 'www.yourdomain.com']
    ```
    
3. Add CSRF trusted origin:
    
    ```bash
    CSRF_TRUSTED_ORIGINS = ['https://yourdomain.com']
    ```
    
4. Set `DEBUG = False` in production.
    

---

## 📁 Step 2A: Method 1 – **Upload Project Files Directly**

1. Zip your project folder (without `venv`).
    
2. Log in to **cPanel**.
    
3. Open **File Manager** → navigate to the folder where you want to host (e.g., `public_html` or subdomain).
    
4. Upload the zip file.
    
5. Extract it inside a clean folder like `myproject`.
    

👉 Tip: Keep project files in a separate folder — don’t mix with other website files.

---

## 📥 Step 2B: Method 2 – **Clone Project from GitHub**

If your project is on GitHub:

1. Open **cPanel Terminal** or **SSH Access**.
    
2. Navigate to your desired directory (e.g., `public_html`):
    
    ```bash
    cd public_html
    ```
    
3. Clone the repo:
    
    ```bash
    git clone https://github.com/yourusername/yourproject.git
    ```
    
4. Rename the folder if needed:
    
    ```bash
    mv yourproject myproject
    ```
    

✅ You can also use **Git™ Version Control** from the cPanel dashboard if terminal access isn’t available.

---

## 🌐 Step 3: Create Python App in cPanel

1. Open **“Setup Python App”** in cPanel.
    
2. Click **Create Application**.
    
3. Choose:
    
    * Python version: e.g., `3.10`
        
    * Application Root: `myproject`
        
    * Application URL: select your domain/subdomain
        
    * Application startup file: `passenger_`[`wsgi.py`](http://wsgi.py)
        
4. Create the app.
    

This will automatically set up a **virtual environment** for your project.

---

## 🧪 Step 4: Install Dependencies

Open the **Terminal** from cPanel or click “**Enter to virtual environment**” on the Python App page.

Install requirements:

```bash
pip install -r requirements.txt
```

If you don’t have `requirements.txt`:

```bash
pip freeze > requirements.txt
```

Then re-upload it or push it to GitHub and install again.

---

## 📝 Step 5: Configure `passenger_`[`wsgi.py`](http://wsgi.py)

Inside your project root (e.g., `myproject`), create or edit `passenger_`[`wsgi.py`](http://wsgi.py):

```bash
import os
import sys

# Add project path
sys.path.insert(0, os.path.dirname(__file__))

os.environ['DJANGO_SETTINGS_MODULE'] = 'myproject.settings'

from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()
```

👉 Replace `myproject.settings` with the actual path of your settings file.

---

## 🧭 Step 6: Collect Static Files on Server

```bash
python manage.py collectstatic
```

Make sure your [`settings.py`](http://settings.py) contains:

```bash
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
```

This will create a static folder to serve your assets.

---

## 🛢 Step 7: (Optional) Configure Database

If you’re using MySQL from cPanel:

1. Create a database and user in **MySQL® Databases**.
    
2. Update [`settings.py`](http://settings.py):
    

```bash
DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'yourdbname',
        'USER': 'yourdbuser',
        'PASSWORD': 'yourdbpassword',
        'HOST': 'localhost',
        'PORT': '3306',
    }
}
```

3. Run migrations:
    

```bash
python manage.py migrate
```

---

## 🔁 Step 8: Restart the Python App

* Go to **“Setup Python App”** again.
    
* Click **Restart App**.
    
* Wait a few seconds, then open your domain.
    

✅ Your Django project is now live!

---

## 🛠 Common Issues & Fixes

| Problem | Reason | Solution |
| --- | --- | --- |
| 500 Internal Server Error | Wrong WSGI path or missing package | Check `passenger_`[`wsgi.py`](http://wsgi.py) and reinstall requirements |
| Static files not loading | Static not collected | Run `collectstatic` and ensure `STATIC_ROOT` is correct |
| Database connection failed | Wrong credentials | Recheck username, password, host, and database name |
| Changes not appearing | App not restarted | Click **Restart App** after every change |

---

## 🌟 Extra Tips

* ✅ Use `.env` file or environment variables for sensitive data.
    
* 🛡 Set correct file permissions (755 for folders, 644 for files).
    
* 🔐 Always set `DEBUG = False` in production.
    
* 🌍 Add [`www.yourdomain.com`](http://www.yourdomain.com) and [`yourdomain.com`](http://yourdomain.com) to `ALLOWED_HOSTS`.
    
* 🧰 Use Git pull to update your project easily when using GitHub.
    

---

## 📝 Example File Structure

```bash
public_html/
   └── myproject/
       ├── manage.py
       ├── myproject/
       │   ├── settings.py
       │   ├── urls.py
       │   └── wsgi.py
       ├── passenger_wsgi.py
       ├── requirements.txt
       └── static/
```

---

## 🏁 Conclusion

Deploying a Django project on **cPanel** is simple once you know the steps.  
You can either:

* 📂 **Upload** your project manually, or
    
* 🪝 **Clone** it directly from GitHub for easier version control.
    

With Python App support, cPanel takes care of most server configurations for you. Just make sure your WSGI file, dependencies, and settings are correct — and your Django app will be online in no time. 🚀

---

✅ **Pro Tip:** If you plan to maintain and update your project frequently, using **GitHub cloning** with `git pull` is much faster and cleaner than manually uploading files every time.
