Deploying Django application - paradoxSid/notes GitHub Wiki

Method 1

Prerequisites

  • Python brew install python3
  • pip
  • virtualenv pip3 install virtualenv
  • awsebcli pip3 install awsebcli

Configure Django application

  • Go to the application base dir and activate virtualenv
  • Create a folder with mkdir .ebextensions
  • Create a file ./.ebextensions/django.config and add the below lines to
container_commands:
  01_collectstatic:
    command: "source /var/app/venv/*/bin/activate && python3 manage.py collectstatic --noinput"
    leader_only: true

option_settings:
  aws:elasticbeanstalk:container:python:
      WSGIPath: <PROJECT_NAME>.wsgi:application
  aws:elasticbeanstalk:environment:proxy:staticfiles:
      /static: static
  aws:elasticbeanstalk:application:environment:
      DJANGO_SETTINGS_MODULE: <PROJECT_NAME>.settings
      PYTHONPATH: "/var/app/current:$PYTHONPATH"
  • Initialize eb cli with eb init -p python-3.6 <PROJECT_NAME>
    • Your aws-access-id or aws-secret-key can be found here.
  • Create an environment and deploy your application to it with eb create.
eb create <ANY_NAME>

*** This may take upto 5 minutes ***

  • Domain name of the new environment can be found with eb status under CNAME. Copy it.
  • Inside ALLOWED_HOSTS in your Django's settings.py add the above copied domain names.
  • Now deploy your Django app with eb deploy
  • To deploy staged changes i.e. yet to be commit eb deploy --staged
  • You can open the app with eb open
  • To terminate the current eb env eb terminate

Debug logs

  • The eb instance will automatically be setup with the ssh.
  • You can connect to the ssh with eb ssh.
  • In the ssh enter cd /var/log/ to navigate to log directory.
  • In my experience with Beanstalk, there are three that are the most informative about deployment issues:
eb-engine.log
cfn-init.log
cfn-init-cmd.log
  • To view last 100 lines from any file use tail -n 100 <LOG_FILE>.

Method 2

  • Setup aws ec2 instance with an Ubuntu machine.
  • In Security Groups add Custom TCP with Port range: 8000 and 8080.
  • Connect to aws instance via ssh.
  • In Django's settings.py set ALLOWED_HOSTS = ['*'].
  • Run Django with python3 manage.py runserver 0.0.0.0:8000.
  • You server is running at port 8000, You can access it using your instance’s Public IPv4.

To Keep The server running

  • Install screen with sudo apt-get install screen.
  • Create a new screen with screen -S <screen_name>.
  • Run the Django application inside screen same as you would have ran it in without screen.
  • If you want to back to your main terminal press key shortcut ctrl+a+d.
  • To continue with a screen screen -r <screen_name>.
  • To remove a screen screen -X -S <screen_name> quit.

This way screen will keep the Django sever online even if you close the ssh client.

⚠️ **GitHub.com Fallback** ⚠️