How to Make a Django Website on DreamHost

by

Hey there! Some links on this page may be affiliate links which means that, if you choose to make a purchase, I may earn a small commission at no extra cost to you. I greatly appreciate your support!

In this tutorial, you will learn how to deploy Django on DreamHost. This tutorial will cover installing a Python virtual environment on DreamHost, starting a Django app, and configuring the Passenger wsgi file.

NOTE: You will need SSH access to a DreamHost hosting package. If you need help accessing your DreamHost server, see How to Login via SSH on DreamHost.

1. Install a Python Virtual Environment

Create a virtual environment for Python 3.

python3 -m pip install --upgrade pip
pip3 install virtualenv
virtualenv -p /usr/bin/python3 dj

You can activate your virtual environment like this.

source dj/bin/activate

2. Install Django

With your virtual environment activated, install Django and other Python packages that you might need via the pip package manager.

pip3 install Django

3. Create Django Project

Create a Django project called helloworld in the root of your web directory.

cd django.tonyflorida.me
django-admin startproject helloworld

4. Configure the Web Server Gateway Interface

The web server gateway interface, or Passenger in this case, allows Django to communicate with the web server. Create a file in the root of your website directory called passenger_wsgi.py with the following content modified for your system.

import sys, os
INTERP = "/home/lemonaid/dj/bin/python3" # absolute path to your virtual env
#INTERP is present twice so that the new python interpreter 
#knows the actual executable path 
if sys.executable != INTERP: os.execl(INTERP, INTERP, *sys.argv)

cwd = os.getcwd()
sys.path.append(cwd)
sys.path.append(cwd + '/helloworld')  # name of your Django project

sys.path.insert(0,cwd+'../dj/bin') # virtual env # relative path to your virtual env
sys.path.insert(0,cwd+'../dj/lib/python3.8/site-packages') # relative path to your virtual env

os.environ['DJANGO_SETTINGS_MODULE'] = "helloworld.settings" # name of your Django project
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()

5. Configure Django Settings

Open and edit your Django settings file which in my case is located at /home/lemonaid/django.tonyflorida.me/helloworld/helloworld/settings.py.

Add your domain name to your ALLOWED_HOSTS.

ALLOWED_HOSTS = ['django.tonyflorida.me', 'www.django.tonyflorida.me']

Add the absolute path to your public/static directory which will be the home for your static files like images, JavaScript, and CSS files.

STATIC_URL = '/static/'
STATIC_ROOT = '/home/lemonaid/django.tonyflorida.me/public/static/'

Next, create the static folder in your public directory.

cd ~/django.tonyflorida.me/public/
mkdir static

You can move your static files into the static directory with the collectstatic command.

cd helloworld
python manage.py collectstatic

6. Test it Out

In a web browser, go to your domain and you will see the default Django page. If you need more context, please see the video below which will walk you through the process of setting up a Django website on DreamHost.

YouTube video


Meet Tony

With a strong software engineering background, Tony is determined to help as many people as possible start their online busines. Discover why Tony quit his hedge fund job to pursue this mission. You can send Tony a message here.

Leave a Comment