Linux apache mysql python mod_python Django
說明:系統rhel 5.3,默認安裝httpd、mysql,沒有安裝的,請下載安裝RPM包,刪除/etc/httpd/modules/mod_python.so,如果有的話.
一、安裝python
1 wget http://www.python.org/ftp/python/2.7.1/Python-2.7.1.tgz
2 tar xfz mod_python-2.7.11.tgz
3 cd python-2.7.11
安裝./config --prefix=/usr/local/python/
make && make install
4 ln -s /usr/local/python/bin/python2.7 /usr/bin/
5 ln -s /usr/local/python/bin/python/usr/bin/
二、安裝setuptools
wget http://pypi.python.org/packages/2.7/s/setuptools/setuptools-0.6c11-py2.7.egg#md5=fe1f997bc722265116870bc7919059ea
1 sh setuptools-0.6c11-py2.7.egg
2 ldconfig #讓它生效
三、安裝 mysqldb模塊
wget http://cdnetworks-kr-2.dl.sourceforge.net/project/mysql-python/mysql-python/1.2.3/MySQL-python-1.2.3.tar.gz
1 tar zxvf MySQL-python-1.2.3.tar.gz
2 cd MySQL-python
3python2.7 setup.py build
# #ln -s /usr/local/python/bin/python2.7 /usr/bin/(注意建立連接)
4 python2.7 setup.py install ##安裝
5測試:
[root@localhost conf]# python2.7
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
wget http://archive.apache.org/dist/httpd/modpython/mod_python-3.3.0b.tgz
1 安裝前安裝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.2.4.el5.centos.i386.rpm,因為要動態加入python模塊,要不然找不到
2 tar xvf mod_python-3.3.0b.tgz
3cd mod_python-3.3.0b
4 ./configure --with-apxs=/usr/sbin/apxs --with-python=/usr/local/python/bin/python ###(apache支持python)
5 make && make install
6 注意
LoadModule python_module modules/mod_python.so這個不用添加,因為在/etc/httpd/conf.d/python.conf 已經配置好
7ervice httpd restart (重啟下apache)
[root@localhost conf]# python
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
wget http://www.djangoproject.com/download/1.2.5/tarball/
1 tar xfz Django-1.2.5.tar.gz
2 cd Django-1.2.5
3 python2.7 setup.py install
4測試:
[root@localhost conf]# python
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.
六、測試全部
1 mkdr /www
2cd /www
3/usr/local/python/bin/django-admin.py startproject mytest
4 cd mytest
5python manage.py runserver 0.0.0.0:8000
6 效果如下:
瀏覽器訪問:
7 CRTL C退出
七、配置apache虛擬目錄(虛擬目錄有兩種,一種是基於目錄,另一種是基於域名)
1基於虛擬目錄
vi /etc/httpd/conf/httpd.conf
添加以下配置文件:
Alias /python "/www"
<Directory /www>
AllowOverride FileInfo
AddHandler mod_python .py
PythonDebug On
</Directory>
##service httpd restart(記得重啟)##
在/www目錄下面的test.py寫入:
from mod_python import apache
def handler(req):
req.write("Hello World!")
return apache.OK
在瀏覽器訪問:
2虛擬主機基於域名訪問:
1 vi /etc/httpd/conf/httpd.conf
NameVirtualHost *:80###去掉註釋
添加如下配置:
<VirtualHost *:80>
ServerAdmin admin@zhnews.com
DocumentRoot /www
ServerName www.test.com
<Directory "/www">
AllowOverride FileInfo
</VirtualHost>
##service httpd restart(記得重啟)##
##修改下本地hosts文件
瀏覽器訪問:
到此,配置成功.
#######本人在linuxtone.org也發過#######
本文出自 「游造技術博客」 博客,請務必保留此出處http://youzao.blog.51cto.com/3946111/741226
[火星人 ] Linux apache mysql python mod_python Django已經有235次圍觀