Configure mod_wsgi with Apache for running Django project

Part One: prepare the environment
 1. Download source code of modwsgi from http://code.google.com/p/modwsgi/downloads/list
 2. Unpack the downloaded package tar -zvxf package_name
 3. Enter the unpackaged folder, run. /configure 
 4. if you encounter an error, make sure you have development package of httpd (apache) installed on your machine.
 to install the development package of Apache, run yum install httpd-devel.x86_64 
 5. re-run $./configure
 6. $ make
 7. sudo make install
 8. create file mod_wsgi.conf under /etc/httpd/conf.d, ans add th efollowing line to the file you created:
 LoadModule wsgi_module modules/mod_wsgi.so
 9. restart the httpd service: sudo /etc/init.dhttpd restart
10. make clean

 Part two: make necessary configuration in httpd.conf file located in /etc/httpd/conf/
 In this part of the configuration we are using the Apache VirtualHost to host the django project.
 1. Configure apache to server VirtualHost:
 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
 2.  Add the following configuration for virtual host:
 <VirtualHost *:8081>
    ServerName virtual name of your server
    ServerAdmin your email address
    DocumentRoot /srv/www/...../cmsdj/
    WSGIScriptAlias / /srv/www/..../cmsdj/apache/django.wsgi
    <Directory /srv/www/..../cmsdj/apache>
      Order allow,deny
      Allow from all
    </Directory>
    ErrorLog /srv/www/..../cmsdj/logs/error.log
    CustomLog /srv/www/..../cmsdj/logs/access.log combined
</VirtualHost>
3. Create django.wsgi file under your app folder
 The detail path to your wsgi file is in the upper virtual host configuration file:
 /srv/www/foodborn.nctr.fda.gov/cmsdj/apache/django.wsgi

The content of this file:
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()

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:
  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”

Part Three: serving the admin files
 Make sure your project’s settings.py file contents this sentence:
 ADMIN_MEDIA_PREFIX = '/media/'
  Then you run the followingcommand to collect all the static files needed by admin models into the project admin folder.
 python manage.py collectstatic

 
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...