Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django on Amazon Web Service (AWS)

I have read these tutorials : https://realpython.com/blog/python/deploying-a-django-app-to-aws-elastic-beanstalk/ and http://docs.aws.amazon.com/elasticbeanstalk/latest/dg/create-deploy-python-django.html

I use Django 1.8.2 and it seems that the amazon's doc is a little bit outdated (they still used django-admin.py instead of django-admin), and some parts of it are not working (when stuff does not work, i fallback to the realpython link one).

So, I got it all working except my admin page does not load the static files. So, the css file is not loaded.

This is my settings.py :

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

i have also tried to use :

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
STATIC_ROOT = os.path.join(BASE_DIR, 'static'). 

but it still does not work.

this is my eb config file :

container_commands:
  01_migrate:
    command: "source /opt/python/run/venv/bin/activate && python papp/manage.py migrate --noinput"
    leader_only: true
  02_createsuperuser:
    command: "source /opt/python/run/venv/bin/activate && python papp/manage.py createsu"
    leader_only: true
  03_collectstatic:
    command: "source /opt/python/run/venv/bin/activate && python papp/manage.py collectstatic --noinput"

  option_settings:
    "aws:elasticbeanstalk:application:environment":
      DJANGO_SETTINGS_MODULE: "papp.settings"
      PYTHONPATH: "/opt/python/current/app/papp:$PYTHONPATH"
    "aws:elasticbeanstalk:container:python":
      WSGIPath: "papp/papp/wsgi.py"
    "aws:elasticbeanstalk:container:python:staticfiles":
      "/static/": "static/"

I used eb deploy command after making the changes.

Is there extra steps that I need to do ? I have read in here : Default Django 1.5 admin css not working that i need to change alias, but it is for apache.

I also read from django doc files such as https://docs.djangoproject.com/en/1.8/howto/static-files/ but im not sure of what to put in the STATIC_ROOT for AWS

any help is much appreciated. Thanks

like image 650
bysreg Avatar asked Jun 03 '15 15:06

bysreg


Video Answer


2 Answers

It turns out "aws:elasticbeanstalk:container:python:staticfiles" maps files in your directory on your EC2 instance (/opt/python/current/app/static/) to /static/

setting STATIC_ROOT in settings.py to os.path.join(BASE_DIR, '..','static') fixed the issue

like image 116
bysreg Avatar answered Sep 21 '22 16:09

bysreg


Although you seem to have solved your problem, I had a similar issue, but since my app was being uploaded from the root of the project directory, setting STATIC_ROOT = os.path.join(BASE_DIR, '..','static') didn't work.

Changing the container_commands to adhere to AWS Docs did the trick

container_commands:
  01_migrate:
    command: "django-admin.py migrate"
    leader_only: true
   02_collectstatic:
     command: "django-admin.py collectstatic --noinput"
     leader_only: true

Before that, following that same tutorial I've got the following issues

The createsu command wasn't working.

Running

$ eb ssh
$ /opt/python/run/venv/bin/python manage.py collectstatic

Somehow pointed to an odd location

You have requested to collect static files at the destination location as specified in your settings: /opt/python/bundle/2/app/static

All the above problems were also solved after changing the commands to the AWS Docs version.

like image 37
galileopy Avatar answered Sep 19 '22 16:09

galileopy