【问题标题】:Deploying Django application with SQL Database to Azure将带有 SQL 数据库的 Django 应用程序部署到 Azure
【发布时间】:2018-12-31 15:33:33
【问题描述】:

我在本地 Windows 机器上构建了一个简单的 Django 应用程序,该应用程序通过利用 Django-Pyodbc-Azure 后端连接到托管在 Azure 上的 SQL Server。我可以在本地计算机上正常连接到数据库,并且我的应用程序运行没有问题。

但是,我没有将应用程序部署到 Azure 的应用服务,并且遇到了问题。部署本身运行没有问题,但是我的日志中显示以下错误消息:

Traceback (most recent call last): File "/home/site/wwwroot/antenv3.6/lib/python3.6/site-packages/sql_server/pyodbc/base.py", line 15, in <module>
import pyodbc as Database
ImportError: libodbc.so.2: cannot open shared object file: No such file or directory

File "/home/site/wwwroot/antenv3.6/lib/python3.6/site-packages/sql_server/pyodbc/base.py", line 17, in <module>
raise ImproperlyConfigured("Error loading pyodbc module: %s" % e)
django.core.exceptions.ImproperlyConfigured: Error loading pyodbc module: libodbc.so.2: cannot open shared object file: No such file or directory

我的 requirements.txt 文件如下所示:

Django==2.1.4
django-pyodbc-azure==2.1.0.0
pyodbc==4.0.25
pytz==2018.7

再一次......这在我的 Windows 机器上本地运行良好。但是当我部署到 Azure 时出现此错误。

我怀疑这与在 Azure 的基于 LINUX 的应用服务上未正确安装 Pyodbc 后端有关?有没有人有解决这个问题的经验?

【问题讨论】:

    标签: django azure pyodbc


    【解决方案1】:

    我在使用 Linux 版本时遇到了与 pyodbc 包相同的问题。我使用的是基于 Windows 的网络应用程序,但不得不从头开始重新构建并意外选择了 Linux 版本。

    切换回 Windows 版本后,我使用了 python 3.6 并按照以下步骤操作(基于此博客,用于部署烧瓶应用程序,https://blogs.msdn.microsoft.com/pythonengineering/2016/08/04/upgrading-python-on-azure-app-service/):

    将 Django Web 应用部署到 Azure

    1. 在门户中点击“Web App”>“New”
    2. 为 Web 应用程序命名:&lt;webappname&gt;
    3. 将资源组设置为与数据库相同
    4. 选择操作系统为 Windows
    5. 点击,部署 - 部署需要几分钟时间
    6. 部署后,您可以单击 URL https://&lt;webappname&gt;.azurewebsites.net 以查看默认的 azure 网页
    7. 在您的 Django 项目中,转到 settings.py 并在“ALLOWED_HOSTS”中添加“.azurewebsites.net”。
    8. 在 Azure 门户>webapp 中,转到 Extensions 并安装 python3.6 x64
    9. 在 Portal > webapp > Application settings,选择“Always on”。
    10. 在 settings.py 中设置:
      • DEBUG = os.getenv('DJANGO_DEBUG') != 'FALSE'
      • 还将其他私有变量设置为环境变量(这些应作为键值对添加(例如 DJANGO_DEBUG = FALSE)在 Portal>webapp>Application settings>Application settings。
    11. 另外,要连接到 SQL 数据库,请将数据库设置设置为:
      • OPTIONS[driver] = 'SQL Server Native Client 11.0'
      • OPTIONS[MARS_Connection] = 'True'
    12. 确保您的 django 项目的基目录中有以下文件:
      • ptvs_virtual_proxy.py
      • .SkipPythonDeployment
      • web.config 确保你有 key="DJANGO_SETTINGS_MODULE" value "&lt;django-project-name&gt;.settings"(见下文)
    13. 在您的本地项目中,运行pip freeze 并将内容放入requirements.txt(见下文)
    14. 通过运行git status 检查所有迁移文件是否已添加到 git
    15. 提交更改并将其推送到您的 git 存储库
    16. 在门户中,在您的新 Web 应用中转到“部署选项”
    17. “Choose Source” -> 按照步骤点击“Finish/Ok”查看部署进度和查看日志
    18. 一旦部署显示“成功”,转到 Kudu (http://&lt;webappname&gt;.scm.azurewebsites.net)>Powershell,然后运行 ​​D:\home\python364x64\python.exe -m pip install --upgrade -r D:\home\site\wwwroot\requirements.txt,这应该会成功,并且只需要在更新包时重新运行。
    19. 然后回到“部署中心”并点击日志按钮,然后点击重新部署,您可能还需要重启web-app以更新环境变量(进入webapp>overview>restart)。

    20. 对于静态文件,要么在本地运行python manage.py collectstatic后提交所有的管理文件,或者你可以设置像this这样的静态文件,然后在Kudu中运行D:\home\python364x64\python.exe D:\home\site\wwwroot\manage.py collectstatic

    web.config 文件:

    <configuration>
    <appSettings>
        <add key="WSGI_HANDLER" value="<django-project-name>.wsgi.application"/>
        <add key="PYTHONPATH" value="D:\home\site\wwwroot"/>
        <add key="WSGI_LOG" value="D:\home\LogFiles\wfastcgi.log"/>
        <add key="PYTHONPATH" value="D:\home\site\wwwroot" />
        <add key="DJANGO_SETTINGS_MODULE" value="<django-project-name>.settings" />
      </appSettings>
      <system.web>
        <compilation debug="true" targetFramework="4.0" />
        <customErrors mode="Off" />
      </system.web>
      <system.webServer>
        <modules runAllManagedModulesForAllRequests="true" />
        <httpErrors errorMode="Detailed"></httpErrors>
        <handlers>
          <add name="PythonHandler" path="handler.fcgi" verb="*" modules="FastCgiModule" scriptProcessor="D:\home\python364x64\python.exe|D:\home\python364x64\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>
    

    requirements.txt 文件:

    cycler==0.10.0
    Django==1.11.15
    django-pyodbc-azure==1.11.15.0
    djangorestframework==3.6.3
    djangorestframework-jsonp==1.0.2
    pyodbc==4.0.25
    

    【讨论】:

    • 好点@Pretasoc,我已经修改了我的答案以包括一步一步。
    【解决方案2】:

    请转到您的网络应用程序的 scm 站点,然后尝试手动安装引发错误的模块。

    详细步骤请参考此article

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-06-16
      • 2016-06-02
      • 2021-12-02
      • 2023-03-27
      • 2013-07-20
      • 1970-01-01
      相关资源
      最近更新 更多