静态文件未在 AWS Lightsail 上提供

Static files not being served on AWS Lightsail

在尝试了 2 天的多个教程并阅读了 Whosebug 之后,我正在寻求帮助!

设置: 开发版在AWS Lightsail服务器上顺利运行。在生产部署期间,我 运行 经常遇到静态文件问题。该应用程序在指定的子域上运行,但它缺少所有 JS/CSS/images/...

我已经按照官方文档进行操作,但无济于事。 1/ https://docs.bitnami.com/aws/infrastructure/django/get-started/deploy-django-project/ 2/ https://docs.bitnami.com/aws/infrastructure/django/get-started/deploy-django-project/

包含相关文件的我的文件夹树:

Project
    - conf
       - httpd-app.conf
       - httpd-prefix.conf
    - Django2Tutorial
      - settings.py
      - wsgi.py
    - Solar
        - static
    - static (after running collectstatic function in terminal,-it includes the admin, Solar statics)

我的设置:

STATIC_ROOT = os.path.join(BASE_DIR,'static')
STATIC_URL = '/static/'
DEBUG = False
ALLOWED_HOSTS = ['54.169.172.***']

wsgi.py 文件

import os
import sys
sys.path.append('/opt/bitnami/apps/django/django_projects/Project')
os.environ.setdefault("PYTHON_EGG_CACHE", "/opt/bitnami/apps/django/django_projects/Project/egg_cache")
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'DjangoTutorial2.settings')
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()

我的配置文件:

1. httpd-app.conf 文件

<IfDefine !IS_DJANGOSTACK_LOADED> 
Define IS_DJANGOSTACK_LOADED
WSGIDaemonProcess wsgi-djangostack   processes=2 threads=15    display-name=%{GROUP}
</IfDefine> 

<Directory "/opt/bitnami/apps/django/django_projects/Project/DjangoTutorial2">

    Options +MultiViews
    AllowOverride All
    <IfVersion >= 2.3>
        Require all granted
    </IfVersion>
    
    WSGIProcessGroup wsgi-djangostack

    WSGIApplicationGroup %{GLOBAL}
                    
</Directory>

Alias /static "/opt/bitnami/apps/django/django_projects/Project/static"
<Directory "/opt/bitnami/apps/django/django_projects/Project/static">
Require all granted  
</Directory>

WSGIScriptAlias /  '/opt/bitnami/apps/django/django_projects/Project/DjangoTutorial2/wsgi.py'

2。 httpd-prefix.conf 文件

# Include file
RewriteEngine On
RewriteCond "%{HTTP_HOST}" ^ec2-([0-9]{1,3})-([0-9]{1,3})-([0-9]{1,3})-([0-9]{1,3})\..*\.amazonaws.com(:[0-9]*)?$
RewriteRule "^/?(.*)" "%{REQUEST_SCHEME}://%1.%2.%3.%4%5/" [L,R=302,NE]
Include "/opt/bitnami/apps/django/django_projects/Project/conf/httpd-app.conf"

其他调整:(/opt/bitnami/apache2/conf/bitnami) 1/ bitnami-apps-prefix.conf 文件

Include "/opt/bitnami/apps/django/django_projects/Project/conf/httpd-prefix.conf"

2/bitnami.conf 文件

VirtualHost _default_:80>
    WSGIScriptAlias / /opt/bitnami/apps/django/django_projects/Project/DjangoTutorial2/wsgi.py
<Directory /opt/bitnami/apps/django/django_projects/Project>
    AllowOverride all
Require all granted
Options FollowSymlinks
    </Directory>
DocumentRoot /opt/bitnami/apps/django/django_projects/Project
</VirtualHost>
  1. bitnami-apps-vhosts.conf 文件是空的?可以这样吗?

也检查过:

任何人都可以建议如何进行吗?这两天真是郁闷啊哈哈

请注意,也许这会有所帮助: 使用 findstatic 函数仔细检查,它将我重定向到 Solar/static 文件夹。我想既然我 运行 收集静态,我应该指向 apache conf 中的项目级静态文件夹而不是 Solar 级静态文件夹。

您需要将 Alias /static/ /opt/bitnami/apps/django/django_projects/Project/static/ 添加到您的虚拟主机配置,以便服务器知道将 /static/ 请求映射到该文件夹​​。

我在这上面浪费了很多时间,这是我最后的工作 bitnami.conf 文件。

<VirtualHost _default_:80>
    WSGIDaemonProcess django_site python-home=/opt/bitnami/projects/env python-path=/opt/bitnami/projects/django_site.com
    WSGIProcessGroup django_site
    WSGIScriptAlias / /opt/bitnami/projects/django_site.com/django_site/wsgi.py

    <Directory /opt/bitnami/projects/django_site.com>
        AllowOverride all
        Require all granted
        Options FollowSymlinks

        WSGIProcessGroup django_site
    </Directory>

    Alias /static/ /opt/bitnami/projects/django_site.com/static/
</VirtualHost>

Include "/opt/bitnami/apache/conf/bitnami/bitnami-ssl.conf"

注:

  • 我知道不推荐,但是我把所有代码都放在了bitnami.conf里面,忽略了httpd-app.confhttpd-prefix.conf

  • 我在 /opt/bitnami/projects/env 创建了 python virtualenv 并在那里安装了所有需要的库

  • 我在 运行 python manage.py collectstatic

    之前将以下代码添加到我的 settings.py 文件中
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
STATIC_URL = '/static/'