python+mod_python+django搭建详解

来源:互联网 发布:员工 福利 优化 通知 编辑:程序博客网 时间:2024/05/22 15:04

python+mod_python+django搭建详解_蓝邦商贸

<!--@page { margin: 2cm }P { margin-bottom: 0.21cm }A:link { color: #0000ff; so-language: zxx }-->

全手动安装开发环境:Apache+Python+PHP+Mysql支持Django框架

该开发环境方案均选用开源服务器及程序支持。
操作系统:Windows XP pro
服务器:Apache 2.2
程序:Python2.5、PHP5.2.6
数据库:Mysql5.0、SQLite3
框架:Django1.0
所需要程序:Python2.5安装文件,

Apache2.2.49for Windows x86安装文件:apache_2.2.9-win32-x86-openssl-0.9.8h-r2

一步步确认就可以了,我的安装目录是c:/python25。然后配置环境变量PATH属性,这样可以保证在任何文件夹下使用命令行工具执行python命令,方法是:开始菜单>右键我的电脑>属性>高级>环境变量,找到Path一项,加入“C:/Python25;”,重启电脑。
Tips: 注意安装时候要选择使用权限为All Users

2.Django1.0:

 

你应该会看到类似(1,0,final)之类的版本信息,恭喜强大好用的python framework django已经安装成功了!

下载以后直接安装就可以了,我的安装目录在c:/apache2.2
Tips:各个文件都安装在了c盘下是为了便于配置,没有特别要求。另同python安装要选择使用权限为All Users。

Important Note for Windows users, PLEASE READ!!!

1. This script does not attempt to modify Apache configuration,
you must do it manually:

Edit C:/Apache2.2/conf/httpd.conf,
find where other LoadModule lines are and add this:
LoadModule python_module modules/mod_python.so

2. Now test your installation using the instructions at this link:
http://www.modpython.org/live/current/doc-html/inst-testing.html

 

让apache支持php,此操作可以参考php5中文手册,解压php文件到C:/PHP,把php.ini配置好,修改path路径增加c:/php;,然后再打开httpd.conf写入

# 对 PHP 5 用这两行:
LoadModule php5_module "c:/php/php5apache2_2.dll"
AddType application/x-httpd-php .php
# 配置 php.ini 的路径
PHPIniDir "C:/php"

 

重启。OK,新建一个test.php在网站根目录下C:/Apache2.2/htdocs,用phpinfo()测试。
PS:为了让php支持cookie及session需要做两个修改在php.ini上,session.save_path = "c:/php/session_tmp"及session.use_cookies = 1,如果需要使用phpmyadmin设置mysql的话就一定要做这些设置了

6.MySQL5:

 

python django-admin.py startproject myproject

Alias /mysite C:/django/myproject #斜体部分没有测试成功

AddHandler mod_python .py
SetHandler python-program
PythonHandler django.core.handlers.modpython
SetEnv DJANGO_SETTINGS_MODULE myproject.settings
PythonDebug On
PythonPath "['C:/django'] + sys.path"
Options Indexes FollowSymLinks
AllowOverride None
Order allow,deny
Allow from all

1. 安装django
运行-cmd打开命令提示框
输入 cd C:/Python25指向python目录,否则不能执行python命令
python setup.py install

2. 先简单的测试一下。
命令提示符下,输入:python
然后输入import django
然后输入django.VERSION
我看到的是这样的:......
3. 在C:/Python25/Scripts可以看到django-admin.py了
4. 在c:/创建django目录,copy django-admin.py到该目录
执行 django-admin.py startproject myproject
创建了myproject
5. cd C:/python25 回到这里
执行 python c:/django/myproject/manage.py runserver
可以看到类似
Django version 0.9.1(SVN), using settings 'myproject.settings'
Development server is running at http://127.0.0.1:8000/
Quit the server with CONTROL-C or CTRL-BREAK.
说明一切正常启动
6. 访问 http://localhost:8000/mysite可以看到 welcome to Django了#加入8000和mysite没有成功,但当改为myproject时测试成功。
7. 在httpd.conf中写入
Alias /mysite C:/django/myproject

AddHandler mod_python .py
SetHandler python-program
PythonHandler django.core.handlers.modpython
SetEnv DJANGO_SETTINGS_MODULE myproject.settings
PythonDebug On
PythonPath "['C:/django'] + sys.path"
Options Indexes FollowSymLinks
AllowOverride None
Order allow,deny
Allow from all

即可以使用http://localhost/mysite/访问了 ##mysite没有测试成功 但myproject成功
8. hello world
在myproject目录创建 helloworld.py
from django.http import HttpResponse
def index(request):
return HttpResponse('Hello, World!')
然后打开urls.py修改urlpatterns,加入一个pattern
(r'^mysite/', 'myproject.helloworld.index'),
9. 重启apache,打开http://localhost/mysite/
看到hello,world

 

 

 



原创粉丝点击