简体   繁体   English

将简单的VS2017 Django应用程序部署到Azure - 服务器错误

[英]Deploy a simple VS2017 Django app to Azure - server error

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). 我一直在尝试使用VS2017 Preview(包括用于Visual Studio的Python工具)创建Django Web应用程序,并将生成的应用程序部署到Azure(我目前正处于30天试用期以评估Azure)。

I've done the following: 我做了以下事情:

  • Start VS2017, create a new project with the "Django Web Project" template. 启动VS2017,使用“Django Web Project”模板创建一个新项目。 This creates a Django webpage with the bootstrap template - simple, and everything works well locally. 这将创建一个带有引导程序模板的Django网页 - 简单,一切都在本地运行良好。

  • In VS, go to Connected Services => Publish, select "Microsft Azure App Service", create a new App Service an an App Plan. 在VS中,转到Connected Services => Publish,选择“Microsft Azure App Service”,创建一个新的App Service和一个App Plan。 The instances are created successfully. 实例已成功创建。

  • Click "Publish" to publish via VS WebDeploy. 单击“发布”以通过VS WebDeploy发布。 Everything looks good in the console and it says Publish: 1 succeeded, 0 failed, 0 skipped at the end. 在控制台中一切看起来都不错,它说Publish: 1 succeeded, 0 failed, 0 skipped最后。

