Deploy Django-based application

onto Amazon Lightsail

Amazon Lightsail is the easiest way to get started on AWS. It offers virtual servers, storage, databases and networking, plus a cost-effective, monthly plan.

In this tutorial you deploy an example Django-based application onto Lightsail. You start by creating the instance, and deploying your application. Next, you ensure the application is running correctly using the Django’s built-in web server. Finally, you configure Apache to host the application.

Get started with Lightsail for free. 

Prerequisites

This is an intermediate level tutorial it assumes that you have:

  • A basic understanding of the Linux command line (cp, cat, etc.)
  • The ability to use a Linux text editor (vi, nano, etc)
  • At least a basic understanding of Django and Python
  • A free Amazon Web Services account
About this Tutorial
Time 30 minutes                                     
Cost Costs may apply
Use Case Compute
Products Amazon Lightsail
Audience Developer
Level Intermediate 
Last Updated March 23, 2020

Already have an account? Sign-in

1. Create a Lightsail instance

a. Sign into your AWS account and navigate to the Lightsail home page.


b. Click on Create Instance.

django1

c. Choose the appropriate AWS Region.

add django
django2

d. Choose an Availability Zone.

django3
django4

e. Under Select a blueprint choose Django.

 

django5

f. You can leave the other settings at their default values. Scroll to the bottom of the screen and click Create Instance.

 

2. Deploy a Django project

In this section you’re going to deploy a simple “Hello, World” application. You will use Django’s command line to create a new project as well as a new application before adding the actual application code in the next section.

If you already have a Django application written, feel free to use it for this tutorial. However, take note of the directory structure used in the “Hello, World” application, as it will be important when you configure your own application.

The Django project will be at the following directory

/opt/bitnami/apps/django/django_projects/tutorial

The project itself will be stored at the following directory

/opt/bitnami/apps/django/django_projects/tutorial/tutorial

The app will be created at the following directory

/opt/bitnami/apps/django/django_projects/tutorial/tutorial/hello_world


a. From the Lightsail home page click on the browser-based SSH client icon for your Django instance. This will start an SSH session into your instance. All the following commands should be run in that session.

django6

b. Enter the following commands to change into the django_projects directory, and create a new tutorial project.
cd /opt/bitnami/apps/django/django_projects && \
django-admin.py startproject tutorial

c. Enter the following command to change into the tutorial project directory, and create a new hello_world app.

cd tutorial && python3 manage.py startapp hello_world

3. Add your application code

Now that you have the scaffolding in place for your application, it’s time to add some code. The application will simply display “Hello, World” whenever the web page is loaded.

You should still be executing these commands in the Lightsail browser-based SSH client window.


a. Enter the following command to open the views.py file using the Vim text editor:

sudo vim /opt/bitnami/apps/django/django_projects/tutorial/hello_world/views.py

b. Press I to edit the file, and replace the existing code with the following:

from django.http import HttpResponse

def index(request):
     return HttpResponse("Hello, world")

c. Press Esc, type :wq! , and press Enter to save and quit Vim.


d. Enter the following command to create a urls.py file for your application in the same directory as the views.py file:

sudo vim /opt/bitnami/apps/django/django_projects/tutorial/hello_world/urls.py

e. Press I to edit the file, and add the following code:

from django.urls import path
from . import views

urlpatterns = [
     path('', views.index, name='index'),
]

f. Press the Esc, type :wq! , and press Enter to save and quit Vim.


g. Enter the following command to open the urls.py file:

sudo vim /opt/bitnami/apps/django/django_projects/tutorial/tutorial/urls.py

h. Press I to edit the file, and replace the existing code with the following:

from django.contrib import admin
from django.urls import include, path

urlpatterns = [
    path('', include('hello_world.urls')),
    path('admin/', admin.site.urls),
]

i. Press the Esc, type :wq! , and press Enter to save and quit Vim.


4. Test the application

Next, you’re going to use Django’s built-in web server to verify everything is working as expected. By default, Apache is using port 80 on your Lightsail instance, so you’ll run the Django web server on port 8000. To be able to access the website, you’ll need to open port 8000 on the Lightsail firewall.


a. Return to the Lightsail home page and click on the name of your Django instance.


b. From the horizontal menu click Networking.

django7

c. Under Firewall click Add Another.

django8

d. Under Port Range enter 8000 and click Save.

django9

e. Return to the Lightsail home page and make a note of the instance IP address.

django10

f. Return to the terminal session, and enter the following command to open the settings.py file:

sudo vim /opt/bitnami/apps/django/django_projects/tutorial/tutorial/settings.py

g. Press I and change the ALLOWED_HOSTS line to include the IP Address of your Lightsail instance as shown in the following example.

Note: When editing your settings.py file, be sure to replace 192.0.2.143 with the public IP address of your instance.

