参考:http://tech.foolpig.com/2010/08/30/python-django-nginx/

环境:Ubuntu 10.04 32bit,自带的有python2.6,已经用LNMP安装了nginx 0.75版本。

现在需要安装的有uwsgi,django,MySQL-python这几个插件。

1.安装uwsgi

uwsgi需要python-dev支持,否则会报错“

In file included from plugins/python/python_plugin.c:1:0:

plugins/python/uwsgi_python.h:2:20: fatal error: Python.h: No such file or directory

”;

需安装python-dev(ubuntu下出现依赖问题,可使用aptitude install python-dev)

wget http://projects.unbit.it/downloads/uwsgi-0.9.7.2.tar.gz  
tar -zxvf uwsgi-0.9.7.2.tar.gz  
cd uwsgi-0.9.7.2
make  
cp uwsgi /usr/bin

2.安装MySQL-python

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.3.tar.gz
cd MySQL-python-1.2.3
python setup.py build 
python setup.py install 
cd .. 
#test 
[root@devel photo_uw]# python 
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 
>>>

3.安装Django 1.2.1

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
python setup.py install 
#test 
[root@devel photo_uw]# python 
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'

4.安装Memcache;

Memcache依赖libevent这个库用于Socket的处理,所以还需要安装libevent,一般的服务器上都会默认安装的,我查看一下,

[/root]ls -al /usr/lib | grep libevent 
lrwxrwxrwx  1 root root       21 Apr  4 21:38 libevent-1.4.so.2 -> libevent-1.4.so.2.1.3
-rw-r--r--  1 root root    91768 Dec  6  2009 libevent-1.4.so.2.1.3
-rw-r--r--  1 root root   114738 Dec  6  2009 libevent.a 
lrwxrwxrwx  1 root root       21 Apr  4 21:38 libevent.so -> libevent-1.4.so.2.1.3
lrwxrwxrwx  1 root root       26 Apr  4 21:38 libevent_core-1.4.so.2 -> libevent_core-1.4.so.2.1.3
-rw-r--r--  1 root root    34268 Dec  6  2009 libevent_core-1.4.so.2.1.3
-rw-r--r--  1 root root    40116 Dec  6  2009 libevent_core.a 
lrwxrwxrwx  1 root root       26 Apr  4 21:38 libevent_core.so -> libevent_core-1.4.so.2.1.3
lrwxrwxrwx  1 root root       27 Apr  4 21:38 libevent_extra-1.4.so.2 -> libevent_extra-1.4.so.2.1.3
-rw-r--r--  1 root root    75432 Dec  6  2009 libevent_extra-1.4.so.2.1.3
-rw-r--r--  1 root root    89786 Dec  6  2009 libevent_extra.a 
lrwxrwxrwx  1 root root       27 Apr  4 21:38 libevent_extra.so -> libevent_extra-1.4.so.2.1.3
[/root]

没问题libevent已安装,接下来就安装Memcache,首先下载,然后解压,配置,编译,安装;

#下载,现在默认的最新版本为1.4.5  
wget http://memcached.googlecode.com/files/memcached-1.4.5.tar.gz  
#解压  
tar zxvf memcached-1.4.5.tar.gz  
#进入目录,配置  
cd memcached-1.4.5
./configure -with-libevent=/usr  
#编译  
make  
make install  
#测试是否安装成功 
[/root/memcached-1.4.5]ls -al /usr/local/bin/mem*
-rwxr-xr-x 1 root root 220367 Apr 24 12:33 /usr/local/bin/memcached 
[/root/memcached-1.4.5] 
#启动 
启动Memcache的服务器端: 
memcached -d -m 64 -u root -l 我的ip(需替换) -p 11211 -c 256 -P /tmp/memcached.pid 
参数说明: 
-d选项是启动一个守护进程, 
-m是分配给Memcache使用的内存数量,单位是MB,我这里是10MB, 
-u是运行Memcache的用户,我这里是root, 
-l是监听的服务器IP地址,如果有多个地址的话,我这里指定了服务器的IP地址202.207.177.177, 
-p是设置Memcache监听的端口,我这里设置了11211,最好是1024以上的端口, 
-c选项是最大运行的并发连接数,默认是1024,我这里设置了256,按照你服务器的负载量来设定, 
-P是设置保存Memcache的pid文件,我这里是保存在 /tmp/memcached.pid,

安装python-memcached

下载地址:http://www.tummy.com/Community/software/python-memcached/

python-memcached库其实就是一个py文件而已;

[/root]wget ftp://ftp.tummy.com/pub/python-memcached/python-memcached-latest.tar.gz 
[/root]tar zxvf python-memcached-latest.tar.gz 
[/root]cd python-memcached-1.47
[/root/python-memcached-1.47]python setup.py install 
[/root]python 
Python 2.6.6 (r266:84292, Sep 15 2010, 15:52:39)  
[GCC 4.4.5] on linux2 
Type "help", "copyright", "credits" or "license" for more information. 
>>> import memcache 
>>>

window下安装Memcache的方法不再此处说明了,在以后的文章中说明;

配置

如果在本地开发需要安装windows版本的python-mysql包;

这儿有新版的包:MySQL-python-1.2.3c1.win32-py2.6.exe能解决问题安装中遇到的问题

在windows平台下需要两个dll文件,

解决方法:下载libmmd.dll(附件)和libguide40.dll(附件)两个dll文件并复制到python安装目录的Lib\site-packages下。

下载地址:http://gs.quke.org/files/python/django/python_mysql_dll.zip

http://stackoverflow.com/questions/645943/mysql-for-python-in-windows

参考以下的文章:

http://fendou.org/2010/06/17/install-python26-mysql-python-on-windows/

http://blogold.chinaunix.net/u3/101606/showart_2007031.html

http://hi.baidu.com/feng2211/blog/item/8b86b6d9816a3f2710df9b79.html

http://i.19830102.com/archives/164