Configure mod_wsgi with Apache for running Django project

Part One Install mod_wsgi and make necessary configuration

1. Install mod_wsgi

[sourcecode language="bash"]
#http://code.google.com/p/modwsgi/downloads/list
wget http://modwsgi.googlecode.com/files/mod_wsgi-3.4.tar.gz
tar -zvxf mod_wsgi-3.4.tar.gz
cd mod_wsgi-3.4
#here must define which python to use
#make: *** [mod_wsgi.la] Error 1
./configure --with-python=/opt/python2.7/bin/python
make
sudo make install
[/sourcecode]

if you encounter an error during configure, "checking Apache version... ./configure: line 1704: apxs: command not found"
make sure you have development package of httpd (apache) installed on your machine.

[sourcecode language="bash"]
yum install httpd-devel.x86_64
[/sourcecode]

2. Configure httpd to load mod_wsgi

[sourcecode language="bash"]
#create file mod_wsgi.conf under /etc/httpd/conf.d
sudo vim /etc/httpd/conf.d/mod_wsgi.conf
#save the following line to mod_wsgi.so file
LoadModule wsgi_module modules/mod_wsgi.so
#restart the httpd service
sudo /etc/init.dhttpd restart
[/sourcecode]

Part two: make necessary configuration in /etc/httpd/conf/httpd.conf
In this part of the configuration we are using the Apache VirtualHost to host the django project.
1. Configure apache to serve VirtualHost:

[sourcecode language="text"]
# a. add one more port for listening by Apache which will be specifically used for virtual host:
Listen 8081
# b. uncomment NameVirtualHost *:80 and change the port number from 80 to the port number you defined in step (a), which is 8081 in my case
[/sourcecode]



2.  Add the following configuration for virtual host:
[sourcecode language="text"]
ServerName virtual name of your server
ServerAdmin your email address
DocumentRoot /srv/www/...../cmsdj/
WSGIScriptAlias / /srv/www/..../cmsdj/apache/django.wsgi
Order allow,deny
Allow from all
ErrorLog /srv/www/..../cmsdj/logs/error.log
CustomLog /srv/www/..../cmsdj/logs/access.log combined
[/sourcecode]

3. Create django.wsgi file under your app folder
The detail path to your wsgi file is in the upper virtual host configuration file:

[sourcecode language="text"]
/srv/www/...../cmsdj/apache/django.wsgi
[/sourcecode]

The content of this file:

[sourcecode language="python"]import os
import sys
path = '/srv/www/foodborn.nctr.fda.gov/' #do not add the project folder name to this path
if path not in sys.path:
sys.path.append(path)
os.environ['DJANGO_SETTINGS_MODULE'] = 'cmsdj.settings'
os.environ['PYTHON_EGG_CACHE'] = '/srv/www/foodborn.nctr.fda.gov/.python-eggs'
import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()
[/sourcecode]

4.  Check your project’s urls.py file to make sure that you are using the full path to the models you are importing and in the urls maps:

[sourcecode language="python"]
a. (r'^$', 'core.views.index') is not the full path since you are omitting the name of the project, which should be changed to (r'^$', 'cmsdj.core.views.index'),
b.  “from core import views”  is not the full path, you need add project name “from cmsdj.core import views”
[/sourcecode]

Part Three: serving the admin files
Make sure your project’s settings.py file contents this sentence:

[sourcecode language="bash"]
ADMIN_MEDIA_PREFIX = '/media/'
[/sourcecode]

Then you run the following command to collect all the static files needed by admin models into the project admin folder.

[sourcecode language="bash"]
python manage.py collectstatic
[/sourcecode]

References:
1. http://blog.perplexedlabs.com/2008/11/10/setup-python-25-mod_wsgi-and-django-10-on-centos-5-cpanel/
2. https://docs.djangoproject.com/en/dev/ref/contrib/staticfiles/#module-django.contrib.staticfiles
3. https://docs.djangoproject.com/en/dev/howto/deployment/modwsgi/

No comments:

Post a Comment

Datatable static image not found on the server

When you use ```datatables.min.css``` and ```datatables.min.js``` locally, instead of datatables CDN, you may have encountered that ```sort...