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.
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 [/caption]
Add a host entry in /etc/hosts/
[caption id="attachment_129" align="aligncenter" width="664"] 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.
There are four steps involved.
- Creating a wsgi file that enables wsgi handler.
- Creating a conf file in sites-available directory of apache
- Enabling the site and reloading apache configuration
- Adding a host entry that points to localhost for the site
Detailed Steps
[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]
[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.
[bash]
sudo a2ensite mysite.conf
sudo service apache2 reload
[/bash]
[caption id="attachment_128" align="aligncenter" width="663"] Run a2ensite and reload apache [/caption]
[caption id="attachment_129" align="aligncenter" width="664"] 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
Post a Comment