---
title: "Set up Brand New Django Server "
author: "eonbell"
date: "2011-02-25T13:31:06Z"
tags: []
categories: ["编程语言"]
slug: "set-up-brand-new-django-server"
draft: false
lastmod: "2011-02-25T13:31:06Z"
id: 929716
url: "https://eonbell.iteye.com/blog/929716"
---

Set up Brand New Django Server ¶

steps to set up django server (in linux):

   1. make sure set up the path of /usr/local/bin before everything 

   1. install httpd (remember to set mod_rewrite) apt-get install apache2-threaded-dev 

    /etc/init.d/apache2 restart

    sudo a2enmod rewrite

    ./configure --enable-rewrite

1b. have quick test on apache server

(when apache server is compile with --enable-rewrite, there would be no need to load mod_rewrite.so)

( if apache already installed without mod_rewrite, and mod_rewrite.so could not be found, use the following command:
/usr/local/apache2/bin/apxs -c -i -a /modules/mappers/mod_rewrite.c )

   2. compile and install mod_fastcgi 

cp Makefile.AP2 Makefile

make

make install

2.4 install sqlite3 (if not already there) (apt-get install sqlite3)

   3. install python (apt-get install python-dev) 

3b. try import sqlite3

   4. install setup tools (apt-get install python-setuptools ) 

   5. install subversion (sudo apt-get install subversion) 

   6. extract django project source 

6b. have a quick stand alone test of the django site

6c. install optional (e.g. PIL, lxml, simplejson,flup) libraries when error is found

(

sudo apt-get install libxml2 libxml2-dev

sudo apt-get install python-imaging (install PIL)

sudo easy_install simplejson

sudo easy_install flup

)

   7. modify apache configuration for django project cgi (refer to existing httpd.conf) 

(sudo apt-get install libapache2-mod-wsgi)

8.

create a file named django.wsgi in your app.

import os]import sys

os.environ! = 'seescanlike.settings']sys.path.append("/app/UAT")]sys.path.append("/app/UAT/seescanlike")

import django.core.handlers.wsgi]application = django.core.handlers.wsgi.WSGIHandler()]


   9. create a file in /etc/apache2/sites-avaliable , such as 001-ssl-uat. and mkdir the logs folder(/app/apache_logs/seescanlike_uat/) at first and chmod 777. 

<VirtualHost *:80>
        ServerName app.sslprd.motherapp.com
        #ServerAlias 4t-cms.4tprd.motherapp.com

        RewriteEngine On

        AddOutputFilterByType DEFLATE text/html application/x-javascript text/xml

        DocumentRoot /app/UAT/seescanlike

        RewriteRule ^/(d-media)($|(\/(.*))) /app/UAT/seescanlike/$0 
        RewriteRule ^/(media)($|(\/(.*))) /usr/local/lib/python2.6/site-packages/django/contrib/admin/$0 
        WSGIDaemonProcess seescanlike_uat threads=25
        WSGIProcessGroup seescanlike_uat
        WSGIScriptAlias / /app/UAT/seescanlike/django.wsgi

        <Directory />
                Options FollowSymLinks
                AllowOverride None
        </Directory>

        <Directory /app/UAT/seescanlike>
            Options FollowSymLinks MultiViews
            AllowOverride AuthConfig
            Order allow,deny
            allow from all
        </Directory>

        ErrorLog /app/apache_logs/seescanlike_uat/error.log

        # Possible values include: debug, info, notice, warn, error, crit,
        # alert, emerg.
        LogLevel warn

        CustomLog /app/apache_logs/seescanlike_uat/access.log combined

</VirtualHost>





10.

cd /etc/apache2/sites-enabled/

ln -s ../sites-avaliable/001-ssl-uat 001-ssl-uat


  11. restart apache. 

/etc/init.d/apache2 restart


12.

add below statemets into seetings_common.py.

otherwise ,will cause a error : IOError: sys.stdout access restricted by mod_wsgi

import sys ]sys.stdout = sys.stderr


  13. Problem "Unable to Open Database File" 

    using SQLite3, your DATABASE_NAME is set to the database file's full path, the database file is writeable by Apache, but you still get the above error.

Solution

Make sure Apache can also write to the parent directory of the database. SQLite needs to be able to write to this directory.

Make sure each folder of your database file's full path does not start with number, eg. /www/4myweb/db (observed on Windows 2000).

If DATABASE_NAME is set to something like '/Users/yourname/Sites/mydjangoproject/db/db', make sure you've created the 'db' directory first.

Make sure your /tmp directory is world-writable (an unlikely cause as other thing on your system will also not work). ls /tmp -ald should produce drwxrwxrwt ...

finished.

/

run site in fast cgi (refer to renew_process), create fastcgi daemon inside ThreeCS folder:

python manage.py runfcgi method=threaded host=127.0.0.1 port=9001

   9. edit apache2 httpd.config (or /etc/apache2/extra/httpd-vhosts.conf depending on your machine) 

here is an sample:

--- 
FastCGIExternalServer /usr/local/apache2/3HK/ThreeCS/mysite.fcgi -host 127.0.0.1:9001

<VirtualHost *:80>

  ServerName ma.three.com.hk 
  DocumentRoot "/usr/local/apache2/3HK/ThreeCS" 
  Alias /media /Library/Python/current/site-packages/django/contrib/admin/media
  RewriteEngine On 
  RewriteRule ^/(media.*)$ /$1 
  RewriteRule ^(.*) /mysite.fcgi/$0  
  ErrorLog "logs/error_ma_log"
  CustomLog "logs/access_ma_log" common

</VirtualHost>

Remarks 1: please make sure your DocumentRoot? and all its parent folders are accessible by the world (DocumentRoot?: chmod a+rx Parents: chmod a+x)

Remarks 2: mysite.fcgi can be a non-exist file (just for redirection)

  10. restart apache2 

  12. create a fastcgi django process (renew_process) 

(new) 12.5 install mod_wsgi

 http://code.google.com/p/modwsgi/downloads/detail?name=mod_wsgi-3.3.tar.gz

  13. setup watchdog on crontab if neccessary 

some hints on troubleshooting:

   1. try to run django project as stand alone server and see if it is OK 

   2. try to telnet port 9001 and see if the port is opened 

   3. try to use treat the django source as simple static site to check if content could be reached, and behaviour of mod_rewrite 

   4. make sure the user account to run apache daemon has the right to access the web folder 

  14. to install mod_header 

/usr/local/apache2/bin/apxs -c -i -a /modules/metadata/mod_headers.c

  15. in case of css problem in the admin page, add the following soft link under the following project root 

media -> /Library/Python/2.6/site-packages/django/contrib/admin/media