Linux+apache+mysql+python+mod_python+Django
說明:系統rhel 5.3,默認安裝httpd、mysql,沒有安裝的,請下載安裝RPM包,刪除/etc/httpd/modules/mod_python.so,如果有的話。
一、安裝python
1
2
|
wget http: //www .python.org /ftp/python/2 .7.1 /Python-2 .7.1.tgz tar xfz mod_python-2.7.11.tgz <br> cd python-2.7.11<br> |
安裝
1
2
3
4
|
. /config --prefix= /usr/local/python/ make && make install ln -s /usr/local/python/bin/python2 .7 /usr/bin/ ln -s /usr/local/python/bin/python/usr/bin/ |
二、安裝setuptools
1
2
3
4
5
|
wget http: //pypi .python.org /packages/2 .7 /s/setuptools/setuptools-0 .6c11-py2.7.egg #md5=fe1f997bc722265116870bc7919059ea sh setuptools-0.6c11-py2.7.egg ldconfig #讓它生效 |
三、安裝 mysqldb模塊
1
2
3
4
5
6
|
wget http: //cdnetworks-kr-2 .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 python2.7 setup.py build # #ln -s /usr/local/python/bin/python2.7 /usr/bin/(注意建立連接) python2.7 setup.py install ##安裝 |
測試:
1
|
[root@localhost conf] # python2.7 |
1
2
3
4
5
|
Python 2.7.1 (r271:86832, Mar 21 2011, 10:13:38) [GCC 4.1.2 20080704 (Red Hat 4.1.2-44)] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> import MySQLdb >>> |
沒有提示說明是正確的。
四、安裝mod_python
1
|
wget http: //archive .apache.org /dist/httpd/modpython/mod_python-3 .3.0b.tgz |
安裝前安裝apr-devel-1.2.7-11.el5_5.2.i386.rpm、apr-util-devel-1.2.7-7.SEL5_3.2.i386.rpm、httpd-devel-2.4.el5.centos.i386.rpm,因為要動態加入python模塊,要不然找不到apxs
1
2
3
4
|
tar xvf mod_python-3.3.0b.tgz cd mod_python-3.3.0b . /configure --with-apxs= /usr/sbin/apxs --with-python= /usr/local/python/bin/python ###(apache支持python) make && make install |
注意
LoadModule python_module modules/mod_python.so這個不用添加,因為在/etc/httpd/conf.d/python.conf 已經配置好
7ervice httpd restart (重啟下apache)
測試:
1
|
[root@localhost conf] # python |
1
2
3
4
5
|
Python 2.7.1 (r271:86832, Mar 21 2011, 10:13:38) [GCC 4.1.2 20080704 (Red Hat 4.1.2-44)] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> import mod_python >>> |
五、安裝Django
1
2
3
4
|
wget http: //www .djangoproject.com /download/1 .2.5 /tarball/ tar xfz Django-1.2.5. tar .gz cd Django-1.2.5 python2.7 setup.py install |
測試:
1
|
[root@localhost conf] # python |
1
2
3
4
5
|
Python 2.7.1 (r271:86832, Mar 21 2011, 10:13:38) [GCC 4.1.2 20080704 (Red Hat 4.1.2-44)] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> import django >>> |
六、測試全部
1
2
3
4
5
|
mkdr /www cd /www . /usr/local/python/bin/django-admin .py startproject mytest mytest python manage.py runserver 0.0.0.0:8000 |
效果如下:
PS:關于mod_python
mod_python是apache組織的一個項目,通過它,可以開發psp或cgi,mod_python功能強大,速度快,是非常優秀的web開發工具。
Mod_python起源于一個被稱為Httpdapy(1997)的項目。很長時間以來, Httpdapy并沒有被稱作mod_python,因為Httpdapy不是專門用于Apache的。 Httpdapy被設計成跨平臺的,實際上最初是為Netscape server寫的(那個時候被稱為Nsapy)(1997)
這個Httpdapy的README文件的摘要,很好的闡述了在HTTP server中嵌入Python所帶來的挑戰和解決方案。
Apache分階段的處理請求(比方說:讀取請求,解析header, 檢查存取路徑,等等)。這些階段能被稱為"處理器"(handler)的函數實現。傳統上, "處理器"是由C語言編寫,并編譯成Apache的模塊。Mod_python提供了一個通過Python寫的Apache處理器的來擴展Apache功能的方法。關于Apache請求處理過程的詳盡描述,請參閱 Apache API Notes, 也可以參閱 Mod_python - Integrating Python with Apache。
為了輕松地從CGI移植,一個標準的mod_python處理器提供了模擬的CGI環境,允許用戶在不對代碼做任何修改的情況下,使遺留的腳本運行在mod_python下(大多數情況)。
mod_python的一個最主要優點就是在性能上超越傳統CGI。下面是一個非常粗略的測試。該測試是在一臺運行Red Hat Linux 7.3的奔騰1.2G的機器上完成的。Ab 用來對4種腳本進行測試,所有的都是從標準cgi模塊輸入(因為這是一個典型Python cgi腳本開始的方式), 然后輸出一個簡單的單詞"Hello!"。這個結果是基于10000次并發為1的請求。
Standard CGI: 23 requests/s
Mod_python cgihandler: 385 requests/s
Mod_python publisher: 476 requests/s
Mod_python handler: 1203 requests/s