2013-05-14

Set up Django 1.5.1 in DreamHost VPS

This post is about setting up Django 1.5.1 in a virtualenv in DreamHost VPS with Apache2 and mod_wsgi 3.4, and is mostly an update to the following post about three years ago:
http://www.wombatnation.com/2010/06/django-on-dreamhost-ps/comment-page-1
Software Versions:

   Apache 2.2.22
   Django 1.5.1
   mod_wsgi 3.4
   PIP 1.3.1
   Python 2.6.6
   VirtualEnv 1.9.1

1. Sign up a VPS account with DreamHost.

2. Create a regular user [user] in the VPS.  The Django project will run under this account.

3. Create an admin user [admin] in the VPS to manage the web server.  This admin account has sudo privileges.

4. Install Python PIP (1.3.1)

   [admin]$ sudo easy_install pip

   PIP is installed at /usr/local/bin/pip.

5. Upgrade VirtualEnv (from 1.4.9 to 1.9.1)

   [admin]$ sudo pip install --upgrade virtualenv

6. Create a virtualenv and install Django (1.5.1) in it

   [user]$ mkdir ~/django
   [user]$ virtualenv ~/django/env
   [user]$ source  ~/django/env/bin/activate
   [env]$ pip install django

7. Create a Django project (demo)

   [env]$ cd ~/django
   [env]$ django-admin.py startproject demo

8. Verify Django project is working

   [env]$ cd ~/django/demo
   [env]$ python manage.py runserver 0.0.0.0:8000

   Visit http://your.domain.com:8000 to verify Django project is up and running.

9. Install mod_wsgi 3.4

   [admin]$ mkdir ~/src
   [admin]$ cd ~/src
   [admin]$ wget http://modwsgi.google.com/files/mod_wsgi-3.4.tar.gz
   [admin]$ tar xzvf mod_wsgi-3.4.tar.gz
   [admin]$ cd mod_wsgi-3.4
   [admin]$ ./configure --with-apxs=/usr/local/dh/apache2/template/sbin/apxs --with-python=/usr/bin/python
   [admin]$ make
   [admin]$ sudo make install

   mod_wsgi is installed at /usr/local/dh/apach2/template/lib/modules/mod_wsi.so

10. Edit httpd.conf

   [admin]$ sudo vim /usr/local/dh/apache2/apache2-psNNNNNN/etc/httpd.conf

   psNNNNNN is your VPS id.

   add the following line in the LoadModule section:
   LoadModule wsgi_module /dh/apache2/template/lib/modules/mod_wsgi.so

   add the following line outside of VirtualHost directive:
   WSGIPythonPath /home/user/django/env/lib/python2.6/site-packages

   add following line inside the VirtualHost directive:
   WSGIScriptAlias /demo /home/user/django/demo/demo/wsgi.py

11. Restart Apache

   [admin]$ sudo /etc/init.d/httpd2 restart apache2-psNNNNNN

12. Edit wsgi.py

   [env]$ vim ~/django/demo/demo/wsgi.py

   add next two lines at the top of the file:
   import sys
   sys.path.insert(0, '/home/user/django/demo')

13. Create a Django app (demoapp) and update urls.py

   [env]$ cd ~/django/demo
   [env]$ python manage.py startapp demoapp
   [env]$ vim ~/django/demo/demoapp/views.py

   Make it look like:
   from django.http import HttpResponse

   def index(request):
       return HttpResponse("Hello world!")

   [env]$ vim ~/django/demo/demo/urls.py

   Add a url pattern:
   url(r'^$', 'demoapp.views.index')

14. Verify

   Visit http://your.domain.com/demo, will see "Hello World!" in browser.

-End-

No comments: