0

我正在从 Django 开发服务器切换到 Apache 进行生产。为此,我安装了modwsgi,并在apache2.conf中指定了以下内容。

# Run my django application.
WSGIScriptAlias / /home/david/registration/registration/wsgi.py
WSGIPythonPath /home/david/registration

<Directory /home/david/registration/registration>
<Files wsgi.py>
Order deny,allow
Allow from all
</Files>
</Directory>

当我重新启动 Apache 时,我发现我的应用程序在我的浏览器中加载,但 CSS 或图像都没有。

以前,我一直用staticfiles它来管理我的静态文件。我有这些设置。

# Absolute path to the directory static files should be collected to.
# Don't put anything in this directory yourself; store your static files
# in apps' "static/" subdirectories and in STATICFILES_DIRS.
# Example: "/home/media/media.lawrence.com/static/"
STATIC_ROOT = '/home/david/static/'

# URL prefix for static files.
# Example: "http://media.lawrence.com/static/"
STATIC_URL = '/static/'

静态文件过去可以工作,但现在不行了。为什么切换到 Apache 会阻止我的静态文件加载?

4

1 回答 1

1

使用 apache2 和 wsgi 和 django 的最佳选择是在 /etc/apache2/sites-enabled/mynewsite.apache 中使用一个新文件(扩展名无关紧要)

该文件必须如下所示:

NameVirtualHost *:80
<VirtualHost *:80>
    ServerAdmin diego@diegue.us
    ServerName yourproject.com.co
    Alias /admin/media/ /path/to/your/admin/media/
    Alias /media/ /path/to/your/media/
    Alias /static/ /path/to/your/static/collected/files/

    <Directory /path/to/your/admin/media/>
        Order deny,allow
        Allow from all
    </Directory>

    <Directory /path/to/your/media/>
        Order deny,allow
        Allow from all
    </Directory>

    <Directory /path/to/your/static/collected/files/>
        Order deny,allow
        Allow from all
    </Directory>

    WSGIScriptReloading On
    WSGIDaemonProcess yourproject python-path=/path/of/packages/for/python # (this is because I use virtualenvwrapper)
    WSGIProcessGroup yourproject
    WSGIApplicationGroup yourproject
    WSGIPassAuthorization On

    WSGIScriptAlias / /path/to/your/yourproject.wsgi
    ErrorLog /var/log/apache2/yourproject-error.log

    # Possible values include: debug, info, notice, warn, error, crit,
    # alert, emerg.
    LogLevel debug

    CustomLog /var/log/apache2/yourproject-access.log combined

</VirtualHost>

您需要告诉 apache 为您的 django 应用程序提供服务,但您还需要告诉它 Apache 为文件提供服务。

于 2012-05-18T21:52:38.253 回答