当前位置: 首页 > 网络应用技术

如何在Apache上启动Django(2023年的最新答案)

时间:2023-03-07 20:13:08 网络应用技术

  简介:许多朋友询问了如何在Apache上启动Apache的Django。本文的首席CTO注释将为您提供详细的答案,以供您参考。我希望这对每个人都会有所帮助!让我们一起看看!

  目前,Apache和MOD_PYTHON是在生产服务器上部署Django的最强大匹配。

  MOD_PYTHON是Apache插件,该插件嵌入了Apache中的Python中,在服务器启动时将Python代码加载到内存。

  Django需要Apaceh 2.x和mod_python 3.x支持。

  请参阅Apache的配置:

  使用mod_python部署

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

  2.然后在Apache配置文件中应该有一个LoadModule指令。看起来像这样:

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

  3.配置Apache将请求URL定位到Django应用程序:

  VirtualHost *:80

  服务器名称

  本地“/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”

  Pythonininterpreter mysite1

  /当地的

  本地“/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”

  Pythonininterpreter mysite2

  /当地的

  [......]

  /VirtualHost

  它告诉Apache / mySite路径的URL使用django的mod_python处理它。它传递了django_settings_module的值,因此mod_python知道此时应使用哪种配置。

  检查mod_python文档以获取详细的说明列表。

  4.重新启动apache并查看:“ target =“ _ blank”:

  /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

  服务器名称

  DocumentRoot/var/www/html/mysite

  wsgiscriptalias // var/www/html/mysite/apache/django.wsgi

  目录 /

  命令拒绝,允许

  从所有人那里允许

  /目录

  目录 /apache

  从所有人那里允许

  /目录

  /VirtualHost

  4.创建和配置WSGI配置文件:

  #文件名:mysite.apache.django.wsgi

  导入OS,系统

  #calculcul callcul the Lath基于WSGI脚本的位置。

  apache_configuration = os.path.dirname(__file__)

  project = os.path.dirname(apache_configuration)

  workspace = os.path.dirname(project)

  sys.path.append(工作区)

  os.environ ['django_settings_module'] ='mysite.settings'

  os.environ ['python_egg_cache'] ='/tmp'

  导入django.core.handlers.wsgi

  应用程序= django.core.handlers.wsgi.wsgihandler()

  打印sys.stderr,ss.path

  shellchmod a+x django.wsgi

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

  数据库= {

  '默认': {

  '引擎':'django.db.backends.mysql',

  “名字”:“ mypite”,

  “用户”:“ admin”,

  '密码':'admin123',

  “主机”:'127.0.0.1',

  '端口':'3306',},}

  }

  template_dirs =((

  '/var/www/html/mysite/模板',,

  治愈

  6.重新启动Apache,访问

  /etc/init.d/apache2 restart

  如果解决问题,请采用它!

  如果没有解决,请继续询问!

  mod_wsgi.so在Windows平台下最好。如果您下载的版本是错误的,那么您当然不能链接。如果可能的话,您可以使用Apache工具来编译MOD_WSGI,应该没关系。

  使用mod_python部署

  1.为了基于mod_python配置django,首先

  apache。

  2.然后在Apache配置文件中应该有一个LoadModule指令。看起来像这样:

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

  3.配置Apache将请求URL定位到Django应用程序:

  VirtualHost *:80

  服务器名称

  本地“/python1”

  Sethandler Python -Program

  pythonhandler django.core.handlers.modpython

  setEnv django_settings_module python1.settings

  Pythonautoreload Off

  pythondebug off

  pythonpath“ ['/var/www/html/python1'] + sys.path”

  Pythonininterpreter Python1

  /当地的

  本地“/python2”

  Sethandler Python -Program

  pythonhandler django.core.handlers.modpython

  setEnv django_settings_module python2.settings

  Pythonautoreload Off

  pythondebug off

  pythonpath“ ['/var/www/html/python2'] + sys.path”

  Pythonininterpreter Python2

  /当地的

  [......]

  /VirtualHost

  它告诉Apache, / python之后的任何URL都使用Django的mod_python对其进行处理。

  它传递了django_settings_module的值,以便mod_python知道此时应使用哪种配置。

  检查mod_python文档以获取详细的说明列表。

  4.重新启动apache并查看:“ target =“ _ blank”:

  /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

  服务器名称

  document root/var/www/html/python

  wsgiscriptalias // var/www/html/python/apache/django.wsgi

  目录 /

  命令拒绝,允许

  从所有人那里允许

  /目录

  目录 /apache

  从所有人那里允许

  /目录

  /VirtualHost

  4.创建和配置WSGI配置文件:

  #文件名:python.apache.django.wsgi

  导入OS,系统

  #calculcul callcul the Lath基于WSGI脚本的位置。

  apache_configuration = os.path.dirname(__file__)

  project = os.path.dirname(apache_configuration)

  workspace = os.path.dirname(project)

  sys.path.append(工作区)

  os.environ ['django_settings_module'] ='python.settings'

  os.environ ['python_egg_cache'] ='/tmp'

  导入django.core.handlers.wsgi

  应用程序= django.core.handlers.wsgi.wsgihandler()

  打印sys.stderr,ss.path

  shellchmod a+x django.wsgi

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

  数据库= {

  '默认': {

  '引擎':'django.db.backends.mysql',

  “名字”:'Python',

  “用户”:“ admin”,

  '密码':'admin123',

  “主机”:'127.0.0.1',

  '端口':'3306',},}

  }

  template_dirs =((

  '/var/www/html/python/pyplates',,

  治愈

  6.重新启动Apache,访问

  /etc/init.d/apache2 restart

  我是Linux主持人,我在本地运行Django

  1在终端输入Django项目的文件夹是Manage.py的目录

  2然后运行python manage.py runserver

  你可以跑

  只是参考:

  Apache是世界上最有用的开源Web服务器软件。现在,我将介绍如何在Ubuntu 14.04上安装和配置Apache。

  工具/成分

  Ubuntu 14.04

  方法/步骤

  命令行安装apache

  打开“终端窗口”,然后输入“ seno apt-get install apache2” -enter-enter“ root用户密码” -enter-enter“ y” - 输入,完成安装完成

  默认网站根目录的路径

  安装Apache完成后,默认网站根目录为“/var/www/html”。在终端窗口 - 输入中输入“ ls/var/www/html”。

  Apache的第一个配置文件apache2.conf路径

  在终端窗口 - 输入中输入“ ls /etc /apache2”,其中有一个“ apache2.conf”配置文件。

  Apache的第二个配置文件000-default.conf路径

  在终端窗口 - 输入-A“ 000-default.conf”配置文件中输入“ LS/ETC/Apache2/stites-abailable”。

  修改网站的根目录

  1.输入“ sudo vi/etc/apache2/apache2.conf"-enter在终端窗口 - 输入“目录/var/var/www/” position-change”/var/var/www/as as as the New Roots as New Roottho目录很好。

  2.输入“ sudo vi/etc/apache2/sites-可用/000-default.conf” -enter-find“ document root/var/html” -sufault.conf“ -enter-change”/var/var/var/var/var/var/var/var/var/var/var/var/wwwww/html“只是一个新的根目录。在这里,我将其更改为“/var/www/”。

  重新启动Apache

  输入“ sudo /etc/init.d/apache2 restart”中的“终端窗口” - “输入root用户密码” -Enter Enter-Restart成功。

  将“ index.html”文件复制到“/var/www”目录

  在终端窗口文件中输入“ cp/var/www/html/index.html/var/www/”,成功复制。

  测试网站的根目录是否成功

  在“ Firefox浏览器”中输入“ 127.0.0.1” - 对“ index.html”文件更改成功的访问

  结论:以上是如何在Apache上启动Django的所有内容,以了解如何在Apache上启动Django。我希望这对每个人都会有所帮助。如果您仍然想进一步了解这一点,请记住收集并关注此网站。