ubuntu11.04 下 Django开发环境配置小结


昨天晚上加今儿早的时间 终把Django的开发环境捣鼓成了,窃喜一下,呵呵。

步骤如下:

1. 我没有去get python,因为系统已经自带了python2.7,够我用了。

2. get django and install

google Django download , 我下载的是Django-1.3.1.tar.gz。在此请恕不提供下载地址了。

解压安装:tarxzvf Django-1.3.1.tar.gz

   cd /home/ethan/Django-1.3.1 (我的用户是ethan, 但我已经切换到了root, su一下即可)

   解释:su====switch user

  python setup.py install   (不用sudo了,已在root下)

3.install mod_python

apt-get install libapache2-mod-python (可以使用apt-cache search mod_python查询一下)

root@ubuntu:~# apt-cache search mod_python
libapache2
-mod-python - Python-embedding module for Apache 2
libapache2
-mod-python-doc - Python-embedding module for Apache 2 -
documentation
libapache2
-mod-wsgi - Python WSGI adapter module for Apache
python
-albatross - Toolkit for Stateful Web Applications
python
-albatross-doc - documentation for the Albatross Web Toolkit
python
-apache-openid - OpenID consumer module for Apache
python
-cherrypy3 - Python web development framework - version 3
python
-myghtyutils - Set of utility classes used by Myghty
templating

4. install python-mysql

   下载mysql for linux , 我在WIN7上用LeapFTP上传到/home/ethan/java文件中,具体联网设置可参考上一篇博文。

   apt-get install python-mysqldb 这是个驱动

5. 测试一下apache,是否安装成功: /etc/init.d/apache2 start

6. 使用django:

cd /usr 

mkdir www

cd www

django-admin.py startproject hello

这样在/usr/www/目录下就生成了一个hello的工程目录hello目录下也有4个文件
__init__.py  (这个不用多少 说明这个目录是个python包)
mamage.py (提供了简单的命令行工具,可以使用多种方式和django进行交互)
settings.py (这事django工程的一个配置文件)
urls.py (url的映射文件)

   

来一个经典的hello world程序

1 在/usr/www/hello目录下新建一个hw.py的文件 内容如下

    from django.http import HttpResponse

def index(request):
return HttpResponse("hello world")

2 配置url映射 编辑urls.py
首先导入 import helloworld
接着在urlpatterns = patterns('',)中加入
(r'^$',helloworld.index)
完整的配置文件如下

    from django.conf.urls.defaults import
*
import hw
# Uncomment the next two lines to enable the admin:
# from django.contrib import admin
# admin.autodiscover()

urlpatterns
= patterns('',

(r'^$',hw.index),

)

运行 先使用django自带的服务器测试
cd /usr/www/hello
python manage.py runserver
你可以看到如下的信息
Validating models...
0 errors found

Django version 1.2.1, using settings 'hello.settings'

Development server is running at http://127.0.0.1:8000/
Quit the server with CONTROL-C.
浏览器打开http://127.0.0.1:8000/ 看到了hello world 的字样 说明这个hello world 程序已经没问题了

最后就是 把这个工程部署到apache上  一共两步

1切换到/etc/apache2/sites-available目录下
拷贝一份default文件 并重命名为hello
编辑hello文件 将其中的/var/www  替换成 /usr/www 如果你的工程是建在/var/www/ 下 这可以跳过
激活hello


 运行命令:a2ensite hello 

命令完成之后会要你运行/etc/init.d/apache2 reload 重新激活配置
2 编辑 /etc/apache2/httpd.conf文件 发现这个文件是一个空文件 啥也没有

<location "/">
SetHandler python-program
PythonHandler django.core.handlers.modpython
SetEnv DJANGO_SETTINGS_MODULE hello.settings
PythonPath "['/usr/www'] + sys.path"
</location>

有写文章中说要加入在 此文件中 加 这样LoadModule python_module modules/mod_python.so  的配置

但是你把apache运行起来的时候 apache会告诉你mod_python已经加载了 所以说这个是不需要加的

这一步就是添加一个虚拟站点 和在tomcat 的server.xml中配置Host是同一个道理

到此配置过程全部结束
运行 /etc/init.d/apache2 restart

浏览http://127.0.1.1/ (在此我没有去修改servername:localhost)
又看到了hello world
大功告成!

在此要特别感谢http://iyouf.info/install-django-and-deploy-on-apache.html的分享和帮助!!!
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值