Python 2.6+Django 1.2.1 + Nginx

Python 2.6.5 + Django 1.2.1 + Nginx Installation && Configuration
fastcgi+django,fastcgi+php

一、install
## 万恶的XXX,封掉了python下的所有目录,首页上的download链接是打不开的,不过可以到其他的链接下载 ##
## 1、http://ftp.python.org/ftp/python/2.6.5/
## 2、http://www.python.org/ftp/python/

1. Python 2.6.5
1) install

wget http://ftp.python.org/ftp/python/2.6.5/Python-2.6.5.tgz
 
tar zxvf Python-2.6.5.tgz
cd Python-2.6.5
./configure --prefix=/opt/python26
make
make install
 
ln -s /opt/python26/bin/python2.6 /usr/bin/python26

## add the /opt/python26/bin to the PATH

vi /etc/profile

###########################

PATH=”$PATH:/opt/mysql/bin:/opt/python/bin:/opt/nginx/sbin:/opt/python26/bin”;export PATH

###########################

2)test

[root@devel photo_uw]# python26
Python 2.6.5 (r265:79063, Jul 20 2010, 10:39:03)
[GCC 4.1.2 20080704 (Red Hat 4.1.2-46)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>>

—————————————————————-

2. PIL

1) install

wget http://effbot.org/downloads/Imaging-1.1.7.tar.gz
 
tar zxvf Imaging-1.1.7.tar.tar
cd Imaging-1.1.7
python26 setup.py build_ext -i
python26 setup.py install
cd ..

2) test

[root@devel photo_uw]# python26
Python 2.6.5 (r265:79063, Jul 20 2010, 10:39:03)
[GCC 4.1.2 20080704 (Red Hat 4.1.2-46)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import Image
>>>

—————————————————————-

3. setuptools

wget http://pypi.python.org/packages/source/s/setuptools/setuptools-0.6c11.tar.gz
 
tar zxvf setuptools-0.6c11.tar.gz
cd setuptools-0.6c11
python26 setup.py install
cd ..

—————————————————————-

4. flup

wget http://www.saddi.com/software/flup/dist/flup-1.0.2.tar.gz
 
tar zxvf flup-1.0.2.tar.gz
cd flup-1.0.2
python26 setup.py install
cd ..

—————————————————————-

5. MySQL-python

1) install

wget http://nchc.dl.sourceforge.net/project/mysql-python/mysql-python/1.2.3/MySQL-python-1.2.3.tar.gz
 
tar zxvf MySQL-python-1.2.3c1.tar.gz
cd MySQL-python-1.2.3c1
python26 setup.py build
python26 setup.py install
cd ..

2) test

[root@devel photo_uw]# python26
Python 2.6.5 (r265:79063, Jul 20 2010, 10:39:03)
[GCC 4.1.2 20080704 (Red Hat 4.1.2-46)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import MySQLdb
>>>

—————————————————————-

6 Django 1.2.1

1) install

wget http://media.djangoproject.com/releases/1.2/Django-1.2.1.tar.gz
 
tar zxvf Django-1.2.1.tar.gz
cp -R Django-1.2.1 /opt/python26
cd /opt/python26/lib/python2.6/site-packages/
ln -s /opt/python26/Django-1.2.1/django django

or

cd Django-1.2.1
python26 setup.py install

2) test

[root@devel photo_uw]# python26
Python 2.6.5 (r265:79063, Jul 20 2010, 10:39:03)
[GCC 4.1.2 20080704 (Red Hat 4.1.2-46)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import django
>>> django.get_version()
'1.2.1'

7. django-treebeard

1) install

wget http://pypi.python.org/packages/source/d/django-treebeard/django-treebeard-1.61.tar.gz
 
tar zxvf django-treebeard-1.61.tar.gz
cd django-treebeard-1.61
python26 setup.py install

2) test

[root@devel photo_uw]# python26
Python 2.6.5 (r265:79063, Jul 20 2010, 10:39:03)
[GCC 4.1.2 20080704 (Red Hat 4.1.2-46)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import treebeard
>>>

二. configure

1) nginx

cd /opt/nginx/conf/vhosts
 
vi vhost-www_tech.conf
 
