【问题标题】:How can I configure a Django website to work with Apache VirtualHosts?如何配置 Django 网站以使用 Apache VirtualHosts?
【发布时间】:2016-08-01 10:58:35
【问题描述】:

我最近开始开发 Django 网站,我想将它们部署在带有 mod_wsgi 和 Apache 的 CentOS VPS 上。

我关注了this guide,它运行良好,但我希望同时运行多个 Django 网站,所以我尝试在适当的配置文件中创建多个虚拟主机,但问题就来了。

<VirtualHost the.ip.of.server:80>
    ErrorLog /root/praxis.error.log

    Alias /static /root/Praxis/static
    <Directory /root/Praxis/static>
        Require all granted
    </Directory>

    <Directory /root/Praxis/Praxis>
        <Files wsgi.py>
            Require all granted
        </Files>
    </Directory>

    WSGIDaemonProcess Praxis python-path=/root/Praxis:/root/Env/Praxis/lib/python2.7/site-packages
    WSGIProcessGroup Praxis
    WSGIScriptAlias /praxis /root/Praxis/Praxis/wsgi.py
</VirtualHost>

如果我删除 VirtualHost 开始和结束标签,网站将按预期运行,但如果我保留这些标签,我将不断收到 404 错误。

我真的不明白为什么只有没有标签才能正常工作。

我也尝试在基于 IP 的虚拟主机和基于名称的虚拟主机之间切换,但没有任何改变。

这里也是wsgi.py

wsgi.py

import os

from django.core.wsgi import get_wsgi_application

sys.path.append('/root/Praxis/Praxis')

os.environ['PYTHON_EGG_CACHE'] = '/root/Praxis/.python-egg'
os.environ['DJANGO_SETTINGS_MODULE'] = 'Praxis.settings'

application = get_wsgi_application()

在我的 httpd.conf 文件中,我也有:

Listen 80

ServerName ip.to.the.server #without port specified

#NameVirtualHost *:80 <-- THIS IS A COMMENT RIGHT NOW

提前感谢大家的努力,我希望有人能够帮助我,我正在努力解决这个问题!谢谢各位。


问题已解决! 我发现(实际上我不知道为什么)所有 VIrtualHosts 都必须放入 httpd.conf 文件而不是外部 .conf 文件中。所以我把它们都放在那里并删除了所有的“IncludeOptional”指令。就是这样!

【问题讨论】:

  • 请从您的标题中删除Solved,并将您找到的解决方案作为正确答案发布。

标签: python django apache centos virtualhost


【解决方案1】:

试试这个:

virtualhost.conf:

<VirtualHost you.ip.address:80>

  ServerAdmin admin@you.ip.address
  ServerName you.ip.address
  ServerAlias you.ip.address

  <Directory /You/directory/where/wsgifile>
    <Files wsgi.py>
      Require all granted
    </Files>
  </Directory>  
  WSGIScriptAlias / /You/directory/where/wsgifile/wsgi.py

  <Directory /You/directory/where/media>
    Require all granted
  </Directory> 
  Alias /media /You/directory/where/media

  <Directory /You/directory/where/static>
    Require all granted
  </Directory> 
  Alias /static /You/directory/where/static

</VirtualHost>

在你的 httpd.conf 中:

ServerName your.server.name 例如 www.example.com 或你的 ip

你的 wsgi.py 文件:

import os
import sys
sys.path.append(r'/Path/to/You/DjanoSite')
sys.path.append(r'/Path/to/You/DjanoSite/DjangoSite/Wherewsgifile')
sys.path.append(r'/Path/to/You/media')
sys.path.append(r'/Path/to/You/static')
os.environ['DJANGO_SETTINGS_MODULE'] = 'DjangoSite.settings'
import django
django.setup()
import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()

【讨论】:

  • 不幸的是它没有用。当我尝试访问该页面时仍然出现 404 错误。
  • 你的 DjangoSite 文件夹在哪里
  • 你的 DjangoSite 文件夹位于 /root ?
  • 是的!我知道以 root 用户身份部署东西不好,当一切正常时,我将在非 root 用户下切换所有内容
  • 尝试答案#1 和答案#2。您将不能以非 root 用户身份运行 80 端口的 apache。存在很多方法,比如我们在 /var/www 目录下创建 django-project 或者在 /home 目录下使用 root 权限。
猜你喜欢
  • 2017-10-01
  • 1970-01-01
  • 2016-09-11
  • 2011-12-12
  • 2023-03-24
  • 2011-06-29
  • 2020-05-25
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多