Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Deploying a Python Web App on Azure

NOTE: For more up-to-date instructions and more explanations, please see the readme of https://github.com/jtanx/isso-azure

Basic instructions

  1. Add a new Web App

  2. Go to https://my-web-app-name.scm.azurewebsites.net and click on Site Extensions

  3. Install the Python 2.7.12 x64 and IIS Manager extensions. Click on Restart site when both are installed.

  4. Under the installed site extensions, click on the play (▶) button for IIS Manager.

  5. Go to the applicationHost.xdt tab

  6. Enter the following contents:

    <?xml version="1.0"?>
    <configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
      <system.webServer>
        <fastCgi>
          <application
            fullPath="D:\home\Python27\python.exe"
            arguments="D:\home\Python27\wfastcgi.py"
            maxInstances="16"
            idleTimeout="21600"
            instanceMaxRequests="10000000"
            signalBeforeTerminateSeconds="60"
            xdt:Transform="InsertIfMissing"
            xdt:Locator="Match(fullPath)">
            <environmentVariables>
              <environmentVariable name="PYTHONHOME" value="D:\home\Python27" />
            </environmentVariables>
          </application>
        </fastCgi>
      </system.webServer>
      <location path="%XDT_SITENAME%" xdt:Transform="InsertIfMissing" xdt:Locator="Match(path)">
        <system.webServer xdt:Transform="InsertIfMissing">
          <applicationInitialization xdt:Transform="InsertIfMissing">
            <add initializationPage="/"  xdt:Transform="InsertIfMissing"/>
          </applicationInitialization>
    
          <rewrite xdt:Transform="InsertIfMissing">
            <rules xdt:Transform="InsertIfMissing">
              <rule name="Force HTTPS" enabled="true" stopProcessing="true">
                <match url="(.*)" ignoreCase="false" />
                <conditions>
                  <add input="{HTTPS}" pattern="off" />
                  <add input="{WARMUP_REQUEST}" pattern="1" negate="true" />
                </conditions>
                <action type="Redirect" url="https://{HTTP_HOST}/{R:1}" appendQueryString="true" redirectType="Permanent" />
              </rule>
            </rules>
          </rewrite>    
        </system.webServer>
      </location>
    </configuration>

    You may omit the <location> block if you wish, but this forces HTTPS.

  7. Click on Save XDT. Stop the application from the portal, before starting again (I find that it might take more than one restart for the change to pick up)

  8. On the client side, modify the deployment script to use the extension Python:

    npm install -g azure-cli
    azure config mode asm
    azure site deploymentscript --python
    

    Replace everything in the SelectPythonVersion section in deploy.cmd with:

    :SelectPythonVersion
    SET PYTHON_RUNTIME=python-2.7
    SET PYTHON_VER=2.7
    SET PYTHON_EXE=%SYSTEMDRIVE%\home\python27\python.exe
    SET PYTHON_ENV_MODULE=virtualenv

    The goal is to replace D:\Python27 with D:\home\Python27

  9. The contents of web.config should be similar to below:

    <?xml version="1.0"?>
    <configuration>
      <appSettings>
        <add key="WSGI_ALT_VIRTUALENV_HANDLER" value="wsgi.application" />
        <add key="WSGI_ALT_VIRTUALENV_ACTIVATE_THIS"
             value="D:\home\site\wwwroot\env\Scripts\activate_this.py" />
        <add key="WSGI_HANDLER"
             value="ptvs_virtualenv_proxy.get_virtualenv_handler()" />
        <add key="WSGI_LOG" value="D:\home\site\wwwroot\log.txt" />
        <add key="PYTHONPATH" value="D:\home\site\wwwroot" />
      </appSettings>
      <system.web>
        <compilation debug="true" targetFramework="4.0" />
      </system.web>
      <system.webServer>
        <handlers>
          <add name="PythonHandler" path="*" verb="*" modules="FastCgiModule" scriptProcessor="D:\home\Python27\python.exe|D:\home\Python27\wfastcgi.py" resourceType="Unspecified" requireAccess="Script"/>
        </handlers>
      </system.webServer>
    </configuration>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.