December 23, 2023 mblum6180_l6d6qy

Django Deployment on DreamHost

Important Notes Before You Begin

  • SQLite on DreamHost: SQLite may not work on DreamHost platforms. It’s recommended to use MySQL as your database backend for production environments.
  • MySQL Client Installation: The mysqlclient library, a requirement for connecting Django to a MySQL database, won’t install unless Python is a custom installation. Ensure you follow the steps above to install a custom version of Python.
  • Preferred Python Version: Django and Passenger tend to work best with Python 3.10. Make sure to install this version for optimal compatibility.

Setting Up MySQL Database

Follow these steps to set up a MySQL database for your Django project:

    1. Install MySQL: Ensure that MySQL is installed on your system. On most Linux distributions, you can install it using the package manager.
    2. Create a Database and User: Log in to MySQL and create a new database and user specifically for your Django project.
mysql -u root -p
CREATE USER 'mydatabaseuser'@'localhost' IDENTIFIED BY 'mypassword';
GRANT ALL PRIVILEGES ON mydatabase.* TO 'mydatabaseuser'@'localhost';

Download and Extract Python

Begin by downloading the Python package and extracting its contents.

cd ~
mkdir tmp
cd tmp
tar zxvf Python-3.10.13.tgz
cd Python-3.10.1

Compile and Install

Configure the Python source code on your system and compile it. This step prepares Python for installation.

./configure --prefix=$HOME/opt/python-3.10.13 --enable-optimizations
make install

Update PATH

Ensure your system recognizes the custom Python version by updating the PATH environment variable.

echo "export PATH=$HOME/opt/python-3.10.13/bin:$PATH" >> ~/.bash_profile
Activate Python

Activate the new Python version and confirm the installation.

source ~/.bash_profile
which python3

Install Pip and Virtualenv

Install Pip and Virtualenv to manage packages and environments.

python3 -m pip install --upgrade pip
pip3 install virtualenv

Create a Virtual Environment

Set up a virtual environment for your Python projects to isolate dependencies.

cd ~/
virtualenv -p /home/username/opt/python-3.10.13/bin/python3 venv
source venv/bin/activate

Install Django and Dependencies

Finally, install Django and any necessary dependencies like mysqlclient for your projects.

pip3 install Django
pip3 install mysqlclient

Create the Project

Start a new Django project within the virtual environment.

cd ~/
source ~/
python3 venv/bin/django-admin startproject projectname

Configure Passenger WSGI

Create a file in the top-level site directory with the following content to set up Passenger WSGI.

import sys, os
INTERP = "/home/username/"
if sys.executable != INTERP: os.execl(INTERP, INTERP, *sys.argv)

cwd = os.getcwd()
sys.path.append(cwd + '/projectname') #You must add your project here


os.environ['DJANGO_SETTINGS_MODULE'] = "projectname.settings"
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()

Edit Project Settings

Modify the Django project’s file to include necessary configurations.

ALLOWED_HOSTS = ['' , '', 'localhost', '']

    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'mydatabase',
        'USER': 'mydatabaseuser',
        'PASSWORD': 'mypassword',
        'HOST': '',
        'PORT': '3306',

STATIC_ROOT = '/home/username/'

Setup Static Files

Prepare the static files directory and collect all static files from your Django project.

cd ~/
mkdir static
cd ~/
python3 collectstatic

Initialize Database

Run migrate to set up the database schema for your Django project.

python3 migrate

Create a Superuser

Create an administrative user for your Django project’s admin panel.

python3 createsuperuser

Notify Passenger on Changes

After any configuration change, notify Passenger by creating a restart.txt file in the /tmp directory.

mkdir tmp
touch tmp/restart.txt