【问题标题】:Django + mod_wsgi: Can someone advise me on my setup and rewrite rulesDjango + mod_wsgi:有人可以就我的设置和重写规则给我建议吗
【发布时间】:2012-09-30 09:03:46
【问题描述】:

这是我第一次将 Django 部署到最近收购的 Linode 服务器上,我很好奇是否有人可以查看我的部署并帮助我解决一些烦人的问题并建议我是否做错了事情。

目录结构

home\
    -public\
        -example.com\
            -public\
                -.htaccess
                -index.html
            -log\
            -application\
                -mysite\
                    -mysite\
                    -manage.py
                    -static\
                    -myapp\
                    -logs\

Django 的部署结构如何?

不正确的 URL 命名

我在我的域“example.com”上托管了名为“myapp”的 Django 应用程序。按照 Django 网站上的说明进行操作,应用程序的 urls.py 必须以“/myapp”开头。这导致应用的域变为“example.com/myapp”。

如何设置它,以便 example.com 只是我编写的 Django 应用程序? 我想简单地导航到 example.com,它会加载我的应用而不是 example.com/myapp。

更奇怪的是,我原以为 example.com 会加载我的 index.html 文件,但它却试图为 Django 查找 URL 映射...

Django 日志文件写入权限

每当我通过 SSH 连接到我的机器以“syncdb”或“collectstatic”时,日志记录模块都会创建我在我的 settings.py 文件中命名的日志文件。这给我带来了问题,因为我是文件的所有者,而 apache2 (www-data) 无法写入它。在我重新启动 apache 服务器之前,必须在每个命令之后手动删除日志文件,这很烦人。

这是我的 /etc/apache2/sites-available/example.com 文件:

# domain: example.com
# public: /home/setheron/public/example.com/

WSGIPythonPath /home/setheron/public/example.com/applications/mysite:/home/setheron/env/lib/python2.7/site-packages

<VirtualHost *:80>
  # Admin email, Server Name (domain name), and any aliases
  ServerAdmin setheron@setheron.com
  ServerName  www.example.example.com
  ServerAlias example.com

  WSGIScriptAlias / /home/setheron/public/example.com/applications/mysite/mysite/wsgi.py

  Alias /static/ /home/setheron/public/example.com/applications/mysite/static/
  <Directory /home/setheron/public/example.com/applications/mysite/static/>
        Order deny,allow
        Allow from all
  </Directory>

  <Directory /home/setheron/public/example.com/applications/mysite/mysite>
  <Files wsgi.py>
        Order deny,allow
        Allow from all
  </Files>
  </Directory>

  # Index file and Document Root (where the public files are located)
  DirectoryIndex index.html index.php
  DocumentRoot /home/setheron/public/example.com/public

  # Log file locations
  LogLevel warn
  ErrorLog  /home/setheron/public/example.com/log/error.log
  CustomLog /home/setheron/public/example.com/log/access.log combined
</VirtualHost>

【问题讨论】:

    标签: django apache apache2 mod-wsgi linode


    【解决方案1】:

    如果您希望 Django 为整个站点提供服务,请摆脱您的公共目录、索引等。除了 /static,你应该只需要你的 WSGIScriptAlias 指令。修复 urls.py 以说明您的网站应该来自 /,而不是 /myapp。

    【讨论】:

    • 嘿,谢谢!我修好了。还有一个问题。是否必须为 www-data (apache) 设置权限作为文件的组?我想使用织物,但我对文件权限设置感到困惑。
    • @Setheron 程序文件必须能够被网络服务器进程读取才能运行。目录应该是可写的,因此它可以生成 .pyc 文件。显然,日志文件应该是可写的。
    • 那么部署脚本通常会 chgrp 到所有文件/目录的 apache 组吗?
    • 我不会说“典型”,但这并不少见。
    猜你喜欢
    • 2011-08-05
    • 1970-01-01
    • 1970-01-01
    • 2011-11-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-28
    相关资源
    最近更新 更多