How to setup wsgi and Django on Ubuntu server.

It actually very easy to setup wsgi and Django.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# Install Django first if not installed
$ sudo apt-get install pip
$ sudo pip install Django

# Install wsgi
$ sudo apt-get install libapache2-mod-wsgi

# Edit virtual host config file as follows:
<VirtualHost *:80>
ServerAdmin ycshao0402@gmail.com
ServerName image.ycshao.com
DocumentRoot /var/www/image
WSGIScriptAlias / "/var/www/image/wsgi.py"

...
<virtualHost>

# Restart apache
$ service apache2 restart

# Don't forget to assign owner of the directory to www-data
$ chown -R www-data /dir/to/your/site

# Done!

And wsgi.py will look like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
import os
import os.path
import sys

sys.path.append(os.path.join(os.path.dirname(__file__)))

os.environ['DJANGO_SETTINGS_MODULE'] = 'img_site.settings'

from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()
#application = django.core.handlers.wsgi.WSGIHandler()
# Apply WSGI middleware here.
# from helloworld.wsgi import HelloWorldApplication
# application = HelloWorldApplication(application)