Deploy Django GreatKart Project on Amazon AWS Elastic Beanstalk

Written by Rathan Kumar

Senior Software Developer | Django Instructor

GETTING READY – PACKAGES UPDATE

Here are the steps to deploy this application on Python 3.8 with Amazon Linux 2 server.

  • Gather all the packages installed into requirements.txt and upgrade them by replacing == to >=
  • Run the below commands.
pip freeze > requirements.txt
pip install -r requirements.txt --upgrade
  • Uninstall django-admin-honeypot and install django-admin-honeypot-updated-2021. Because the former does not work with the latest version of Django.
  • In case you get the warning to add the DEFAULT_AUTO_FIELD, then add the below line of code in your settings.py
DEFAULT_AUTO_FIELD='django.db.models.AutoField'

You usually get this warning when you update your Django version from 3.x to 4.x. If you are already using Django 4.x from the beginning, you probably won’t get this warning.

Gather all the installed packages into requirements.txt so that we can install all of them in the AWS server. Run the below command.

pip freeze > requirements.txt

SERVER CONFIGURATION

  • Create a directory named .ebextensions on the root folder.
  • In the .ebextensions directory, add a configuration file named django.config with the following text.
option_settings:
  aws:elasticbeanstalk:container:python:
    WSGIPath: greatkart.wsgi:application
  aws:elasticbeanstalk:environment:proxy:staticfiles:
    /static: static

Note: option_settings namespace aws:elasticbeanstalk:container:python This setting, WSGIPath specifies the location of the WSGI script that Elastic Beanstalk uses to start your application.

Note: option_settings namespace aws:elasticbeanstalk:environment:proxy:staticfiles: For the static files to be working on Nginx servers, we need to set the proxy configuration. So this is to configure the proxy server to serve static files. Read more…

  • Collect the static files
python manage.py collectstatic
  • Deactivate the virtual environment
deactivate

DEPLOY YOUR SITE WITH EB CLI

  • Initialize your EB CLI repository with the eb init command.
eb init -p python-3.8 django-greatkart-app
  • Create AWS Access Key and Secret Key.
  • (Optional) Run eb init again to configure a default key pair so that you can use SSH to connect to the EC2 instance running your application.
eb init
  • Create an environment and deploy your application to it with eb create.
eb create django-greatkart-env
  • When the environment creation process completes, find the domain name of your new environment by running eb status.
eb status

If the status is Red, then restart the app server.
Once the status is green, run the CNAME. You will most probably get 502 Bad Gateway error.

At this point, we need to take care of 2 things:

  1. Add this CNAME inside our allowed host list.
  2. Provide all the secret information to the server, such as SECRET_KEY, EMAIL_HOST, PASSWORD, etc.

The next step is to push these changes to the AWS server using the command called eb deploy, followed by git add and git commit. Read more…

git add -A
git commit -m "allowed host modified"
eb deploy

Once the deployment is successful, run the CNAME again. You may get this error – deterministic=True requires SQLite 3.8.3 or higher.

This is telling that we need SQLite 3.8.3 or higher. But we don’t need to worry about this sqlite3, we are not using it. We are using AWS RDS Postgres database. So we can safely ignore this error

Follow the video to setup RDS PostgreSQL Database.

Get In Touch with Me!

Book a Call with Rathan - Get Personalized Guidance

Whether you’re just starting with Python, Django or aiming to elevate your skills to the next level, having a mentor can make a world of difference.

You May Also Like…

🎉 Enroll now & Save 15% on your entire cart 🎉

X
0