ALLOWED_HOSTS = ['192.0.2.143']

h. Press the Esc, type :wq! , and press Enter to save and quit Vim.


i. Enter the following command to start Django’s web server on port 8000:

cd /opt/bitnami/apps/django/django_projects/tutorial/ && python3 manage.py runserver 0.0.0.0:8000

j. Point your web browser at port 8000 of the instances IP address (e.g. http:// 192.0.2.143:8000).

You should see “Hello, world”, which confirms that your application is running.

5. Host the application using Apache

It’s not recommended to run production applications using Django’s built-in server. So, in this final section you’ll configure Apache to serve your application.

You’ll do this by configuring the application to use Web Services Gateway Interface (WSGI), and then create an Apache virtual host (vHost).

You should still be executing these commands in the Lightsail browser-based SSH client window.


a. Type the following command to edit the wsgi.py file:

sudo vim /opt/bitnami/apps/django/django_projects/tutorial/tutorial/wsgi.py

b. Type :1,$d and press Enter to select all, and delete the existing contents of the wsgi.py file..


c. Press I to edit, and add the following code to the wsgi.py file:

import os
import sys
sys.path.append('/opt/bitnami/apps/django/django_projects/tutorial')
os.environ.setdefault("PYTHON_EGG_CACHE", "/opt/bitnami/apps/django/django_projects/tutorial/egg_cache")
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "tutorial.settings")
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()

d. Press the Esc, type :wq! , and press Enter to save and quit Vim.


e. Enter the following command to create a conf directory for your application

mkdir /opt/bitnami/apps/django/django_projects/tutorial/conf

f. Enter the following command to create a httpd-app.conf file in the conf directory you just created:

sudo vim /opt/bitnami/apps/django/django_projects/tutorial/conf/httpd-app.conf

g. Press I to edit, and add the following code:

<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/tutorial/tutorial">
    Options +MultiViews
    AllowOverride All
    <IfVersion >= 2.3>
        Require all granted
    </IfVersion>

    WSGIProcessGroup wsgi-djangostack

    WSGIApplicationGroup %{GLOBAL}
</Directory>

Alias /tutorial/static "/opt/bitnami/apps/django/lib/python3.7/site-packages/Django-2.2.9-py3.7.egg/django/contrib/admin/static"
WSGIScriptAlias /tutorial '/opt/bitnami/apps/django/django_projects/tutorial/tutorial/wsgi.py'

h. Press the Esc, type :wq! , and press Enter to save and quit Vim.


i. Enter the following command to create a httpd-prefix.conf file in the conf directory for your application:

sudo vim /opt/bitnami/apps/django/django_projects/tutorial/conf/httpd-prefix.conf

j. Press I to edit, and add the following code:

Include "/opt/bitnami/apps/django/django_projects/tutorial/conf/httpd-app.conf"

k. Press the Esc, type :wq!, and press Enter to save and quit Vim.


l. Enter the following command to open the bitnami-apps-prefix.conf file:

sudo vim /opt/bitnami/apache2/conf/bitnami/bitnami-apps-prefix.conf

m. Press I to edit, and add the following line to the end of the file:

Include "/opt/bitnami/apps/django/django_projects/tutorial/conf/httpd-prefix.conf"

n. Press the Esc, type :wq! , and press Enter to save and quit Vim.


o. Enter the following command to open the bitnami.conf file:

sudo vim /opt/bitnami/apache2/conf/bitnami/bitnami.conf

p. Type :1,$d and press Enter in Vim to select all, and delete the contents of the bitnami.conf file


q. Press I to edit, and add the following code to the file:

<VirtualHost _default_:80>
    WSGIScriptAlias / /opt/bitnami/apps/django/django_projects/tutorial/tutorial/wsgi.py

    <Directory /opt/bitnami/apps/django/django_projects/tutorial>
    AllowOverride all
        Require all granted
        Options FollowSymlinks
    </Directory>

    DocumentRoot /opt/bitnami/apps/django/django_projects/tutorial
</VirtualHost>

r. Press the Esc, type :wq!, and press Enter to save and quit Vim.


s. Enter the following command to restart Apache:

sudo /opt/bitnami/ctlscript.sh restart apache

t. Point your web browser at the IP address of your instance (e.g. http:// 192.0.2.143).

You should see “Hello, world”, which confirms that your application is running.

Congratulations

Congratulations, you’ve completed the tutorial. You’ve successfully deployed a Django-based application on Lightsail.

Amazon Lightsail is a great choice to develop, build, and deploy a variety of applications like WordPress, websites, and blog platforms. 

Was this tutorial helpful?

Thank you
Please let us know what you liked.
Sorry to disappoint you
Is something out-of-date, confusing or inaccurate? Please help us improve this tutorial by providing feedback.