Deploying existing Django project on AWS Lightsail with Bitnami & Apache / 500 Internal Server Error

Keywords: Django - AWS - How to - Other
Description:
Hi all,

i'm quite new to AWS Lightsail and Bitnami and trying to deploy a exiting Django project on AWS Lightsail with Bitnami and Apache using this instruction:

https://docs.bitnami.com/aws/apps/weblate/get-started/deploy-django-project/

I'm running a AWS Lightsail Linux instance with Django 3.1.0.

I created a new folder named projects in htdocs:
/opt/bitnami/apache/htdocs/projects
and uploaded my Django Project to this folder:
/opt/bitnami/apache/htdocs/projects/portfoliodrilldown
in this folder is my manage.py file and the folders for my apps.
My settings.py file and wsgi.py is located in:
/opt/bitnami/apache/htdocs/projects/portfoliodrilldown/core

When I run: cd htdocs/projects/portfoliodrilldown python manage.py run server 0.0.0.0:8000 I can access my website under http://SERVER-IP:8000

However when I try to continue the instructions to host the website using Apache my problems begin.

Following the instruction I created the following files:

1.
/opt/bitnami/apache/conf/vhosts/portfoliodrilldown-vhost.conf

"<IfDefine !IS_PORTFOLIODRILLDOWN_LOADED>
  Define IS_PORTFOLIODRILLDOWN_LOADED
  WSGIDaemonProcess portfoliodrilldown python-home=/opt/bitnami/python python-path=/opt/bitnami/apache/htdocs/projects/portfoliodrilldown processes=2 threads=15
</IfDefine>
<VirtualHost 127.0.0.1:80 _default_:80>
  ServerAlias *
  WSGIProcessGroup portfoliodrilldown
  Alias /robots.txt /opt/bitnami/apache/htdocs/projects/portfoliodrilldown/static/robots.txt
  Alias /favicon.ico /opt/bitnami/apache/htdocs/projects/portfoliodrilldown/static/favicon.ico
  Alias /static/ /opt/bitnami/apache/htdocs/projects/portfoliodrilldown/static/
  <Directory /opt/bitnami/apache/htdocs/projects/portfoliodrilldown/static>
    Require all granted
  </Directory>
  Alias /media/ /opt/bitnami/apache/htdocs/projects/portfoliodrilldown/media/
  <Directory /opt/bitnami/apache/htdocs/projects/portfoliodrilldown/media>
    Require all granted
  </Directory>
  WSGIScriptAlias / /opt/bitnami/apache/htdocs/projects/portfoliodrilldown/core/wsgi.py
  <Directory /opt/bitnami/apache/htdocs/projects/portfoliodrilldown/core>
    <Files wsgi.py>
      Require all granted
    </Files>
  </Directory>
</VirtualHost>"

2.
/opt/bitnami/apache/conf/vhosts/portfoliodrilldown-https-vhost.conf

<IfDefine !IS_PORTFOLIODRILLDOWN_LOADED>
  Define IS_PORTFOLIODRILLDOWN_LOADED
  WSGIDaemonProcess portfoliodrilldown python-home=/opt/bitnami/python python-path=/opt/bitnami/apache/htdocs/projects/portfoliodrilldown processes=2 threads=15
</IfDefine>
<VirtualHost 127.0.0.1:443 _default_:443>
  ServerAlias *
  SSLEngine on
  SSLCertificateFile "/opt/bitnami/apache/conf/bitnami/certs/server.crt"
  SSLCertificateKeyFile "/opt/bitnami/apache/conf/bitnami/certs/server.key"
  WSGIProcessGroup portfoliodrilldown
  Alias /robots.txt /opt/bitnami/apache/htdocs/projects/portfoliodrilldown/static/robots.txt
  Alias /favicon.ico /opt/bitnami/apache/htdocs/projects/portfoliodrilldown/static/favicon.ico
  Alias /static/ /opt/bitnami/apache/htdocs/projects/portfoliodrilldown/static/
  <Directory /opt/bitnami/apache/htdocs/projects/portfoliodrilldown/static>
    Require all granted
  </Directory>
  Alias /media/ /opt/bitnami/apache/htdocs/projects/portfoliodrilldown/media/
  <Directory /opt/bitnami/apache/htdocs/projects/portfoliodrilldown/media>
    Require all granted
  </Directory>
  WSGIScriptAlias / /opt/bitnami/apache/htdocs/projects/portfoliodrilldown/core/wsgi.py
  <Directory /opt/bitnami/apache/htdocs/projects/portfoliodrilldown/core>
    <Files wsgi.py>
      Require all granted
    </Files>
  </Directory>
