Sunday, May 17, 2015

Django deployment headache with Azure Webapp

Cost is always the key factor in my decision making. Through the research, I found out that Microsoft Azure is offering some compiling package for startup compare to other big company. It comes with 3 years of $150 worth of Azure credit each month and also with the benefits of accessing other MS products. As for the startup, who just want to start to launch a business, I believe this can be quite cost effective compare of 12 months free-tier with Amazon AWS and also soon to be ended free server access with Heroku.

However, I will advise to start building on some easy to use server like Heroku, it's way too easy to deploy the code into that server within a day.

After a month of effort in activating the MSDN and Azure account due to my profile into the system. Hooray, but wait... how am I going to deploy the code? With Heroku, there's a very clear instruction in deploying the code for python. However, the article in Azure does not give much detailed on the steps to deploy a workable Django/python WebApp. I have been spending about a week to find out a way to deploy my existing django project to Azure environment. 

In this article, I will highlight the steps for Django deployment. The following is a simple architecture design that I am currently working on my Django project. 

1. Azure Webapp - for the website (front end)
2. Azure VM - for the postgresql and elasticsearch server (back end)

First challenge that I am facing.. how can VM and Webapp communicate and VM is not accessible by public. Without any gurus or experts with me as I am bootstrapping, I need to do the work myself. After some research, with different keywords search, I found a solution.

Azure Virtual Network with point-to-site-connectivity!!

Important: The virtual network should be created before start creating the VM. You can follow this link for the instructions to create virtual network.

Now, it's time to create the VM, I am using ubuntu server VM for postgresql and elasticsearch servers. I will not elaborate much in this blog, as the instructions is quite straightforward.

2nd challenge, no simple but detailed instructions in properly deploy a django project into Azure environment. Through some trial and error as well as getting clue from the instructions, I have finally managed to deploy the django project into Azure server successfully but it's taking too much time to do that. Hence, I decide to share the knowledge with others who may encounter the same issue.

1. Read this link to give you an overview on creating, and deploying Django to Azure Webapp.
2. Azure offers different management sites, like http://manage.windowsazure.com and http://portal.azure.com
2. I am using manage.windowsazure.com. Create a new webapp using custom gallery.
3. Follow the instructions and continue with the steps show on the screen, for example:
4. Once you have completed Azure will deploy the source code automatically.
5. However, this is not ready. You will be getting server error as due to missing files such as web.config, which is used by IIS of Azure.
6. Install Azure CLI to any environment you have. You can follow the instructions in Azure for the installation and understand the usage. Make sure you are connected to azure site with your command line.
7. Generate a custom deployment script with Azure CLI. With the following line on the root of your repository.
     azure site deploymentscript
8. A deployment script file will be generated with name, deploy.cmd. Edit the file to include the syncdb, and other relevant deployment steps that you normally performed with python manage.py command. *important: Please remember that you need to put --noinput at the end of syncdb as Azure deployment script will hang when an interaction is needed.
9. Once you have completed all the necessary changes for the settings.py and deploy.cmd. Check in you changes to the git repository, Azure will automatically pickup the changes and run the deployment.
10. Remember, you need to have web.config in the git too.

Voila. The deployment now should have include the steps to run syncdb, loaddata, createsuperuser or execute easy_install if you need to install packages specific for windows environment as Azure webapp is running on Windows.

Send me some comments if you have any questions or suggestions.

No comments: