March 22, 2015 in Django
I have recently moved away from PHP and begun working with more all-purpose languages, Python being the main one. While experimenting with this new language, I rewrote my blog to use the Python web framework Django. Django 1.7 with Python 3.x is not supported by default on Dreamhost's shared servers, so here is how I installed my own custom version of Python to deploy my app.
In this tutorial I will walk you through installing Python 3.4.3 and Django 1.7 in a Python virtual environment on a Dreamhost shared server.
First, ssh into your server an make sure you are in the home directory. In this directory create a 'python' folder and move to it
mkdir python cd python
Download and extract the Python source so we can compile our custom installation.
wget http://python.org/ftp/python/3.4.3/Python-3.4.3.tar.xz tar xvfJ Python-3.4.3.tar.xz cd Python-3.4.3
Compile the source and install it in a directory called 'Python34'.
./configure --prefix=$HOME/Python34 make make install
Add the installation to the PATH variable.
echo 'export PATH=$HOME/Python34/bin:$PATH' >> ~/.bash_profile source ~/.bash_profile
Create a virtualenv and install Django.
pyvenv-3.4 venv source venv/bin/activate pip install django
Now Python is installed and ready to go. To finish setting up your site, edit the settings in your Dreamhost dashboard to allow for Passenger. Once you have done this, create a file passenger_wsgi.py in the root of your website directory.
import sys, os cwd = os.getcwd() sys.path.append(cwd) sys.path.append(cwd + '/my_project') INTERP = os.path.expanduser("~/venv/bin/python") if sys.executable != INTERP: os.execl(INTERP, INTERP, *sys.argv) sys.path.insert(0,'$HOME/venv/bin') sys.path.insert(0,'$HOME/venv/lib/python3.4/site-packages/django') sys.path.insert(0,'$HOME/venv/lib/python3.4/site-packages') os.environ['DJANGO_SETTINGS_MODULE'] = "my_project.settings" from django.core.wsgi import get_wsgi_application application = get_wsgi_application()
Create the restart file to start your passenger site
touch tmp/restart.txt
Finally, you add your application to the root of the website directory. Now we can set up the Django application.
cd my_project python manage.py migrate python manage.py loaddata data.json python manage.py collectstatic
That's all there is to it! Now whenever you make a change, you need to edit tmp/restart.txt. Passenger looks at the time-stamp of that file to determine if it needs to restart.