This results in the standard Azure Welcome-start-page hostingstart.html showing, not the Django page. 这导致标准的Azure Welcome-start-page hostingstart.html显示,而不是Django页面。 Once I remove that html file, there's only a The page cannot be displayed because an internal server error has occurred. 删除该html文件后,只有一个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. 我尝试了各种各样的东西:转到portal.azure.com“应用程序设置”,将Python版本从“关闭”设置为“3.4”(事实上我想要3.5,MS的教程使用哪一个 - 但任何会做现在) - 然后只有一个hostingstart-python.html显示,仍然没有Django。 I've tried adding a default web.config via "Add => New Item => Azure web.config (FastCGI)" in VS. 我尝试通过VS中的“Add => New Item => Azure web.config(FastCGI)”添加默认的web.config。 I've tried editing that web.config with various values of WSGI_HANDLER (eg django.core.handlers.wsgi.WSGIHandler() ) and DJANGO_SETTINGS_MODULE (eg mydjangopage.settings ), I tried adding "wfastcgi" to requirements.txt, etc. Always just getting a server error. 我尝试使用各种值的WSGI_HANDLER (例如django.core.handlers.wsgi.WSGIHandler() )和DJANGO_SETTINGS_MODULE (例如mydjangopage.settings )编辑web.config,我尝试将“wfastcgi”添加到requirements.txt等。总是只是出现服务器错误。

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. 我一直试图这样做几个小时,我已经阅读了Microsoft,他们的博客和网络上的每个可能的部署帮助页面。 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? 在VS中创建一个新项目,点击“发布”,它应该有效吗? (It's definitely not, I restarted from scratch multiple times and tried so many things.) (绝对不是,我从头开始多次重启并尝试了很多东西。)

I had the same problem. 我有同样的问题。 I solved it as follows: 我解决了如下:

Create a Django Web App via the azure portal: 通过azure门户创建Django Web App:

  1. Go to azure and hit the "plus sign" in the left upper corner for adding a resource 转到azure并点击左上角的“加号”以添加资源
  2. Kick on “Web and Mobile” 踢“网络和移动”
  3. Click on “Show all” 点击“全部显示”
  4. Type “Django” into the search field 在搜索字段中输入“Django”
  5. Select the one that is marked in the picture below and create the resource by following the instructions 选择下图中标记的那个,然后按照说明创建资源

Deploy on the newly created resource 部署在新创建的资源上

  1. Go to Visual Studio and start the deployment 转到Visual Studio并开始部署
  2. Instead of creating a new azure resource out of VS select the one you just created for the deployment. 而不是从VS创建新的azure资源,而是选择刚刚为部署创建的资源。

Wait a moment. 稍等片刻。

  1. It takes about 3 minutes until you can see you deployment. 您需要大约3分钟才能看到部署。 Before that you will get a default screen that looks a bit different than the one you already know. 在此之前,您将获得一个与您已经知道的屏幕略有不同的默认屏幕。
  2. After that you should see the correct django page. 之后你应该看到正确的django页面。

Right now, publishing support in VS 2017 is in a bit of a transition period. 目前,VS 2017的发布支持处于一个过渡期。 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. 在接下来的两次更新中,我们希望将其恢复为一键式系统(并且在此过程中,可以从任何地方发布Python应用程序,而不仅仅是在VS中),但是现在有几个手动步骤。

(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) (我将总结以下步骤,但规范文档将在https://aka.ms/PythonOnAppService - 现在它是一篇博客文章,其中包含这些步骤和一些背景故事)

  1. After creating your new site (either through the portal or through VS - publishing content is okay too), install one of the Python site extensions 创建新站点后(通过门户网站或通过VS - 发布内容也可以),安装一个Python 站点扩展 通过Azure门户安装站点扩展

  2. Configure your web.config file to have the correct path to the site extension you installed in the scriptProcessor attribute (something like D:\\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) 配置web.config文件以获得在scriptProcessor属性中安装的站点扩展的正确路径(类似于D:\\home\\python361x64\\python.exe - 请参阅实际路径的每个扩展的描述 - VS 2017还包括项目模板,以帮助设置这些,所以通过添加新项目的想法)

  3. Update the WSGI_HANDLER and DJANGO_SETTINGS_MODULE variables as necessary (a typical value for WSGI_HANDLER for a Django app is myapp.wsgi.application , assuming you have a wsgi.py file in your project) 根据需要更新WSGI_HANDLERDJANGO_SETTINGS_MODULE变量(对于Django应用程序, WSGI_HANDLER的典型值是myapp.wsgi.application ,假设您的项目中有wsgi.py文件)

  4. Publish your site through VS. 通过VS发布您的网站。

  5. Use the console to install your packages - eg D:\\home\\python361x64\\python.exe -m pip install -r requirements.txt 使用控制台安装软件包 - 例如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. 如果事情已在运行,您可能需要在此时重新启动站点,但通常您现在可以通过VS快速发布,而无需重新安装Python或任何软件包。

If you are deploying your site through ARM with a JSON template, you can also specify the site extension there: (from here ) 如果您通过ARM使用JSON模板部署站点,还可以在那里指定站点扩展:(从此处

"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'))]"
        ]
      },
    ...
  1. Create a WebApp on Azure (Do not enable python in Application Settings !) 在Azure上创建WebApp(不要在应用程序设置中启用python!)

    From deployment option select local git repository : 从部署选项中选择local git repository 部署选项 From properties copy the git url: properties复制git url: 在此输入图像描述

  2. Copy virtualenv_proxy.py file from https://github.com/Azure-Samples/python-docs-hello-world into your folder. virtualenv_proxy.py文件从https://github.com/Azure-Samples/python-docs-hello-world复制到您的文件夹中。

  3. Create a file runtime.txt and write python-3.4 into it. 创建一个文件runtime.txt并将python-3.4写入其中。
  4. Create a file web.3.4.config with content : 使用内容创建web.3.4.config文件:
<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. 您需要在此处更改的唯一设置是DJANGO_SETTINGS_MODULE的值,将myModule替换为您的模块名称。 All the over settings and paths should be exactly the same if you created an Web App. 如果您创建了Web应用程序,则所有过度设置和路径应完全相同。

  1. Push everything to the git url you got at point 1, Azure will detect and set up Python 3.4 and install all packages from requirements.txt . 将所有内容推送到第1点的git url,Azure将检测并设置Python 3.4并安装requirements.txt所有包。 After that everything should work. 之后一切都应该有效。 If not, connect using ftp and look into \\LogFiles\\wfastcgi.log for the error. 如果没有,请使用ftp连接并查看\\LogFiles\\wfastcgi.log以查找错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM