ifuleyou 发表于 2018-9-26 13:19:16

Linux+apache+mysql+python+mod_python+Django

  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测试:
# python2.7
  Python 2.7.1 (r271:86832, Mar 21 2011, 10:13:38)
   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模块,要不然找不到apxs
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)
8 测试:
# python
  Python 2.7.1 (r271:86832, Mar 21 2011, 10:13:38)
   on linux2
  Type "help", "copyright", "credits" or "license" for more information.
  >>> import mod_python
  >>>

五、安装Django
wgethttp://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.pyinstall
  4测试:
# python
  Python 2.7.1 (r271:86832, Mar 21 2011, 10:13:38)
   on linux2
  Type "help", "copyright", "credits" or "license" for more information.
  >>> import django
  >>>

六、测试全部
  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"

AllowOverride FileInfo
AddHandler mod_python .py
PythonHandler test
PythonDebug On

##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###去掉注释
      添加如下配置:
  
  ServerAdmin admin@zhnews.com
  DocumentRoot /www
  ServerName www.test.com
  
  AllowOverride FileInfo
  AddHandler mod_python .py
  PythonHandler test
  PythonDebug On
  Options Indexes FollowSymLinks MultiViews
  AllowOverride all
  Order allow,deny
  Allow from all
  

##service httpd restart(记得重启)##
##修改下本地hosts文件IP映射成www.test.com##
浏览器访问:
   

到此,配置成功。
#######本人在linuxtone.org也发过#######

页: [1]
查看完整版本: Linux+apache+mysql+python+mod_python+Django