</VirtualHost>

3.
/opt/bitnami/apache/conf/vhosts/core-http-vhost.conf

<IfDefine !IS_CORE_LOADED>
  Define IS_CORE_LOADED
  WSGIDaemonProcess CORE python-home=/opt/bitnami/python python-path=/opt/bitnami/apache/htdocs/projects/portfoliodrilldown/core
</IfDefine>
<VirtualHost 127.0.0.1:80 _default_:80>
  ServerAlias *
  WSGIProcessGroup CORE
  Alias /robots.txt /opt/bitnami/apache/htdocs/projects/portfoliodrilldown/core/static/robots.txt
  Alias /favicon.ico /opt/bitnami/apache/htdocs/projects/portfoliodrilldown/core/static/favicon.ico
  Alias /static/ /opt/bitnami/apache/htdocs/projects/portfoliodrilldown/core/static/
  <Directory /opt/bitnami/apache/htdocs/projects/portfoliodrilldown/core/static>
    Require all granted
  </Directory>
  WSGIScriptAlias / /opt/bitnami/apache/htdocs/projects/portfoliodrilldown/core/wsgi.py
  <Directory /opt/bitnami/apache/htdocs/projects/portfoliodrilldown/core>
    <Files wsgi.py>
      Require all granted
    </Files>
  </Directory>
</VirtualHost>

4.
/opt/bitnami/apache/conf/vhosts/core-https-vhost.conf

<IfDefine !IS_CORE_LOADED>
  Define IS_CORE_LOADED
  WSGIDaemonProcess CORE python-home=/opt/bitnami/apache/htdocs/projects/portfoliodrilldown/core
</IfDefine>
<VirtualHost 127.0.0.1:443 _default_:443>
  ServerAlias *
  SSLEngine on
  SSLCertificateFile "/opt/bitnami/apache2/conf/bitnami/certs/server.crt"
  SSLCertificateKeyFile "/opt/bitnami/apache2/conf/bitnami/certs/server.key"
  WSGIProcessGroup CORE
  Alias /robots.txt /opt/bitnami/apache/htdocs/projects/portfoliodrilldown/core/static/robots.txt
  Alias /favicon.ico /opt/bitnami/apache/htdocs/projects/portfoliodrilldown/core/static/favicon.ico
  Alias /static/ /opt/bitnami/apache/htdocs/projects/portfoliodrilldown/core/static/
  <Directory /opt/bitnami/apache/htdocs/projects/portfoliodrilldown/core/static>
    Require all granted
  </Directory>
  WSGIScriptAlias / /opt/bitnami/apache/htdocs/projects/portfoliodrilldown/core/wsgi.py
  <Directory /opt/bitnami/apache/htdocs/projects/portfoliodrilldown/core>
    <Files wsgi.py>
      Require all granted
    </Files>
  </Directory>
</VirtualHost>

My wsgi.py file contains:

import os
from django.core.wsgi import get_wsgi_application
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'core.settings')
application = get_wsgi_application()

And my settings.py file:

import os
from decouple import config
from unipath import Path
import dj_database_url

# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR    = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
PROJECT_DIR = Path(__file__).parent

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = config('SECRET_KEY', default='##########')

DEBUG = config('DEBUG', default=False)

ALLOWED_HOSTS = ['REDACTED_IP_ADDRESS', '.portfoliodrilldown.com', '127.0.0.1']

# Application definition

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'mathfilters',
    'app',  # Enable the inner app
]

MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
    'whitenoise.middleware.WhiteNoiseMiddleware',
]

ROOT_URLCONF = 'core.urls'
LOGIN_REDIRECT_URL = "my_portfolio"   # Route defined in app/urls.py
LOGOUT_REDIRECT_URL = "login"  # Route defined in app/urls.py
TEMPLATE_DIR = os.path.join(BASE_DIR, "core/templates")  # ROOT dir for templates

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [TEMPLATE_DIR],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
                ### Von mir HinzugefĆ¼gt
                'app.context_processors.user_settings',
                'app.context_processors.useable_currencies',
                'app.context_processors.useable_commodities',
            ],
        },
    },
]

WSGI_APPLICATION = 'core.wsgi.application'

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
    }
}

AUTH_PASSWORD_VALIDATORS = [
    {
        'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
    },
]

LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'UTC'

USE_I18N = True

USE_L10N = True

USE_TZ = True

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))

# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.9/howto/static-files/
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
STATIC_URL = '/static/'

# Extra places for collectstatic to find static files.
STATICFILES_DIRS = (
    os.path.join(BASE_DIR, 'core/static'),
)

After restarting the Apache Server I tried to access the website using several variations of:

http://SERVER-IP/portfoliodrilldown/core
http://SERVER-IP/portfoliodrilldown
http://SERVER-IP/core
...

but im always getting the 500 Internal Server Error.

I hope someone of you guys can help me to fix this problem!
Big thanks in advance!! :slight_smile:

Cheers, Max

Sorry, I forgot to add the error log:

