wfastcgi.py provides a bridge between IIS and Python using WSGI and FastCGI, similar to what mod_python provides for Apache HTTP Server.
Even though using wfastcgi was preferred way to run Python apps in IIS. I would like to provide an alternate solution using HTTP Platform Handler in this blog.
Below are list of steps we would follow
- Create Sample Project
- Create Azure WebApp and Use Site Extension to Upgrade Python
- Create and Change Deployment script
- Adding web.config (for production app use second option with waitress)
- Publish App
You can find a Sample Python Django project with above operations @ GitHub Link
Create Sample Project
Follow https://www.djangoproject.com/start/ to get started with Django
- Install Django and
- Write your first Django app
(or)
If you have PyCharm, Follow details at below link to create a sample app
https://www.jetbrains.com/help/pycharm/2016.1/creating-django-project.html
Create Azure WebApp and Use Site Extension to Upgrade Python
Navigate to Azure portal
- Create a new web app
- Setup Continuous Deployment
- Navigate to your App Service blade, select Extensions and then Add.
- From the list of extensions, scroll down until you spot the Python logos, then choose the version you need
For this blog I’m choosing Python 2.7.12 x64
, It would install new version of python @ D:\home\Python27
Create and Change Deployment script
I have explained more on how to utilize deployment script and what it does @ link. you can use this script to run commands on Azure App after moving code to it.
Install Azure Cli using below command
Use Below command to create deployment script.
Above step would create below two files
- .deployment
- deploy.cmd
Note: If you haven’t used nodejs/npm earlier, Create above files manually and copy content for these files from https://github.com/prashanthmadi/azure-django-httphandler
Replace content of deploy.cmd file with content at Link
We are changing default deployment script(removing virtual environment creation and others) and adding below content to install packages listed in requirements.txt
Adding web.config(Development Mode)
Note: Ignore this section for production app
Azure web apps would use IIS which can be configured using web.config file. In our web.config file we would ask the requests to use httpplatformhandler and forward them to python.exe
Create below file at root folder and copy content from link
web.config
Adding web.config(Production Mode - Using Waitress)
Waitress is meant to be a production-quality pure-Python WSGI server with very acceptable performance
Django app by default uses a development server which would be started when we execute manage.py file. I would provide steps to use Waitress instead below
Create web.config
file with below content
If you observe carefully, we have changed
- httpplatform arguments section to
run_waitress_server.py
instead ofmanage.py runserver %HTTP_PLATFORM_PORT%
- Added new evnironmentVariable
PORT
to pass the internal port number we use in waitress
Add below line at the end in requirements.txt
file. This would install waitress module during deployment
Create a new file run_waitress_server.py
with below content
I have seen some Azure users facing issue with ssl. set url_scheme
to resolve the issue as below
Publish App
Navigate to your root folder and commit your changes to WEB_APP_GIT_URL
Serving Static Files
I have seen couple of users complaining about static files. Here is the fix for it
Use below line of code(at D:\home\site\wwwroot
using kudu console) to generate static file folder in Django app, we can also stitch this inside deploy.cmd to automate it. Version number
in url may change based on your python installation.
Once you have static folder in D:\home\site\wwwroot
folder as above screenshot
- Navigate to Azure Portal and add a new section in
Virtual application and directories
underApplication Settings
with below values - Navigate to
D:\home\site\wwwroot\static
folder and add web.config file with below content
D:\home\site\wwwroot\static\web.config content
当在复杂的环境中面临问题,格物之道需:浊而静之徐清,安以动之徐生。 云中,恰是如此!