代码之家  ›  专栏  ›  技术社区  ›  matousc

Django+Apache:无法提供静态文件

  •  0
  • matousc  · 技术社区  · 7 年前

    我认为这是一个常见的问题,但我尝试过的解决方案中没有一个是有效的。

    <VirtualHost *:80>
            ServerName xxxx
            ServerAdmin xxxx
            DocumentRoot /home/matousc/apps/iacah
    
            ErrorLog ${APACHE_LOG_DIR}/error.log
            CustomLog ${APACHE_LOG_DIR}/access.log combined
    
            Alias /static /home/matousc/apps/iacah/www/static
            <Directory /home/matousc/apps/iacah/www/static>
                    Require all granted
                    Allow from all
            </Directory>
    
            <Directory /home/matousc/apps/iacah/app/mainapp>
                    <Files wsgi.py>
                            Require all granted
                    </Files>
            </Directory>
    
            WSGIDaemonProcess iacah python-path=/home/matousc/apps/iacah/app  python-home=/home/matousc/apps/appenv
            WSGIProcessGroup iacah
            WSGIScriptAlias / /home/matousc/apps/iacah/app/mainapp/wsgi.py
    </VirtualHost>
    

    我可以通过互联网访问该页面,所以 我确信我正在编辑正确的apacheconf文件 . 但是,不会加载静态文件。

    静态文件不会随一起下载 403错误 . 我注意到,如果我更改行:

    Alias /static/ /home/matousc/apps/iacah/www/static
    

    static :

    Alias /static /home/matousc/apps/iacah/www/static
    

    那我就去 . 在教程中,我看到了这两种选择,所以我有点困惑为什么它可以发挥作用。

    业主 /www/ 文件夹是 www-data (我正在使用ubuntu 18):

    drwxrwx--x  3 www-data www-data 4096 Sep 21 10:14 .
    drwxr-xr-x  8 matousc  matousc  4096 Sep 21 10:14 ..
    drwxrwxrwx 12 www-data www-data 4096 Sep 21 10:14 static
    

    我使用这台机器作为多主机,我还有一个静态网站,可以正常工作(文件被正确地提供)

    <VirtualHost *:80>
        ServerName xxxxx
        ServerAdmin xxxx
        DocumentRoot /home/matousc/apps/placeholder
    
        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined
    
        <Directory /home/matousc/apps/placeholder>
                Require all granted
                Options +Indexes
                AllowOverride None
         Order allow,deny
                Allow from all
        </Directory>
    

    在Django中,我使用(我希望推荐)设置:

    STATIC_URL = "/static/"
    
    if production_machine:
        level_up = os.path.dirname(BASE_DIR)
        STATIC_ROOT = os.path.join(level_up, "www", "static")
        STATICFILES_DIRS = (
            STATIC_ROOT,
        )
    else:
        STATIC_ROOT = os.path.join(BASE_DIR, 'static')
    
    STATICFILES_FINDERS = (
        'django.contrib.staticfiles.finders.FileSystemFinder',
        'django.contrib.staticfiles.finders.AppDirectoriesFinder',
    )
    

    (修订) production_machine 应该是真的)。

    2 回复  |  直到 7 年前
        1
  •  1
  •   Du D.    7 年前

    您运行过manage.py吗?在生产环境中,您需要调用此函数,以便django将您的开发代码复制到静态根位置。

    https://docs.djangoproject.com/en/1.11/ref/contrib/staticfiles/#collectstatic

        2
  •  0
  •   matousc    7 年前

    我不确定主要的问题是什么,但问题在之后神奇地消失了 又是一个纯巫术的小时

    1. 确保您正在编辑正确的apache conf文件

    2. 如@Du D。建议确保正确收集静态文件。可能会出现多个问题(它不会抓取所有静态文件夹等)。在你的生活中寻找问题 settings.py

    3. 确保包含收集的静态文件的文件夹归apache用户所有,并且具有递归的rwx访问权限!

    4. 在apacheconf文件中大量使用斜杠。似乎斜杠用法的某些组合比其他组合更优越。就我而言 /static/ /static .

    推荐文章