Skip to main content

Running django on apache with mod_wsgi in Ubuntu/Linux Mint

Django comes with a built in web server that should only be used in development. Integrating django and apache is a bit messy. Apart from Django and Apache itself, libapache2-mod-wsgi is needed to run django with apache2.

There are four steps involved.


  1. Creating a wsgi file that enables wsgi handler.


  2. Creating a conf file in sites-available directory of apache


  3. Enabling the site and reloading apache configuration


  4. Adding a host entry that points to localhost for the site



Detailed Steps


  • First to create a mysite.wsgi file in any directory of your choice with following code
    [python]
    import os
    import sys
    sys.path = ['/var/www/mysite'] + sys.path # change the path to project directory
    os.environ['DJANGO_SETTINGS_MODULE'] = 'mysite.settings'
    import django.core.handlers.wsgi.WSGIHandler()
    [/python]


  • Then create mysite.conf file in /etc/apache2/sites-available directory and write following code
    [xml]
    <VirtualHost *.80>
    WSGIScriptAlias / /home/user/mysite.wsgi

    ServerName = mysite.dev
    Alias /static /var/www/mysite/static/

    <Directory /var/www/mysite/>
    Order allow,deny
    Allow from all
    </Directory>
    </VirtualHost>
    [/xml]
    Change the project name mysite with whatever you are using.


  • Next step is to enable the site with the following command and then reload apache2
    [bash]
    sudo a2ensite mysite.conf
    sudo service apache2 reload
    [/bash]
    [caption id="attachment_128" align="aligncenter" width="663"]Run a2ensite and reload apache Run a2ensite and reload apache [/caption]

  • Add a host entry in /etc/hosts/
    [caption id="attachment_129" align="aligncenter" width="664"]Add a hosts entry to localhost Add a hosts entry to localhost[/caption]

  • Also copy the static files in /usr/local/lib/python2.7/dist-packages/django/contrib/admin/static/ to your project direcory for all the css and js admin panel.

    Comments

    Popular posts from this blog

    jQuery support in Aptana Studio 3

    I use Aptana Studio primarily for web development. It has great support for languages like PHP and JavaScript. But the problem is that I mostly use jQuery rather than code in JavaScript and Aptana does not have good support / content assist from beginning. So we need to install ruble for jQuery library. Ruble is short for Ruby bundle and is a runtime environment that allows the extensibility of Aptana Studio's editors by using Ruby. To install the jQuery bundle follow the following steps: Navigate to Commands | Bundle Developement | Install Bundle. Select jQuery and click OK. Aptana will clone jQuery ruble from github. Include appropriate jQuery sdocml in Project Build Path from Project Properties. Include a VSDoc file for jQuery in your project. [caption id="attachment_47" align="aligncenter" width="1313"] Aptana fetching jquery ruble from GitHub[/caption] You must include appropriate version VSDoc for correct Content Assist. [caption id="attac...

    How to split large mp3 files using mp3splt

    If you have a  lengthy  or large MP3 file and you want to  split  it into files of smaller size or duration, You can easily do this using mp3splt. Mp3splt   is an utility to split mp3 and ogg vorbis digital audio files without the need of decoding. It can be used to split a file into no of tracks of fixed duration and size or specified  split points. It supports variable bit rate mp3 files, silent detection, splitting with local .XMCD, .CDDB or .CUE file splitpoints or from external servers like   tracktype.org . It copies the original files first and then generates new, smaller files during the process of splitting without altering the original file. It does not do any encoding or decoding so the splitting is very fast and lossless. Steps to follow Navigate to directory where you have downloaded mp3splt { if using portable version } . Run and execute mp3slpt using the command   mp3splt  . Specify the options according to which you wanna split. The basic syntax is   mp3splt [file path]...