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.
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.
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.
** 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.
** [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
** [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.
** 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
I was able to deploy with the following procedure with the information of the official document + α.
I usually make a Django app. The following settings are required when deploying.
settings.py
DEBUG = False
ALLOW_HOST = ['*']
STATIC_ROOT = 'static'
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>;
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
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.
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.
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のログイン画面が表示されれば完了です。
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