python - 如何配置 Django 网站以使用 Apache VirtualHosts?

标签 python django apache centos virtualhost

我最近开始开发 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”指令。而已!

最佳答案

尝试这个:

虚拟主机.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()

关于python - 如何配置 Django 网站以使用 Apache VirtualHosts?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36546021/

相关文章:

python - 如何通过使用 Pandas 定义除法函数来获得结果?

Django 从 sqlite3 切换到 Postgresql 问题

apache - Oracle HTTP Server (OHS) Apache 2.2.13 Poodle SSLv3 修复?

apache - 网站 ERR_CONNECTION_RESET - 客户端未向服务器发送 SSL hello 数据包

apache - 比特纳米 AH02042 : rejecting client initiated renegotiation

python - 使用简单路径找不到 Django 页面

python - 如何创建交集函数?

Python - 创建模式列表

python - Django FormView将POST值解释为列表

python - 尝试使用 Python 设置 Postgresql - 安装了 psycopg2 但出错