问题
I've been trying to create a Django web app using VS2017 Preview (which includes Python tools for Visual Studio), and deploy the resulting app to Azure (I'm currently on the 30-day trial period to evaluate Azure).
I've done the following:
Start VS2017, create a new project with the "Django Web Project" template. This creates a Django webpage with the bootstrap template - simple, and everything works well locally.
In VS, go to Connected Services => Publish, select "Microsft Azure App Service", create a new App Service an an App Plan. The instances are created successfully.
Click "Publish" to publish via VS WebDeploy. Everything looks good in the console and it says
Publish: 1 succeeded, 0 failed, 0 skipped
at the end.
This results in the standard Azure Welcome-start-page hostingstart.html
showing, not the Django page. Once I remove that html file, there's only a The page cannot be displayed because an internal server error has occurred.
. I've tried various things: Going to portal.azure.com "Application Settings", setting Python version from "Off" to "3.4" (I'd like 3.5 in fact, which one of MS's tutorial uses - but any will do for now) - then there's just a hostingstart-python.html
showing, still no Django. I've tried adding a default web.config via "Add => New Item => Azure web.config (FastCGI)" in VS. I've tried editing that web.config with various values of WSGI_HANDLER
(e.g. django.core.handlers.wsgi.WSGIHandler()
) and DJANGO_SETTINGS_MODULE
(e.g. mydjangopage.settings
), I tried adding "wfastcgi" to requirements.txt, etc. Always just getting a server error.
I have been trying to do this for several hours now and I've read every possible Deployment help page from Microsoft, their blogs, and the web. All the information seems outdated, having missing pieces of information, or is just not working. At this point I'm quite disappointed and ready to give up. Isn't it supposed to be so simple? Create a new project in VS, hit "Publish", and it's supposed to work? (It's definitely not, I restarted from scratch multiple times and tried so many things.)
回答1:
I had the same problem. I solved it as follows:
Create a Django Web App via the azure portal:
- Go to azure and hit the "plus sign" in the left upper corner for adding a resource
- Kick on “Web and Mobile”
- Click on “Show all”
- Type “Django” into the search field
- Select the one that is marked in the picture below and create the resource by following the instructions

Deploy on the newly created resource
- Go to Visual Studio and start the deployment
- Instead of creating a new azure resource out of VS select the one you just created for the deployment.

Wait a moment.
- It takes about 3 minutes until you can see you deployment. Before that you will get a default screen that looks a bit different than the one you already know.
- After that you should see the correct django page.
回答2:
Right now, publishing support in VS 2017 is in a bit of a transition period. In the next couple of updates we want to get it back to a one-click system (and in the process, make it possible to publish Python apps from anywhere, not just within VS), but for now there are a couple of manual steps.
(I'll summarize the steps below, but the canonical documentation will be at https://aka.ms/PythonOnAppService - right now it's a blog post with these steps and some of the backstory)
After creating your new site (either through the portal or through VS - publishing content is okay too), install one of the Python site extensions
Configure your
web.config
file to have the correct path to the site extension you installed in thescriptProcessor
attribute (something likeD:\home\python361x64\python.exe
- see the description of each extension for the actual paths - VS 2017 also includes item templates to help set these up, so look through Add New Item for ideas)Update the
WSGI_HANDLER
andDJANGO_SETTINGS_MODULE
variables as necessary (a typical value forWSGI_HANDLER
for a Django app ismyapp.wsgi.application
, assuming you have awsgi.py
file in your project)Publish your site through VS.
Use the console to install your packages - e.g.
D:\home\python361x64\python.exe -m pip install -r requirements.txt
You may need to restart your site at this point if things were already running, but in general you can now publish quickly through VS without having to reinstall Python or any packages.
If you are deploying your site through ARM with a JSON template, you can also specify the site extension there: (from here)
"resources": [ { "apiVersion": "2015-08-01", "name": "[parameters('siteName')]", "type": "Microsoft.Web/sites", ... "resources": [ { "apiVersion": "2015-08-01", "name": "python352x64", "type": "siteextensions", "properties": { }, "dependsOn": [ "[resourceId('Microsoft.Web/sites', parameters('siteName'))]" ] }, ...
回答3:
Create a WebApp on Azure (Do not enable python in Application Settings !)
From deployment option select
local git repository
: Fromproperties
copy the git url:Copy
virtualenv_proxy.py
file from https://github.com/Azure-Samples/python-docs-hello-world into your folder.- Create a file
runtime.txt
and writepython-3.4
into it. - Create a file
web.3.4.config
with content :
<configuration> <appSettings> <add key="WSGI_ALT_VIRTUALENV_HANDLER" value="django.core.wsgi.get_wsgi_application()" /> <add key="WSGI_ALT_VIRTUALENV_ACTIVATE_THIS" value="D:\home\site\wwwroot\env\Scripts\python.exe" /> <add key="pythonpath" value="%SystemDrive%\home\site\wwwroot" /> <add key="WSGI_HANDLER" value="virtualenv_proxy.get_venv_handler()" /> <add key="WSGI_LOG" value="D:\home\LogFiles\wfastcgi.log"/> <add key="DJANGO_SETTINGS_MODULE" value="myModule.settings" /> </appSettings> <system.webServer> <modules runAllManagedModulesForAllRequests="true" /> <handlers> <add name="Python FastCGI" path="handler.fcgi" verb="*" modules="FastCgiModule" scriptProcessor="D:\Python34\python.exe|D:\Python34\Scripts\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>
The only settings you need to change here is the value for DJANGO_SETTINGS_MODULE
, replace myModule
with your module name. All the over settings and paths should be exactly the same if you created an Web App.
- Push everything to the git url you got at point 1, Azure will detect and set up
Python 3.4
and install all packages fromrequirements.txt
. After that everything should work. If not, connect using ftp and look into\LogFiles\wfastcgi.log
for the error.
来源:https://stackoverflow.com/questions/43506691/deploy-a-simple-vs2017-django-app-to-azure-server-error