Deploy a Python 3.6 / Django / Postgres web app on Azure

I tried to deploy a Django app using PostgreSQL to the App Service of Microsoft Azure, but I had a lot of trouble, so I will write it down.

Basic knowledge

What I found

I found out through various trials and errors with the document.

--The default OS of App Service is Windows --You can also select Linux as the OS, but currently there are restrictions as it is treated as a preview version --Deployable with Docker on Linux --The default deployment script supports Django static file placement --Only available Python versions 2.7 or 3.4 --Some modules cannot be installed with pip on Python 3.4 on Windows (such as psycopg2) --You can install other Python versions using extensions --In this case, you need to create the deploy command (command to execute pip) yourself.

It is natural that the OS is Windows because it is Microsoft, but it is easy to overlook the fact that Linux is a person who grew up in the world of common sense.

Official documentation

Azure Web Apps documentation

When I searched the page about Python from the menu, I found the following four.

I think it's best to try 1 first and then see 4. 2 is a tutorial, but be careful because it is a special usage of deploying with a Docker image on Linux which is a preview version. You don't have to look at 3 if you don't use Visual Studio.

1. Quick Start> Create Python App

** Create a Python web app in Azure **

--Procedures for deploying a simple app using Flask. --All required files are included in Git Sample Code. --Operations on Azure are performed using the Azure CLI. --Deployment is done by local Git, and git push will execute the deployment.

2. Tutorials> 1 --Python and PostgreSQL

