3

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.

  • Open a known static URL, and tell us the errors showing by the browser and by the apache logs – Victor Jul 31 '20 at 09:29
  • @VictorE: Error 404. It displays my custom error404 page if i try to open one of the images – Wouter Gyssels Jul 31 '20 at 09:31
  • Show us the error in /var/log/apache2/error.log (or whatver is the error log for your server. That should tell you how it's mapping the request to the file in your directory – Victor Jul 31 '20 at 09:42
  • You need to add "Alias /static/ /opt/bitnami/apps/django/django_projects/Project/static/" to your virtualhost config – Victor Jul 31 '20 at 09:56
  • btw just remembered with Apache, 404 errors are logged in access.log. can you check that file? – Victor Jul 31 '20 at 10:02
  • @VictorE: Thank you for looking into it. About the logs:172.26.0.*** - - [31/Jul/2020:17:58:29 +0800] "GET /assets/media/logos/favicon.ico HTTP/1.1" 200 7661 172.26.0.*** - - [31/Jul/2020:17:58:29 +0800] "GET /assets/media/logos/favicon.ico HTTP/1.1" 200 7661 103.100.175.*** - - [31/Jul/2020:17:58:29 +0800] "GET /static/assetsadmin/assets/plugins/custom/datatables/datatables.bundle.js HTTP/1.1" 200 2536 103.100.175.*** - - [31/Jul/2020:17:58:44 +0800] "GET /static/img/favicontest.ico HTTP/1.1" 200 2536. – Wouter Gyssels Jul 31 '20 at 10:04
  • @VictorE: about your suggestion, which file will this be ? httpd-app.conf file? or bitnami.conf file? – Wouter Gyssels Jul 31 '20 at 10:06
  • Inside the in your bitnami.conf file – Victor Jul 31 '20 at 10:08
  • @VictorE: Hero! It works! Thank you so much. You can't imagine how long I have been struggling with this! – Wouter Gyssels Jul 31 '20 at 10:11

2 Answers2

2

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.

Victor
  • 2,864
  • 1
  • 12
  • 20
  • Resolved! Thank you so much. 48h of my life wasted on this... – Wouter Gyssels Jul 31 '20 at 10:16
  • I did this. Added `Alias /alternativ/static "/opt/bitnami/projects/alternativ/static" Require all granted ` at the end of `httpd-app.conf ` file. but it doesn't still load anything. – Shahriar.M Nov 19 '20 at 15:18
1

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/'
Rami Alloush
  • 2,308
  • 2
  • 27
  • 33