[Tue Jan 12 17:20:29.441018 2021] [wsgi:error] [pid 11705] [remote 88.152.184.70:17611] mod_wsgi (pid=11705): Failed to exec Python script file '/opt/bitnami/apache/htdocs/projects/portfoliodrilldown/core/wsgi.py'.
[Tue Jan 12 17:20:29.441080 2021] [wsgi:error] [pid 11705] [remote 88.152.184.70:17611] mod_wsgi (pid=11705): Exception occurred processing WSGI script '/opt/bitnami/apache/htdocs/projects/portfoliodrilldown/core/wsgi.py'.
[Tue Jan 12 17:20:29.441260 2021] [wsgi:error] [pid 11705] [remote 88.152.184.70:17611] Traceback (most recent call last):
[Tue Jan 12 17:20:29.441342 2021] [wsgi:error] [pid 11705] [remote 88.152.184.70:17611]   File "/opt/bitnami/apache/htdocs/projects/portfoliodrilldown/core/wsgi.py", line 13, in <module>
[Tue Jan 12 17:20:29.441350 2021] [wsgi:error] [pid 11705] [remote 88.152.184.70:17611]     application = get_wsgi_application()
[Tue Jan 12 17:20:29.441360 2021] [wsgi:error] [pid 11705] [remote 88.152.184.70:17611]   File "/opt/bitnami/python/lib/python3.8/site-packages/django/core/wsgi.py", line 12, in get_wsgi_application
[Tue Jan 12 17:20:29.441366 2021] [wsgi:error] [pid 11705] [remote 88.152.184.70:17611]     django.setup(set_prefix=False)
[Tue Jan 12 17:20:29.441374 2021] [wsgi:error] [pid 11705] [remote 88.152.184.70:17611]   File "/opt/bitnami/python/lib/python3.8/site-packages/django/__init__.py", line 19, in setup
[Tue Jan 12 17:20:29.441380 2021] [wsgi:error] [pid 11705] [remote 88.152.184.70:17611]     configure_logging(settings.LOGGING_CONFIG, settings.LOGGING)
[Tue Jan 12 17:20:29.441388 2021] [wsgi:error] [pid 11705] [remote 88.152.184.70:17611]   File "/opt/bitnami/python/lib/python3.8/site-packages/django/conf/__init__.py", line 83, in __getattr__
[Tue Jan 12 17:20:29.441394 2021] [wsgi:error] [pid 11705] [remote 88.152.184.70:17611]     self._setup(name)
[Tue Jan 12 17:20:29.441402 2021] [wsgi:error] [pid 11705] [remote 88.152.184.70:17611]   File "/opt/bitnami/python/lib/python3.8/site-packages/django/conf/__init__.py", line 70, in _setup
[Tue Jan 12 17:20:29.441407 2021] [wsgi:error] [pid 11705] [remote 88.152.184.70:17611]     self._wrapped = Settings(settings_module)
[Tue Jan 12 17:20:29.441415 2021] [wsgi:error] [pid 11705] [remote 88.152.184.70:17611]   File "/opt/bitnami/python/lib/python3.8/site-packages/django/conf/__init__.py", line 177, in __init__
[Tue Jan 12 17:20:29.441420 2021] [wsgi:error] [pid 11705] [remote 88.152.184.70:17611]     mod = importlib.import_module(self.SETTINGS_MODULE)
[Tue Jan 12 17:20:29.441428 2021] [wsgi:error] [pid 11705] [remote 88.152.184.70:17611]   File "/opt/bitnami/python/lib/python3.8/importlib/__init__.py", line 127, in import_module
[Tue Jan 12 17:20:29.441434 2021] [wsgi:error] [pid 11705] [remote 88.152.184.70:17611]     return _bootstrap._gcd_import(name[level:], package, level)
[Tue Jan 12 17:20:29.441442 2021] [wsgi:error] [pid 11705] [remote 88.152.184.70:17611]   File "<frozen importlib._bootstrap>", line 1014, in _gcd_import
[Tue Jan 12 17:20:29.441450 2021] [wsgi:error] [pid 11705] [remote 88.152.184.70:17611]   File "<frozen importlib._bootstrap>", line 991, in _find_and_load
[Tue Jan 12 17:20:29.441459 2021] [wsgi:error] [pid 11705] [remote 88.152.184.70:17611]   File "<frozen importlib._bootstrap>", line 961, in _find_and_load_unlocked
[Tue Jan 12 17:20:29.441468 2021] [wsgi:error] [pid 11705] [remote 88.152.184.70:17611]   File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
[Tue Jan 12 17:20:29.441477 2021] [wsgi:error] [pid 11705] [remote 88.152.184.70:17611]   File "<frozen importlib._bootstrap>", line 1014, in _gcd_import
[Tue Jan 12 17:20:29.441486 2021] [wsgi:error] [pid 11705] [remote 88.152.184.70:17611]   File "<frozen importlib._bootstrap>", line 991, in _find_and_load
[Tue Jan 12 17:20:29.441495 2021] [wsgi:error] [pid 11705] [remote 88.152.184.70:17611]   File "<frozen importlib._bootstrap>", line 973, in _find_and_load_unlocked
[Tue Jan 12 17:20:29.441514 2021] [wsgi:error] [pid 11705] [remote 88.152.184.70:17611] ModuleNotFoundError: No module named 'core'
[Tue Jan 12 17:20:37.193990 2021] [wsgi:error] [pid 11705] [remote 88.152.184.70:17670] mod_wsgi (pid=11705): Failed to exec Python script file '/opt/bitnami/apache/htdocs/projects/portfoliodrilldown/core/wsgi.py'.
[Tue Jan 12 17:20:37.194051 2021] [wsgi:error] [pid 11705] [remote 88.152.184.70:17670] mod_wsgi (pid=11705): Exception occurred processing WSGI script '/opt/bitnami/apache/htdocs/projects/portfoliodrilldown/core/wsgi.py'.
[Tue Jan 12 17:20:37.194211 2021] [wsgi:error] [pid 11705] [remote 88.152.184.70:17670] Traceback (most recent call last):
[Tue Jan 12 17:20:37.194280 2021] [wsgi:error] [pid 11705] [remote 88.152.184.70:17670]   File "/opt/bitnami/apache/htdocs/projects/portfoliodrilldown/core/wsgi.py", line 13, in <module>
[Tue Jan 12 17:20:37.194287 2021] [wsgi:error] [pid 11705] [remote 88.152.184.70:17670]     application = get_wsgi_application()
[Tue Jan 12 17:20:37.194297 2021] [wsgi:error] [pid 11705] [remote 88.152.184.70:17670]   File "/opt/bitnami/python/lib/python3.8/site-packages/django/core/wsgi.py", line 12, in get_wsgi_application
[Tue Jan 12 17:20:37.194303 2021] [wsgi:error] [pid 11705] [remote 88.152.184.70:17670]     django.setup(set_prefix=False)
[Tue Jan 12 17:20:37.194311 2021] [wsgi:error] [pid 11705] [remote 88.152.184.70:17670]   File "/opt/bitnami/python/lib/python3.8/site-packages/django/__init__.py", line 19, in setup
[Tue Jan 12 17:20:37.194316 2021] [wsgi:error] [pid 11705] [remote 88.152.184.70:17670]     configure_logging(settings.LOGGING_CONFIG, settings.LOGGING)
[Tue Jan 12 17:20:37.194325 2021] [wsgi:error] [pid 11705] [remote 88.152.184.70:17670]   File "/opt/bitnami/python/lib/python3.8/site-packages/django/conf/__init__.py", line 83, in __getattr__
[Tue Jan 12 17:20:37.194330 2021] [wsgi:error] [pid 11705] [remote 88.152.184.70:17670]     self._setup(name)
[Tue Jan 12 17:20:37.194338 2021] [wsgi:error] [pid 11705] [remote 88.152.184.70:17670]   File "/opt/bitnami/python/lib/python3.8/site-packages/django/conf/__init__.py", line 70, in _setup
[Tue Jan 12 17:20:37.194343 2021] [wsgi:error] [pid 11705] [remote 88.152.184.70:17670]     self._wrapped = Settings(settings_module)
[Tue Jan 12 17:20:37.194351 2021] [wsgi:error] [pid 11705] [remote 88.152.184.70:17670]   File "/opt/bitnami/python/lib/python3.8/site-packages/django/conf/__init__.py", line 177, in __init__
[Tue Jan 12 17:20:37.194357 2021] [wsgi:error] [pid 11705] [remote 88.152.184.70:17670]     mod = importlib.import_module(self.SETTINGS_MODULE)
[Tue Jan 12 17:20:37.194365 2021] [wsgi:error] [pid 11705] [remote 88.152.184.70:17670]   File "/opt/bitnami/python/lib/python3.8/importlib/__init__.py", line 127, in import_module
[Tue Jan 12 17:20:37.194370 2021] [wsgi:error] [pid 11705] [remote 88.152.184.70:17670]     return _bootstrap._gcd_import(name[level:], package, level)
[Tue Jan 12 17:20:37.194378 2021] [wsgi:error] [pid 11705] [remote 88.152.184.70:17670]   File "<frozen importlib._bootstrap>", line 1014, in _gcd_import
[Tue Jan 12 17:20:37.194387 2021] [wsgi:error] [pid 11705] [remote 88.152.184.70:17670]   File "<frozen importlib._bootstrap>", line 991, in _find_and_load
[Tue Jan 12 17:20:37.194395 2021] [wsgi:error] [pid 11705] [remote 88.152.184.70:17670]   File "<frozen importlib._bootstrap>", line 961, in _find_and_load_unlocked
[Tue Jan 12 17:20:37.194404 2021] [wsgi:error] [pid 11705] [remote 88.152.184.70:17670]   File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
[Tue Jan 12 17:20:37.194413 2021] [wsgi:error] [pid 11705] [remote 88.152.184.70:17670]   File "<frozen importlib._bootstrap>", line 1014, in _gcd_import
[Tue Jan 12 17:20:37.194421 2021] [wsgi:error] [pid 11705] [remote 88.152.184.70:17670]   File "<frozen importlib._bootstrap>", line 991, in _find_and_load
[Tue Jan 12 17:20:37.194430 2021] [wsgi:error] [pid 11705] [remote 88.152.184.70:17670]   File "<frozen importlib._bootstrap>", line 973, in _find_and_load_unlocked
[Tue Jan 12 17:20:37.194449 2021] [wsgi:error] [pid 11705] [remote 88.152.184.70:17670] ModuleNotFoundError: No module named 'core'

Hi @portfoliodrilldown,

Thanks for using Bitnami. According to our docs, the app settings ( settings.py) should be placed under /opt/bitnami/projects/YOUR_APP, but you are placing it into /opt/bitnami/projects/YOUR_APP/core. I don't know if setting the paths to this folder instead to /opt/bitnami/projects/YOUR_APP for the python-path value, or moving the settings.py file to /opt/bitnami/projects/YOUR_APP would work. Can you check it?

I also noticed you hardcoded some IP address in that file. Can you try to set it with '*' (i.e. from everywhere), and then restrict the access once everything is working?

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.