** [Building Docker Python and PostgreSQL apps on Azure](https://docs.microsoft.com/en-us/azure/app-service-web/app-service-web-tutorial-docker-python-postgresql -app) **

--This is an example of an application that uses PostgreSQL in Flask. --It describes how to deploy with a Docker image. -Sample code

3. How-to Guide> App Development> Python> Django App Using MySQL

** [Django and MySQL on Azure with Python Tools 2.2 for Visual Studio](https://docs.microsoft.com/en-us/azure/app-service-web/web-sites-python-ptvs- django-mysql) **

--The development method using Python Tools 2.2 for Visual Studio (PTVS) is written.

4. How-to Guide> App Development> Runtime Configuration> Python

** Configure Python with Azure App Service Web Apps **

--The description of the following files required for deployment is written. - requirements.txt - runtime.txt - web.config - ptvs_virtualenv_proxy.py - .deployment - deploy.cmd

Deploy a Python3.6 / Django / PostgreSQL web app

I was able to deploy with the following procedure with the information of the official document + α.

  1. Create a Django app locally --Create a PostgreSQL database in Azure --Create an App Service on Azure --Install Python 3.6 as an extension on App Service --Create a file for deployment --Deploy the Django app

1. Create a Django app locally

I usually make a Django app. The following settings are required when deploying.

settings.py


DEBUG = False
ALLOW_HOST = ['*']
STATIC_ROOT = 'static'

2. Create a PostgreSQL database in Azure

Building Docker Python and PostgreSQL apps on Azure, Create a PostgreSQL database in Azure by referring to "Creating an operational PostgreSQL database".

AZ_GROUP="<resource_group>"
AZ_LOCATION="japanwest"
AZ_PG="<postgresql_name>"
AZ_PG_USER="<admin_username>"
AZ_PG_PASS="<admin_password>"

az postgres server create -g $AZ_GROUP -n $AZ_PG -l $AZ_LOCATION -u $AZ_PG_USER -p $AZ_PG_PASS
az postgres server firewall-rule create -g $AZ_GROUP --server-name $AZ_PG --start-ip-address=0.0.0.0 --end-ip-address=255.255.255.255 --name AllowAllIPs

Create the database and user.

$ psql -h ${AZ_PG}.postgres.database.azure.com -U ${AZ_PG_USER}@${AZ_PG} postgres

python


CREATE DATABASE <database_name>;
CREATE USER <user> WITH PASSWORD '<password>';
GRANT ALL PRIVILEGES ON DATABASE <database_name> TO <user>;

3. Create an App Service on Azure

Refer to Create Python Web App in Azure and follow the following. Execute the command.

AZ_GROUP="<resource_group>"
AZ_LOCATION="japanwest"
AZ_APPSERVICE_PLAN="<plan_name>"
AZ_APPSERVICE_PLAN_TYPE="Free"
AZ_APP_NAME="<app_name>"

az group create -n $AZ_GROUP -l $AZ_LOCATION
az appservice plan create -n $AZ_APPSERVICE_PLAN -g $AZ_GROUP --sku $AZ_APPSERVICE_PLAN_TYPE
az webapp create -n $AZ_APP_NAME -g $AZ_GROUP --plan $AZ_APPSERVICE_PLAN --deployment-local-git

4. Install Python 3.6 as an extension on App Service

Upgrading Python on Azure App Service – Python Engineering at Microsoft

Select the App Service you created from the screen of the Azure portal> Select the extension> Select the Python version you want to install and install it.

5. Create a file for deployment

Create the following files.

requirements.txt


Django
psycopg2

.deployment


[config]
command = deploy.cmd

deploy.cmd copies the default one and changes the path of python.exe and pip to match the version.

deploy.cmd


@if "%SCM_TRACE_LEVEL%" NEQ "4" @echo off

:: ----------------------
:: KUDU Deployment Script
:: Version: 1.0.15
:: ----------------------

SET PYTHON_HOME=D:\home\python361x86
SET PYTHON=%PYTHON_HOME%\python.exe
SET PIP=%PYTHON% -m pip

:: Prerequisites
:: -------------

:: Verify node.js installed
where node 2>nul >nul
IF %ERRORLEVEL% NEQ 0 (
  echo Missing node.js executable, please install node.js, if already installed make sure it can be reached from current environment.
  goto error
)

:: Setup
:: -----

setlocal enabledelayedexpansion

SET ARTIFACTS=%~dp0%..\artifacts

IF NOT DEFINED DEPLOYMENT_SOURCE (
  SET DEPLOYMENT_SOURCE=%~dp0%.
)

IF NOT DEFINED DEPLOYMENT_TARGET (
  SET DEPLOYMENT_TARGET=%ARTIFACTS%\wwwroot
)

IF NOT DEFINED NEXT_MANIFEST_PATH (
  SET NEXT_MANIFEST_PATH=%ARTIFACTS%\manifest

  IF NOT DEFINED PREVIOUS_MANIFEST_PATH (
    SET PREVIOUS_MANIFEST_PATH=%ARTIFACTS%\manifest
  )
)

IF NOT DEFINED KUDU_SYNC_CMD (
  :: Install kudu sync
  echo Installing Kudu Sync
  call npm install kudusync -g --silent
  IF !ERRORLEVEL! NEQ 0 goto error

  :: Locally just running "kuduSync" would also work
  SET KUDU_SYNC_CMD=%appdata%\npm\kuduSync.cmd
)
goto Deployment

::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:: Deployment
:: ----------

:Deployment
echo Handling python deployment.

:: 1. KuduSync
IF /I "%IN_PLACE_DEPLOYMENT%" NEQ "1" (
  call :ExecuteCmd "%KUDU_SYNC_CMD%" -v 50 -f "%DEPLOYMENT_SOURCE%" -t "%DEPLOYMENT_TARGET%" -n "%NEXT_MANIFEST_PATH%" -p "%PREVIOUS_MANIFEST_PATH%" -i ".git;.hg;.deployment;deploy.cmd"
  IF !ERRORLEVEL! NEQ 0 goto error
)

IF NOT EXIST "%DEPLOYMENT_TARGET%\requirements.txt" goto postPython

pushd "%DEPLOYMENT_TARGET%"

:: 4. Install packages
echo Pip install requirements.

%PIP% install -r requirements.txt
IF !ERRORLEVEL! NEQ 0 goto error

REM Add additional package installation here
REM -- Example --
REM env\scripts\easy_install pytz
REM IF !ERRORLEVEL! NEQ 0 goto error

:: 5. Copy web.config
IF EXIST "%DEPLOYMENT_SOURCE%\web.%PYTHON_VER%.config" (
  echo Overwriting web.config with web.%PYTHON_VER%.config
  copy /y "%DEPLOYMENT_SOURCE%\web.%PYTHON_VER%.config" "%DEPLOYMENT_TARGET%\web.config"
)

:: 6. Django collectstatic
IF EXIST "%DEPLOYMENT_TARGET%\manage.py" (
  echo Collecting Django static files. You can skip Django specific steps with a .skipDjango file.
  IF NOT EXIST "%DEPLOYMENT_TARGET%\static" (
    MKDIR "%DEPLOYMENT_TARGET%\static"
  )
  %PYTHON% manage.py collectstatic --noinput --clear
)

popd

:postPython

::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
goto end

:: Execute command routine that will echo out when error
:ExecuteCmd
setlocal
set _CMD_=%*
call %_CMD_%
if "%ERRORLEVEL%" NEQ "0" echo Failed exitCode=%ERRORLEVEL%, command=%_CMD_%
exit /b %ERRORLEVEL%

:error
endlocal
echo An error has occurred during web site deployment.
call :exitSetErrorLevel
call :exitFromFunction 2>nul

:exitSetErrorLevel
exit /b 1

:exitFromFunction
()

:end
endlocal
echo Finished successfully.

web.config


<?xml version="1.0"?>
<configuration>
  <appSettings>
    <add key="PYTHONPATH" value="D:\home\site\wwwroot" />
    <add key="WSGI_HANDLER" value="django.core.wsgi.get_wsgi_application()" />
    <add key="WSGI_LOG" value="D:\home\LogFiles\wfastcgi.log" />
    <add key="DJANGO_SETTINGS_MODULE" value="project.settings" />
  </appSettings>
  <system.webServer>
    <handlers>
      <remove name="Python27_via_FastCGI" />
      <remove name="Python34_via_FastCGI" />
      <add name="Python FastCGI"
           path="handler.fcgi"
           verb="*"
           modules="FastCgiModule"
           scriptProcessor="D:\home\python361x86\python.exe|D:\home\python361x86\wfastcgi.py"
           resourceType="Unspecified"
           requireAccess="Script" />
    </handlers>
    <rewrite>
      <rules>
        <rule name="Static Files" stopProcessing="true">
          <conditions>
            <add input="true" pattern="false" />
          </conditions>
        </rule>
        <rule name="Configure Python" stopProcessing="true">
          <match url="(.*)" ignoreCase="false" />
          <conditions>
            <add input="{REQUEST_URI}" pattern="^/static/.*" ignoreCase="true" negate="true" />
          </conditions>
          <action type="Rewrite" url="handler.fcgi/{R:1}" appendQueryString="true" />
        </rule>
      </rules>
    </rewrite>
  </system.webServer>
</configuration>

Change the DJANGO_SETTINGS_MODULE part to match your Django project.

6. Deploy the Django app

AZ_USER="<user>"
AZ_PASS="<password>"
az webapp deployment user set --user-name $AZ_USER --password $AZ_PASS
git remote add azure <git_url>
git push azure master

The git URL will be displayed when you do ʻaz webapp create`. You can also see the App Service overview on the portal.

ブラウザで<app_name>.azurewebsites.net/adminにアクセスしてadminのログイン画面が表示されれば完了です。

Impressions

It was pretty swayed because of the structure of the official documentation. I was confused because the tutorial part explained a completely different way. .. ..

Recommended Posts

Deploy a Python 3.6 / Django / Postgres web app on Azure
Deploy a Django application on Google App Engine (Python3)
Deploy a Django app made with PTVS on Azure
Implement a Django app on Hy
Sample to put Python Flask web app on Azure App Service (Web App)
(Failure) Deploy a web app made with Flask on heroku
Deploy the Django app on Heroku [Part 2]
Deploy the Django app on Heroku [Part 1]
Create and deploy a Django (PTVS) app using Azure Table storage
Daemonize a Python web app with Supervisor
How to deploy a Django app on heroku in just 5 minutes
Deploy a Python app on Google App Engine and integrate it with GitHub
Vienna with Python + Flask web app on Jenkins
Django python web framework
Looking back on creating a web service with Django 1
[Beginner] [Python / Django] A fledgling web engineer tried a Django tutorial-Part 7-
(Python) Try to develop a web application using Django
[Beginner] [Python / Django] A fledgling web engineer tried a Django tutorial-Part 1-
How to deploy a Django application on Alibaba Cloud
[Beginner] [Python / Django] A fledgling web engineer tried a Django tutorial-Part 2-
[Beginner] [Python / Django] A fledgling web engineer tried a Django tutorial-Part 0-
[Beginner] [Python / Django] A fledgling web engineer tried a Django tutorial-Part 5-
How to build a Django (python) environment on docker
[Beginner] [Python / Django] A fledgling web engineer tried a Django tutorial-Part 6-
Steps from installing Python 3 to creating a Django app
Looking back on creating a web service with Django 2
[Beginner] [Python / Django] A fledgling web engineer tried a Django tutorial-Part 4-
How to use Django on Google App Engine / Python
Write code to Unit Test a Python web app
[Beginner] [Python / Django] A fledgling web engineer tried a Django tutorial-Part 3-
Deploy a real-time web app with swampdragon x apache
Deploy a web app created with Streamlit to Heroku
Web App Development Practice: Create a Shift Creation Page with Django! (Experiment on admin page)
Deploy masonite app on Heroku 2020
[Django3] Display a web page in Django3 + WSL + Python virtual environment
Deploy a Django application on EC2 with Nginx + Gunicorn + Supervisor
Building a Python environment on Mac
If you know Python, you can make a web application with Django
Building a Python environment on Ubuntu
[Python / Django] Create a web API that responds in JSON format
Create a Python environment on Mac (2017/4)
# 3 Build a Python (Django) environment on AWS EC2 instance (ubuntu18.04) part2
Web App Development Practice: Create a Shift Creation Page with Django! (Shift creation page)
Deploy a Django application with Docker
Deploy your Django application on Heroku
python + django + scikit-learn + mecab (1) on heroku
python + django + scikit-learn + mecab (2) on heroku
Deploy Flask app on heroku (bitterly)
Create a python environment on centos
Run python3 Django1.9 with mod_wsgi (deploy)
Build a web application with Django
Deploy the Flask app on Heroku
Make a scraping app with Python + Django + AWS and change jobs
How to deploy a web application on Alibaba Cloud as a freelancer
Deploy the Flask app on heroku
Launched a web application on AWS with django and changed jobs
Build a python3 environment on CentOS7
Until you deploy a SpringBoot project in Gradle on App Engine Flexible
Web App Development Practice: Create a Shift Creation Page with Django! (Introduction)
Build your Django app on Docker and deploy it to AWS Fargate
Until you publish (deploy) a web application made with bottle on Heroku