Static files not being served on AWS Lightsail

狂风中的少年 提交于 2020-12-25 21:23:31

问题


After 2 days of trying multiple tutorials & reading StackOverflow, I'm calling for help!

The setting: The development version is running smoothly on the AWS Lightsail server. It's during the production deployment that I'm running in continuous problems with the static files. The application runs on the appointed subdomain but it's missing all the JS/CSS/images/...

I have followed the official docs but to no avail. 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/

My Folder Tree with relevant files:

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)

My settings:

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

wsgi.py file

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()

My conf files :

1. httpd-app.conf file

<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 file

# 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/$1" [L,R=302,NE]
Include "/opt/bitnami/apps/django/django_projects/Project/conf/httpd-app.conf"

Other adjustments made: (/opt/bitnami/apache2/conf/bitnami) 1/ bitnami-apps-prefix.conf file

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

2/ bitnami.conf file

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. the bitnami-apps-vhosts.conf file is empty? Can this be?

Checked as well:

  • restarted Apache on multiple occasions
  • played around with the static_url & static_roots

Can anyone advise how to proceed? It's been extremely frustrating 2 days haha.

Note, maybe this can help : double-checked with the findstatic function, it redirects me to the Solar/static folder. I thought since I ran collect static, I should point to the Project level static folder in the apache conf & not the Solar level static folder.


回答1:


You need to add Alias /static/ /opt/bitnami/apps/django/django_projects/Project/static/ to your virtualhost config so the server knows to map /static/ requests to that folder.




回答2:


I wasted A LOT of time on this, here's my final working bitnami.conf file.

<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"

Note:

  • I know it's not recommended, but I put all the codes inside bitnami.conf and neglected httpd-app.conf and httpd-prefix.conf

  • I created python virtualenv at /opt/bitnami/projects/env and installed all needed libraries there

  • I added the below code to my settings.py file before running python manage.py collectstatic

STATIC_ROOT = os.path.join(BASE_DIR, 'static')
STATIC_URL = '/static/'


来源:https://stackoverflow.com/questions/63187103/static-files-not-being-served-on-aws-lightsail

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!