How I deployed Django

software development

#1

I wrote a small recount of how I deployed Django 1.4 and Python 2.7.3 within a virtualenv on Dreamhost

I am no expert (actually a complete noob) but I managed to get it working.

Here is a link to the original post I made

This guide upgrades the default python installation to 2.7.3, installs virtualenv and pip.

I assume that you have created a project locally and it works without any problems when you run python manage.py runserver.

I assume that you have made changes to your setting file for a production server eg turned debug off. If not, follow this guide.

I assume that you have used git to create a repository. I recommend using Bitbucket - make a private repository and use this guide if you are not familiar with it.

I used MySQL so if you used any others then you will have to Google for a different method when you reach the MySQL related steps.

I assume that your local project makes use of other apps that you have downloaded e.g South

Log into the Dreamhost control panel, go into the user section and create a user with shell access.
Go into the domain section and create a new domain. Make sure you select the “Passenger” option.
Open the MySQL database section and create a database. These settings will be used in settings.py
SSH into the server. If you are using windows you should use Putty but I have never used it. In linux, open up a terminal and type:

ssh <shell_user>@<server_name>.dreamhost.com

You need to replace <shell_user> with the username you created in step 1
<server_name> can be found on the Dreamhost control panel. Go into the “manage users” section and it is listed under “machine” beside the shell account you created. You will then be asked your password.
Voila! You are in. To upgrade python, follow these steps:

 wget http://www.python.org/ftp/python/2.7.3/Python-2.7.3.tgz
 tar zxvf Python-2.7.3.tgz
 cd Python-2.7.3
 ./configure --prefix=$HOME/Python27
 make
 make install

To install pip, we also have to install easy_install:

wget http://peak.telecommunity.com/dist/ez_setup.py
python ez_setup.py
easy_install pip

Next we install virtualenv then make a new environment under the domain that we created in step 2 and finally switch into this environment:

pip install virtualenv
virtualenv /home/<shell_user>/<yourdomain.com>/env
source /home/<shell_user>/<yourdomain.com>/env/bin/activate

Now we update bashrc so that it has access to the new python and anything you install in the virtualenv:

cd $home
vi ~/.bashrc

(use this to navigate the file: http://www.lagmonster.org/docs/vi.html )

add the following to the file by pressing i to enter insert mode:

export PATH="$HOME/Python27/bin:$PATH"
export PATH="$HOME/<site>/env/bin/:$PATH"

press escape to go back to commander mode, then type “:x” then press enter. This will save and exit. Vi takes some getting used to. You should be back at the shell. Type:

source ~/.bashrc

Now you can install your required packages:

pip install django==1.4.1
pip install south
pip install ...

Change into the site directory then use git to clone your repository using these commands:

cd $home
cd <yourdomain.com>
git clone https://<bitbucket_username>@bitbucket.org/<bitbucket_username>/<repo>

If you want to copy the contents of your local database then do the following:
On the local machine (not in the terminal that is connected to the server):

mysqldump -u <local_user> -p <db-name> > db-name.out
scp db-name.out <shell_user>@<server_name>.dreamhost.com:/home/<shell_user>/<yourdomain.com>

Back in the original terminal that is connected via SSH, using the database details set in step 3:

cd $home
cd <yourdomain.com>
mysql -u <sql_user> -p <db-name> < db-name.out

Copy your static assets into your <yourdomain.com>/public folder. Assuming they were part of your repo and in a folder called “assets”, use these commands:

cd $home
cd <yourdomain.com>
cd <youprojectname>
cp -r /home/<shell_user>/<yourdomain.com>/<yourprojectname>/assets /home/<shell_user>/<yourdomain.com>/public

Go into the domain folder and create a file called passenger_wsgi.py:

cd $home
cd <youdomain.com>
vi passenger_wsgi.py

In here, press i to enter insert mode then paste the following. You will need to adjust this to match your site:

import sys, os
cwd = os.getcwd()
sys.path.append(cwd)

project_location = cwd + '/<youprojectname>'

sys.path.insert(0, project_location)

#Switch to new python
if sys.version < "2.7.3": os.execl("/home/<shell_user>/<yourdomain.com>/env/bin/python",
    "python2.7.3", *sys.argv)

sys.path.insert(0,'/home/<shell_user>/<yourdomain.com>/env/bin')
sys.path.insert(0,'/home/<shell_user>/<yourdomain.com>/env/lib/python2.7/site-packages/django')
sys.path.insert(0,'/home/<shell_user>/<yourdomain.com>/env/lib/python2.7/site-packages')


os.environ['DJANGO_SETTINGS_MODULE'] = "<youprojectname>.settings"

import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()

That should be it! Exit vi by pressing escape then typing :x to save and exit. Test it by running “python passenger_wsgi.py” in the command and there should not be any errors.

I am sure that there are plenty of errors in this but I wrote this guide with the thought “what would I have wanted in a guide to save me hours of time!”.

I recommend reading around the subject a lot. The Dreamhost wiki is good: wiki.dreamhost.com and Google everything.

I want to thank everyone before me that has taken the time to write up their experience of deploying to Dreamhost - there was one post in particular that I found that helped me immensely and makes up the chunk of this advice ( I copied down the commands in order in a text file) but I cannot find it in my history.

I am not an expert in website deployment, far from it, but I hope that this helps someone out there.