欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  网络运营

Apache上部署Django步骤详细介绍

程序员文章站 2023-11-02 11:06:58
apache上部署django 目前,apache和mod_python是在生产服务器上部署django的最健壮搭配。mod_python 是一个在apache中嵌入...

apache上部署django

目前,apache和mod_python是在生产服务器上部署django的最健壮搭配。mod_python 是一个在apache中嵌入python的apache插件,它在服务器启动时将python代码加载到内存中。

django 需要apaceh 2.x 和mod_python 3.x支持。

apache的配置参见:http://www.djangoproject.com/r/apache/docs/

 使用mod_python部署

1.为了配置基于 mod_python 的 django,首先要安装有可用的 mod_python 模块的 apache。

2.然后应该有一个 loadmodule 指令在 apache 配置文件中。 它看起来就像是这样:

loadmodule python_module /usr/lib/apache2/modules/mod_python.so

3.配置apache,用来定位请求url到django应用:

<virtualhost *:80>
    servername www.example.com

    <location "/mysite1">
      sethandler python‐program
      pythonhandler django.core.handlers.modpython
      setenv django_settings_module mysite1.settings
      pythonautoreload off
      pythondebug off
      pythonpath "['/var/www/html/mysite1'] + sys.path"
      pythoninterpreter mysite1
    </location>

    <location "/mysite2">
      sethandler python‐program
      pythonhandler django.core.handlers.modpython
      setenv django_settings_module mysite2.settings
      pythonautoreload off
      pythondebug off
      pythonpath "['/var/www/html/mysite2'] + sys.path"
      pythoninterpreter mysite2
    </location>

    [......]
</virtualhost>

它告诉 apache,任何在 / mysite这个路径之后的 url 都使用 django 的 mod_python 来处理。 它 将django_settings_module 的值传递过去,使得 mod_python 知道这时应该使用哪个配置。

查看 mod_python 文档获得详细的指令列表。

4.重启apache,查看http://www.example.com/mysite:

/etc/init.d/apache2 restart

使用mod_wsgi部署

1.下载安装 mod_wsgi 模块,生成mod_wsgi.so和wsgi.conf

2.在配置中加载模块:

loadmodule python_module /usr/lib/apache2/modules/mod_wsgi.so

3.修改apache配置文件httpd.conf

<virtualhost *:80>
    servername www.example
    documentroot /var/www/html/mysite
    wsgiscriptalias / /var/www/html/mysite/apache/django.wsgi
    <directory />
        order deny,allow
        allow from all
    </directory>
    <directory /apache>
        allow from all
    </directory>
</virtualhost>

4.创建并配置wsgi的配置文件:

# filename:mysite.apache.django.wsgi
import os, sys

#calculate the path based on the location of the wsgi script.
apache_configuration= os.path.dirname(__file__)
project = os.path.dirname(apache_configuration)
workspace = os.path.dirname(project)
sys.path.append(workspace)
os.environ['django_settings_module'] = 'mysite.settings'
os.environ['python_egg_cache'] = '/tmp'

import django.core.handlers.wsgi
application = django.core.handlers.wsgi.wsgihandler()
print >> sys.stderr, sys.path  
shell>chmod a+x django.wsgi

5.修改django项目配置文件settings.py:

databases = {
  'default': {
    'engine': 'django.db.backends.mysql',
    'name': 'mysite',           
    'user': 'admin',           
    'password': 'admin123',         
    'host': '127.0.0.1',           
    'port': '3306', }
}

template_dirs = (
  '/var/www/html/mysite/templates',
)

6.重启apache,访问http://www.example.com/mysite

/etc/init.d/apache2 restart

感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!