--------------------------------------------------------------------
	upstream backend_www_tech {
		#server unix:/var/run/fcgi/www/www_tech.sock;
		server unix:/var/run/fcgi/www_tech.sock;
	}
 
	server {
		listen       192.168.0.250;
		server_name  192.168.0.250;
 
		#charset gb2312;
 
		access_log  /var/log/nginx/www_tech/www_tech_access_log combined;
		error_log   /var/log/nginx/www_tech/www_tech_error_log notice;
 
		location / {
			root        /infoware/www_tech/web;
			#allow  all;
			#fastcgi_pass    backend_dorm;
			fastcgi_pass    backend_www_tech;
			fastcgi_param   PATH_INFO       $fastcgi_script_name;
			fastcgi_param   REMOTE_ADDR     $remote_addr;
			fastcgi_pass_header             Authorization;
			include         fastcgi_params_django;
 
		}
 
		location /static {
			root            /infoware/www_tech/web;
		}
 
		error_page  404     http://192.168.0.250/static/error404.htm;
	}
--------------------------------------------------------------------

####################################################################

## 这里不能使用nginx默认的fastcgi_params,测试的时候发现所有的页面都会跳转到首页,而且也不报错

## 貌似是SCRIPT_NAME所引起的问题,而Django好像使用的是PATH_INFO,为了避免和其他的fcgi冲突,所以新建一个django专用的fastcgi_params_django

## Python2.4 + Django 0.96 版本不需要,因为Django 0.96 还不支持SCRIPT_NAME

## 可以查看django的django\core\handlers目录下的modpython.py 文件

vi /opt/nginx/conf/fastcgi_params_django
 
fastcgi_param  PATH_INFO          $fastcgi_script_name;
fastcgi_param  REQUEST_METHOD     $request_method;
fastcgi_param  QUERY_STRING       $query_string;
fastcgi_param  CONTENT_TYPE       $content_type;
fastcgi_param  CONTENT_LENGTH     $content_length;
fastcgi_param  SERVER_PROTOCOL    $server_protocol;
# fastcgi_param  SCRIPT_NAME        $fastcgi_script_name;
# fastcgi_param  REQUEST_URI        $request_uri;
# fastcgi_param  DOCUMENT_URI       $document_uri;
# fastcgi_param  DOCUMENT_ROOT      $document_root;
fastcgi_param  GATEWAY_INTERFACE  CGI/1.1;
fastcgi_param  SERVER_SOFTWARE    nginx/$nginx_version;
fastcgi_param  REMOTE_ADDR        $remote_addr;
fastcgi_param  REMOTE_PORT        $remote_port;
fastcgi_param  SERVER_ADDR        $server_addr;
fastcgi_param  SERVER_PORT        $server_port;
fastcgi_param  SERVER_NAME        $server_name;

2) fcgi

cd /infoware/_conf/www_tech/
 
vi start_www_tech.sh
------------------------------------------------------------------
#!/bin/bash
 
## start_www_tech.sh: start www_tech app in django fcgi mode
## ljzhou, 2010.08.26
 
## TODO
#  - executed under deamontools(in method=thread mode), to get
#    (1) controled env (2) 'web' uid (3) monitored process
#  - dormctl.sh script(nginx+django?), just as apachectl
 
APP_DIR="/infoware/www_tech/web"
CFG_DIR="/infoware/_conf/www_tech"
 
PYTHON="/usr/bin/python26"
DJANGO_ADMIN="/opt/python26/lib/python2.6/site-packages/django/bin/django-admin.py"
#PYTHON="/opt/python/bin/python2"
#DJANGO_ADMIN="/usr/lib/python2.4/site-packages/django/bin/django-admin.py"
 
############### no config below this line ##################
export PYTHONPATH="$PYTHONPATH:$CFG_DIR"
# mysettings.py in $CFG_DIR
#export DJANGO_SETTINGS_MODULE=mysettings
export DJANGO_SETTINGS_MODULE=settings
 
umask 027
 
## server: self daemonized, total num=20 with 10 threads each
## Help message:   help
## TCP socket  :   host=... port=...
PIDFILE="/var/run/fcgi/www_tech.pid"
if [ -f $PIDFILE ]; then
    kill `cat -- $PIDFILE`
    rm -f -- $PIDFILE
    sleep 3
fi
 
$PYTHON $DJANGO_ADMIN \
    runfcgi daemonize=true method=prefork \
    maxspare=5 minspare=2 maxchildren=10 maxrequests=500 \
    socket="/var/run/fcgi/www_tech.sock" pidfile=$PIDFILE \
    umask=000 debug=true \
    --pythonpath=$APP_DIR
 
# EOF: start_www_tech.sh
 
------------------------------------------------------------------
stop script like this , only Remark those:
 
#$PYTHON $DJANGO_ADMIN \
#    runfcgi daemonize=true method=prefork \
#    maxspare=5 minspare=2 maxchildren=10 maxrequests=500 \
#    socket=&"/var/run/fcgi/www_tech.sock" pidfile=$PIDFILE \
#    umask=000 debug=true \
#    --pythonpath=$APP_DIR
原文地址:https://www.cnblogs.com/lhj588/p/2389943.html