This tutorial describes how to host a Django-based website on Microsoft 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:
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:
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.
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
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:
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 def home(request): html = "<html><body>Hello World!</body></html>" return HttpResponse(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'), )
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).
<VirtualHost *:80> ServerName yourVmUrl </VirtualHost> WSGIScriptAlias / /var/www/helloworld/helloworld/wsgi.py WSGIPythonPath /var/www/helloworld
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:
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.