我们首先需要安装Python和Django环境,如下:
- #!/bin/bash
- tar zxvf Python-2.6.5.tgz
- cd Python-2.6.5
- ./configure --prefix=/usr --enable-shared
- make && make install
- cd ..
- ldconfig -v
- tar zxvf setuptools-0.6c11.tar.gz
- cd setuptools-0.6c11
- python setup.py build && python setup.py install
- cd ..
- tar zxvf MySQL-python-1.2.3.tar.gz
- cd MySQL-python-1.2.3
- sed -i 's/threadsafe = True/threadsafe = False/' site.cfg
- sed -i 's/#mysql_config = \/usr\/local\/bin\/mysql_config/mysql_config = \/usr\/local\/mysql\/bin\/mysql_config/' site.cfg
- python setup.py build && python setup.py install
- cd ..
- tar zxvf pymssql-1.0.2.tar.gz
- cd pymssql-1.0.2
- python setup.py build && python setup.py install
- cd ..
- tar zxvf Django-1.3.1.tar.gz
- cd Django-1.3.1
- python setup.py build && python setup.py install
- cd ..
这里我们选择使用mod_uwsgi作为Apache对Python的支持,而不是选用mod_python,这两种方式在性能上有什么不同,大家可以测试下。
- tar zxvf mod_wsgi-3.3.tar.gz
- cd mod_wsgi-3.3
- ./configure --with-apxs=/usr/local/apache/bin/apxs --with-python=/usr/bin/python
- make && make install
- cd ..
修改httpd.conf,添加mod_uwsgi模块,增加对Python的支持。
- LoadModule wsgi_module modules/mod_wsgi.so
- WSGIScriptAlias /py "/usr/local/apache/htdocs/py/py.wsgi"
- <Directory "/usr/local/apache/htdocs/py">
- Order Deny,Allow
- Allow from all
- </Directory>
- <IfModule dir_module>
- DirectoryIndex index.html index.php index.py
- </IfModule>
关于wsgi文件的写法,根据具体环境的不同,可能会有写差别,这里给出一个较为通用的写法,如下:
- import os, sys
- from os.path import abspath, dirname, join
- from django.core.handlers.wsgi import WSGIHandler
- sys.path.insert(0, abspath(join(dirname(__file__), "./")))
- sys.path.insert(0, abspath(join(dirname(__file__), "../")))
- sys.stdout = sys.stderr
- os.environ["DJANGO_SETTINGS_MODULE"] = "py.settings"
- os.environ["PYTHON_EGG_CACHE"] = "/tmp"
- os.environ["LANG"] = "zh_CN.utf8"
- application = WSGIHandler()
如果配置成功,则会出现如下效果:

