Sunday, June 10, 2012

Django Site in Production with Lighttpd & FastCGI

OS : Ubuntu

Python : 2.7.1

1) Install lighttpd
sudo apt-get, install lighttpd
2) Install django
I want to download it to / var / www installation,
$ Cd / var / www
$ Sudo wget http://media.djangoproject.com/releases/1.3/Django-1.3.1.tar.gz
$ Sudo tar xzvf Django-1.3.1.tar.gz
$ Cd the Django-1.3.1
$ Sudo python setup.py install
3) Installed back to / www
cd / var / www
4) Then we have to check the django whether the installation is successful, we first build a new project with django-admin.py
the sudo django-admin.py startproject the portal
the cd the portal
sudo the chmod + the x the manage.py
sudo python manage.py runserver
Displays the following screen, django has a normal installation
Validating the models ...

0 errors found
Django version 1.3.1, using settings 'portal.settings'
Development server is running at http://127.0.0.1:8000/ with
Quit the server with CONTROL-C.
5) install flup
This is the WSGI Server, before installation, you must first determine the version of the python in the command line input
$ / Var / www / portal $ python
Python 2.7.1 + (r271: 86832, Apr 11 2011, 18:13:53)
[GCC 4.5.2] on linux2 to
Ubuntu 11.04 Built-in 2.7.1 version? or because I just have the update?? XD So we had to select the development flup-1.0.3 version
$ Cd / var / www
$ Sudo wget http://pypi.python.org/packages/2.7/f/flup/flup-1.0.3.dev_20110405-py2.7.egg # md5 = 
   1fa9a03ade17f88990340884a1113b5a
$ Sudo wget http://peak.telecommunity.com/dist/ez_setup.py
$ Sudo python ez_setup.py flup-1.0.3.dev_20110405-py2.7.egg
6)by adding mysite.fcgi
This code is used to tell the server how to deal with the fastcgi program
#! / Usr / bin / python
import sys, os
# Add a custom Python path.
sys.path.insert (0, / var / www ")
# Switch to the directory of your project. (Optional.)
# Os.chdir ("/ home / user / myproject")
# Set the the DJANGO_SETTINGS_MODULE environment variable.
os.environ ['DJANGO_SETTINGS_MODULE'] = "portal.settings"
from django.core.servers.fastcgi import runfastcgi
runfastcgi (["method = threaded", "daemonize = false"])
Above the red part, be especially careful if you look at the django official website files are written is not the same as the official website of
the paper was wrong, or is the older version of the file XD. I later installed a long time, only to find that there are wrong installation should pay
special attention to Oh! Storage of the above file, enter
$ For python mysite.fcgi
There will be a long list of things. Find what you see

It worked!

This script file is correct.
7) edit the lighttpd config
Start the fastcgi mode
$ Sudo lighttpd-enable-mod Fastcgi
Then here to note in the / etc / lighttpd / you will see to lighttpd.conf this profile do not move, which is the default. lighttpd read this default will
go to read the folder conf-enabled profile without charge. The start fastcgi mode, so read the default
lighttpd would automatically go read the conf-enabled inside the profile. Set fastcgi
$ Vim / etc/lighttpd/conf-enabled/10-fastcgi.conf server.modules + = ("mod_fastcgi",
"Mod_accesslog
)

server.modules + = ("mod_rewrite",
Mod_fastcgi under "
"Mod_accesslog
)
$ SERVER ["socket"] == "172.16.33.10:8000" {
server.document-root = "/ var / www / portal"
the fastcgi.server = (
Fcgi "=> (
"Localhost" => (
The bin-path = "/ var / www / portal / mysite.fcgi
"Socket" => "/ var / www / portal / mysite.sock",
"Check-local" => "the disable"
)
),
)

alias.url = (
"/ Media" => "/ usr/local/lib/python2.7/dist-packages/django/contrib/admin/media /",
)

url.rewrite-once = (
"^ (/ Media. *) $" => "$ 1"
^ / The favicon.ico $ "=>" / media / the favicon.ico
"^ (/ *) $" => "/ Mysite.fcgi $ 1"
)
}
8)Establish runfastcgi the script
This script is mainly through the manage.py the command the specified runfcgi parameter operation, this script will be related to things in
order to facilitate the start Fastcgi the server into a script.
    #! / Bin / bash
    # Replace these three settings.
    PROJDIR = "/ var / www / portal"
    PIDFILE = "$ PROJDIR / mysite.pid"
    SOCKET = "$ PROJDIR / mysite.sock"


    cd $ PROJDIR
    if [-f $ PIDFILE]; then
    kill `cat - $ PIDFILE`
    rm-f - $ PIDFILE
    fi


    the exec / usr / bin / env - \
    PYTHONPATH is = ".. / Python: .." \
    . / Manage.py runfcgi socket = $ SOCKET pidfile = $ PIDFILE 
9)To remember to to modify runfastcgi execute permissions.
$ Sudo chmod + x runfastcgi
10)Restart lighttpd
$ / Etc / init.d / the lighttpd
11 ) implementation of runfastcgi
$. / Runfastcgi
12 )enter the URL in the browser http:// [yourIP]: 8000
You will able to see start page of your django site.
13 )File permissions
drwxr--xr-x 2 www-data root 4096 2011-09-22 19:42 the portal

portal folder inside the file are as follows:
- Rw-r - r - 1 www-data root 0 2011-09-22 19:16 __ init__.py
-Rw-r - r - 1 www-data root 124 2011-09-22 19:17 __ init__.pyc
-Rwxrwxrwx 1 www-data root 503 2011-09-22 19:16 the manage.py
-Rwxrwxrwx 1 www-data root 401 2011-09-22 7:22 p.m. mysite.fcgi
-Rwxrwxrwx 1 www-data root 334 2011-09-22 19:31 runfastcgi
-Rw-r - r - 1 www-data root 5031 2011-09-22 19:16 your settings.py
-Rw-r - r - 1 www-data root 2652 2011-09-22 19:17 settings.pyc
-Rw-r - r - 1 www-data root 565 2011-09-22 19:16 The urls.py
-Rw-r - r - 1 www-data www-data 261 2011-09-22 19:42 urls.pyc
13 )use the django when the Admin can not find staic resource (CSS, image, js )?
Modify the lighttpd config. alias.url = ("/ static / admin /" => "/ usr/local/lib/python2.7/dist-packages/django/contrib/admin/media /")

[Reference]
http://blog.finalevil.com/2011/09/how-to-use-django-on-lighttpd-with.html

1 comment:

  1. Hi, I tried to follow your steps here but stuck at number 4. after I've done

    sudo chmod +x manage.py

    Then I ran the line

    sudo python manage.py runserver

    but this error came out,

    sudo: manage.py: command not found


    Can you help me here? I've followed your steps faithfully until number 4 where I stuck there.

    ReplyDelete