Django Hello World Web Application (mac-linux)
This tutorial describes how to host a Django-based website on Windows Azure using a Linux virtual machine. This tutorial assumes you have no prior experience using Azure. Upon completing this guide, you will have a Django-based application up and running in the cloud.
You will learn how to:
- Setup an Azure virtual machine to host Django. While this tutorial explains how to accomplish this under Linux, the same could also be done with a Windows Server VM hosted in Azure.
- Create a new Django application from Linux.
By following this tutorial, you will build a simple Hello World web application. The application will be hosted in an Azure virtual machine.
A screenshot of the completed application is below:
Creating and configuring an Azure virtual machine to host Django
- Follow the instructions given here to create an Azure virtual machine of the Ubuntu Server 14.04 LTS distribution.
Note: you only need to create the virtual machine. Stop at the section titled How to log on to the virtual machine after you create it.
- Instruct Azure to direct port 80 traffic from the web to port 80 on the virtual machine:
- Navigate to your newly created virtual machine in the Azure Portal and click the ENDPOINTS tab.
- Click the ADD button at the bottom of the screen.
- Open up the TCP protocol's PUBLIC PORT 80 as PRIVATE PORT 80.
Setting up the development environment
Note: If you need to install Python or would like to use the Client Libraries, please see the Python Installation Guide.
The Ubuntu Linux VM already comes with Python 2.7 pre-installed, but it doesn't have Apache or django installed. Follow these steps to connect to your VM and install Apache and django.
Launch a new Terminal window.
Enter the following command to connect to the Azure VM.
$ ssh yourusername@yourVmUrl
Enter the following commands to install django:
$ sudo apt-get install python-setuptools
$ sudo easy_install django
Enter the following command to install Apache with mod-wsgi:
$ sudo apt-get install apache2 libapache2-mod-wsgi
Creating a new Django application
Open the Terminal window you used in the previous section to ssh into your VM.
Enter the following commands to create a new Django project:
$ cd /var/www
$ sudo django-admin.py startproject helloworld
The django-admin.py script generates a basic structure for Django-based websites:
- helloworld/manage.py helps you to start hosting and stop hosting your Django-based website
- helloworld/helloworld/settings.py contains Django settings for your application.
- helloworld/helloworld/urls.py contains the mapping code between each url and its view.
Create a new file named views.py in the /var/www/helloworld/helloworld directory. This will contain the view that renders the "hello world" page. Start your editor and enter the following:
from django.http import HttpResponse
html = "<html><body>Hello World!</body></html>"
Now replace the contents of the urls.py file with the following:
from django.conf.urls import patterns, url
urlpatterns = patterns('',
url(r'^$', 'helloworld.views.home', name='home'),
Setting up Apache
Create an Apache virtual host configuration file /etc/apache2/sites-available/helloworld.conf. Set the contents to the following, and make sure to replace yourVmUrl with the actual URL of the machine you are using (for example pyubuntu.cloudapp.net).
WSGIScriptAlias / /var/www/helloworld/helloworld/wsgi.py
Enable the site with the following command:
$ sudo a2ensite helloworld
Restart Apache with the following command:
$ sudo service apache2 reload
Finally, load the web page in your browser:
Shutting down your Azure virtual machine
When you're done with this tutorial, shutdown and/or remove your newly created Azure virtual machine to free up resources for other tutorials and avoid incurring Azure usage charges.