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 DATABASE mydatabase;
CREATE USER 'mydatabaseuser'@'localhost' IDENTIFIED BY 'mypassword';
GRANT ALL PRIVILEGES ON mydatabase.* TO 'mydatabaseuser'@'localhost';
FLUSH PRIVILEGES;
EXIT;

Download and Extract Python

Begin by downloading the Python package and extracting its contents.

cd ~
mkdir tmp
cd tmp
wget https://www.python.org/ftp/python/3.10.13/Python-3.10.13.tgz
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
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 ~/example.com
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 ~/example.com
source ~/example.com/venv/bin/activate
python3 venv/bin/django-admin startproject projectname

Configure Passenger WSGI

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

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

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

sys.path.insert(0,cwd+'/venv/bin')
sys.path.insert(0,cwd+'/venv/lib/python3.10/site-packages')

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 settings.py file to include necessary configurations.

ALLOWED_HOSTS = ['example.com' , 'www.example.com', 'localhost', '127.0.0.1']

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

STATIC_ROOT = '/home/username/example.com/public/static/'

Setup Static Files

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

cd ~/example.com/public
mkdir static
cd ~/example.com/projectname/
python3 manage.py collectstatic

Initialize Database

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

python3 manage.py migrate

Create a Superuser

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

python3 manage.py 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