Deng Yi's profileThought WalkerPhotosBlogListsMore Tools Help

Blog


    April 22

    Ubuntu 7.04 配置Django + Apache开发环境

    先说一下本文档基于的安装环境:

    Ubuntu 7.04 Feisty Fawn
    Apache2 2.2.3
    Python 2.5
    Django 0.96

    这里假设我们已经拥有了一个全新的Ubuntu 7.04 的 Server 版安装环境,下面分步讲解安装配置各模块过程:

    1. 安装Apache2和mod-python
      利用Ubuntu的apt-get安装二进制包非常方便,不过如果不是使用root用户的话,请使用sudo命令:

      sudo apt-get install apache2

      同时,安装mod-python模块:

      sudo apt-get install libapache2-mod-python

      启动apache2

      sudo /etc/init.d/apache2 start

      如果发现有这样的报错:
      Could not determine the server's fully qualified domain name, using 127.0.0.1 for ServerName
      原因多半是因为没有配置ServerName:

      sudo vi /etc/apache2/apache2.conf

      打开apache2.conf文件,在末尾加入ServerName localhost,保存退出

      sudo /etc/init.d/apache2 restart

      重新引导启动apache2就可以了。具体可以参见此文章
    2. 安装Django

      最新版本的Ubuntu已经自带了Django的二进制包,不过,这里讲解如何从源码编译安装。
      Ubuntu已经缺省安装好了Python 2.5.1的rc0,所以这里无需再安装Python,不过,ez-setup.py并不是最新的,需要更新:

      cd /usr/src
      wget -c http://peak.telecommunity.com/dist/ez_setup.py
      sudo python ez_setup.py

      下载Django 0.96

      wget -c http://media.djangoproject.com/releases/0.96/Django-0.96.tar.gz
      tar zxvf Django-0.96.tar.gz
      cd Django-0.96
      sudo python setup.py install

      执行之后没有错误,表示Django已经安装完成。
      在Shell命令行中输入

      python (回车)

      >>>import django (回车)

      如果没有报错则表示django安装成功。
    3. 配置基础Django环境

      安装成功以后的Django实际文件存在于/usr/lib/python2.5/site-packages/django,我们将使用Django的django-admin.py(在django/bin目录中)来创建我们的Django项目,Django安装成功后,会自动在/usr/bin中建立一个django-admin.py的一个符号链接,所以无需指定路径就可以使用这个脚本文件了。

      创建项目djangotest,缺省apache2的文档根目录是/var/www:

      cd /var/www
      sudo python django-admin.py startproject djangotest

      这样会在/var/www下建立一个djangotest的目录,里面包含下面的几个文件:

      __inti__.py
      manage.py
      settings.py
      uls.py

      对于初始设置来说,并不需要更改这些文件,至于这些文件的具体用途,可以查阅相关的Django文档。
      这时已经可以启动Django自带的Web Server来测试是否创建成功了:

      sudo python manage.py runserver

      出现类似的提示表示启动成功:

      Validating models...
      0 errors found.

      Starting server on port 8000 with settings module 'newtest.settings'.
      Go to http://127.0.0.1:8000/ for Django.
      Quit the server with CONTROL-C (Unix) or CTRL-BREAK (Windows).

      但是此Server只接受来自与本地的访问,所有外来的访问请求都不响应,所以只适合于本地开发用途,如果使用虚拟机或者有专门的机器来安装Django环境,则不能使用了,对于生产环境则更不适用,所以需要配置apache2以便和Django良好合作。

    4. 配置Apache2以便与Django一同工作

      配置/etc/apache2/site-available/default

      sudo vi /etc/apach2/site-available/default

      在<VirtualHost>中增加一个<Directory>段(Apache2的配置详见apache.org),内容如下:

      <Directory /var/www>
      SetHandler python-program
      PythonPath "['/var/www'] + sys.path"
      PythonHandler django.core.handlers.modpython
      SetEnv DJANGO_SETTINGS_MODULE djangotest.settings
      PythonDebug On
      </Directory>

      这里有几点需要注意:
      1)PythonPath一定要声明在前,因为它的作用是要将我们的项目目录加入到sys.path中;
      2)PythonPath里面 [] 中的目录一定要是我们项目的上一级目录(搞不懂为什么是这样);
      3)SetEnv中那个settings的名字就是我们的项目名+.settings。

      上述配置完后,

      sudo /etc/init.d/apache2 restart

      重新启动apache2,无异常成功。

      在客户机上的浏览器中输入 http://服务器IP/djangotest,出现:

      It worked!
      Congratulations on your first Django-powered page.

      这样的提示页面,则表示Django安装成功!

    Comments

    Please wait...
    Sorry, the comment you entered is too long. Please shorten it.
    You didn't enter anything. Please try again.
    Sorry, we can't add your comment right now. Please try again later.
    To add a comment, you need permission from your parent. Ask for permission
    Your parent has turned off comments.
    Sorry, we can't delete your comment right now. Please try again later.
    You've exceeded the maximum number of comments that can be left in one day. Please try again in 24 hours.
    Your account has had the ability to leave comments disabled because our systems indicate that you may be spamming other users. If you believe that your account has been disabled in error please contact Windows Live support.
    Complete the security check below to finish leaving your comment.
    The characters you type in the security check must match the characters in the picture or audio.

    To add a comment, sign in with your Windows Live ID (if you use Hotmail, Messenger, or Xbox LIVE, you have a Windows Live ID). Sign in


    Don't have a Windows Live ID? Sign up

    Trackbacks

    The trackback URL for this entry is:
    http://iamdengyi.spaces.live.com/blog/cns!81672FE4A3A2AC93!897.trak
    Weblogs that reference this entry
    • None