DreamHost & Django how to setup 2020   

 

Long story: I used Wordpress for my couple websites and found that I had lots of trouble with plugins and finding it just hard to use when I wanted something specific. I knew a little bit of Python and start searching around for alternatives and I found Django and Flask. Django “seemed” easier since it came with a lot of built in features compared to flask which is more barebones. A beginner programmer with not much knowledge of web application frameworks I took the plunge with Django

 

   How to get Django working on a DreamHost server

 

What you need? If you haven't signed up for DreamHost or on the fence about it try this affilate link dreamhost.com 

  • SSH access to your DreamHost server
  • I’m assuming you have put your Django project uploaded in ~/WEBSITENAME/django_project e.g. /techgeek.biz/django_project
  • Once you are logged in you can copy and past the whole thing! And it will take a few mins to setup. Or do line by line and watch it

# install python

cd ~

mkdir tmp

cd tmp

wget https://www.python.org/ftp/python/3.8.1/Python-3.8.1.tgz

tar zxvf Python-3.8.1.tgz

cd Python-3.8.1

./configure --prefix=$HOME/opt/python-3.8.1

make

make install

cd ~

 

# add to bash profile

export PATH=$HOME/opt/python-3.8.1/bin:$PATH

. ~/.bash_profile

# should show where it is installed

which python3

python3 --version

pip3 --version

 

# upgrade pip

python3 -m pip install --upgrade pip

 

# install virtualenv

pip3 install wheel

pip3 install virtualenv

 

# makevirtual env

virtualenv -p ~/opt/python-3.8.1/bin/python3 ~/prodvenv

source ~/prodvenv/bin/activate

python -V

python3 -m pip install --upgrade pip

pip install wheel

 

# if you have a requirements file

pip install -r requirements.txt

 

 

Additionally, you will 2 extra files and a folder

  • In your website directory eg techgeek.biz/
  • techgeek.biz/passenger_wsgi.py
  • In your passenger file

 

## Direct import

import sys, os

INTERP = "/home/USERNAME/prodvenv/bin/python"

#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 + '/django_project')  #You must add your project here

 

sys.path.insert(0,cwd+'/prodvenv/bin')

sys.path.insert(0,cwd+'/prodvenv/lib/python3.8/site-packages')

 

os.environ['DJANGO_SETTINGS_MODULE'] = "django_project.settings"

from django.core.wsgi import get_wsgi_application

application = get_wsgi_application()

 

 

Next the webserver looks to this folder/restart.txt to see if it needs to be restarted

# folder

techgeek.biz/tmp

# file

techgeek.biz/temp/restart.txt

 

Easy way to do this is

touch techgeek.biz/temp/restart.txt

 

Everything should be working…… should….   

What you can do to test and see the errors is launch the passagenger_wgi.py file, I actually discovered this by accident and its super helpful, it will show you what’s failing and where

 

# head to directory

cd techgeek.biz/

python passenger_wsgi.py

 

As I said this will give you some insight why possibly it’s not working

 

Hope this one was especially helpful for you


Luke Keam
Thank you for reading. Email me: [email protected]
Luke Keam | techgeek.biz