本文介绍如何将Django Web应用部署到函数计算。与传统的部署方法相比,您可将传统的Django应用一键部署至远端直接用于生产,跳过购买机器等步骤的同时,还拥有了弹性伸缩、按量付费和免运维等特性。

前提条件

您已完成以下操作:

背景信息

Django是基于Python语言开发的Web框架。使用Django,只要很少的代码,开发人员就可以轻松地完成一个正式网站所需要的大部分内容,并进一步开发出全功能的Web服务。关于Django的更多信息,请参见Django

操作步骤

  1. 在目标目录中创建以下命令,初始化Django项目。
    s init devsapp/start-django -d start-django
    -d用于指定项目名称,本示例以项目名称start-django为例,介绍如何迁移Django Web到函数计算。
    说明 若已存在该项目则跳过此步骤。
    输出示例:
     Serverless Awesome: https://github.com/Serverless-Devs/package-awesome
    
     devsapp!start-django@0.0.14.zip file decompression completed
     please select credential alias default
    
        ______ _
        |  _  (_)
        | | | |_  __ _ _ __   __ _  ___
        | | | | |/ _` | '_ \ / _` |/ _ \
        | |/ /| | (_| | | | | (_| | (_) |
        |___/ | |\__,_|_| |_|\__, |\___/
             _/ |             __/ |
            |__/             |___/
    
    
        Welcome to the start-django application
         This application requires to open these services:
             FC : https://fc.console.aliyun.com/
             ACR: https://cr.console.aliyun.com/
         This application can help you quickly deploy the Django project:
             Full yaml configuration: https://github.com/devsapp/django#%E5%AE%8C%E6%95%B4yaml
             Django development docs: https://docs.djangoproject.com/en/3.2/
         This application homepage: https://github.com/devsapp/start-django
    
    
    ‍ Thanks for using Serverless-Devs
     You could [cd /test/start-django] and enjoy your serverless journey!
    ️ If you need help for this example, you can use [s -h] after you enter folder.
     Document Star:https://github.com/Serverless-Devs/Serverless-Devs
  2. 执行以下命令,进入项目目录。
    cd start-django
  3. 执行以下命令部署项目。
    s deploy
    输出示例:
    [2021-09-03T11:49:11.322] [INFO ] [S-CLI] - Start ...
    [2021-09-03T11:49:11.332] [INFO ] [S-CLI] - Start the pre-action
    [2021-09-03T11:49:11.332] [INFO ] [S-CLI] - Action: pip3 install -t ./src -r ./src/requirements.txt -i https://mirrors.aliyun.com/pypi/simple/
    WARNING: Running pip install with root privileges is generally not a good idea. Try `pip3 install --user` instead.
    Collecting asgiref==3.3.1 (from -r ./src/requirements.txt (line 1))
      Using cached https://mirrors.aliyun.com/pypi/packages/89/49/5531992efc62f9c6d08a7199dc31176c8c60f7b2548c6ef245f96f29d0d9/asgiref-3.3.1-py3-none-any.whl
    Collecting Django==3.1.7 (from -r ./src/requirements.txt (line 2))
      Using cached https://mirrors.aliyun.com/pypi/packages/b8/6f/9a4415cc4fe9228e26ea53cf2005961799b2abb8da0411e519fdb74754fa/Django-3.1.7-py3-none-any.whl
    Collecting pytz==2021.1 (from -r ./src/requirements.txt (line 3))
      Using cached https://mirrors.aliyun.com/pypi/packages/70/94/784178ca5dd892a98f113cdd923372024dc04b8d40abe77ca76b5fb90ca6/pytz-2021.1-py2.py3-none-any.whl
    Collecting sqlparse==0.4.1 (from -r ./src/requirements.txt (line 4))
      Using cached https://mirrors.aliyun.com/pypi/packages/14/05/6e8eb62ca685b10e34051a80d7ea94b7137369d8c0be5c3b9d9b6e3f5dae/sqlparse-0.4.1-py3-none-any.whl
    Installing collected packages: asgiref, pytz, sqlparse, Django
    Successfully installed Django-3.1.7 asgiref-3.3.1 pytz-2021.1 sqlparse-0.4.1
    Target directory /test/start-django/src/asgiref-3.3.1.dist-info already exists. Specify --upgrade to force replacement.
    Target directory /test/start-django/src/sqlparse-0.4.1.dist-info already exists. Specify --upgrade to force replacement.
    Target directory /test/start-django/src/asgiref already exists. Specify --upgrade to force replacement.
    Target directory /test/start-django/src/django already exists. Specify --upgrade to force replacement.
    Target directory /test/start-django/src/sqlparse already exists. Specify --upgrade to force replacement.
    Target directory /test/start-django/src/pytz already exists. Specify --upgrade to force replacement.
    Target directory /test/start-django/src/pytz-2021.1.dist-info already exists. Specify --upgrade to force replacement.
    Target directory /test/start-django/src/Django-3.1.7.dist-info already exists. Specify --upgrade to force replacement.
    [2021-09-03T11:49:14.648] [INFO ] [S-CLI] - End the pre-action
      Using web framework type: nas, If you want to deploy with container, you can [s cli fc-default set web-framework container] to switch.
    (node:2908) [DEP0005] DeprecationWarning: Buffer() is deprecated due to security and usability issues. Please use the Buffer.alloc(), Buffer.allocUnsafe(), or Buffer.from() methods instead.
    (Use `node --trace-deprecation ...` to show where the warning was created)
      End of request
      Deployed.
      End of request
    [2021-09-03T11:49:19.688] [INFO ] [FC-DEPLOY] - Using region: cn-shenzhen
    [2021-09-03T11:49:19.690] [INFO ] [FC-DEPLOY] - Using access alias: default
    [2021-09-03T11:49:19.690] [INFO ] [FC-DEPLOY] - Using accessKeyID: 188077086902****
    [2021-09-03T11:49:19.690] [INFO ] [FC-DEPLOY] - Using accessKeySecret: KdcUEJLoGto****
      Using fc deploy type: sdk, If you want to deploy with pulumi, you can [s cli fc-default set deploy-type pulumi] to switch.
    [2021-09-03T11:49:19.936] [INFO ] [FC-DEPLOY] - Checking Service serverless-devs-django exists
    [2021-09-03T11:49:20.070] [INFO ] [FC-DEPLOY] - Setting role: AliyunFCDefaultRole
    [2021-09-03T11:49:20.249] [INFO ] [RAM] - Checking Role AliyunFCDefaultRole exists
    [2021-09-03T11:49:20.346] [INFO ] [RAM] - Updating role: AliyunFCDefaultRole
    [2021-09-03T11:49:20.462] [INFO ] [RAM] - Checking Plicy AliyunFCDefaultRolePolicy exists
    [2021-09-03T11:49:20.562] [INFO ] [FC-DEPLOY] - Using vpcConfig: auto: fc will try to generate related vpc resources automatically
    [2021-09-03T11:49:21.094] [INFO ] [VPC] - Getting vpc: fc-deploy-component-generated-vpc-cn-shenzhen
    [2021-09-03T11:49:21.192] [INFO ] [VPC] - Getting vswitch: fc-deploy-component-generated-vswitch-cn-shenzhen
    [2021-09-03T11:49:21.358] [INFO ] [VPC] - Getting securityGroup: fc-deploy-component-generated-securityGroup-cn-shenzhen
    [2021-09-03T11:49:21.484] [INFO ] [FC-DEPLOY] - Generated vpcConfig:
    securityGroupId: sg-wz92ctigpi221sm****
    vSwitchId: vsw-wz9qnuult4q5g4f7n****
    vpcId: vpc-wz9x9bzs0wtvjgt6n****
    
    [2021-09-03T11:49:21.484] [INFO ] [FC-DEPLOY] - Using nasConfig: auto: fc will try to generate related nas file system automatically
    [2021-09-03T11:49:38.106] [INFO ] [FC-DEPLOY] - Generated nasConfig:
    groupId: 10003
    mountPoints:
      - fcDir: /mnt/auto
        nasDir: /serverless-devs-django
        serverAddr: example.com
    userId: 10003
    
    [2021-09-03T11:49:38.108] [INFO ] [FC-DEPLOY] - Checking Function django exists
    [2021-09-03T11:49:38.249] [WARN ] [FC-DEPLOY] - Image registry.cn-shenzhen.aliyuncs.com/web-framework/python3:0.0.1 dose not exist locally.
    Maybe you need to run 's build' first if it dose not exist remotely.
    [2021-09-03T11:49:38.252] [INFO ] [FC-DEPLOY] - Checking Trigger serverless-devs-django exists
    [2021-09-03T11:49:38.345] [INFO ] [FC-DEPLOY] - Checking Trigger serverless-devs-django exists
    [2021-09-03T11:49:38.445] [INFO ] [FC-DEPLOY] - Creating service: serverless-devs-django
    [2021-09-03T11:49:38.446] [INFO ] [FC-DEPLOY] - Creating function: django
    [2021-09-03T11:49:38.446] [INFO ] [FC-DEPLOY] - Creating triggers: ["serverless-devs-django"]
      Make service serverless-devs-django success.
      Make function serverless-devs-django/django success.
      Make trigger serverless-devs-django/django/serverless-devs-django success.
    [2021-09-03T11:49:41.163] [INFO ] [FC-DEPLOY] - Checking Service serverless-devs-django exists
    [2021-09-03T11:49:41.246] [INFO ] [FC-DEPLOY] - Checking Function django exists
    [2021-09-03T11:49:41.376] [INFO ] [FC-DEPLOY] - Checking Trigger serverless-devs-django exists
    [2021-09-03T11:49:41.563] [INFO ] [FC-DEPLOY] - Creating custom domain: django.serverless-devs-django. 188077086902****.cn-shenzhen.fc.devsapp.net
    [2021-09-03T11:49:42.283] [INFO ] [FC-DOMAIN] - Creating custom domain: django.serverless-devs-django. 188077086902****.cn-shenzhen.fc.devsapp.net
    
    There is auto config in the service: serverless-devs-django
    [2021-09-03T11:49:42.392] [INFO ] [WEB-FRAMEWORK] - nas component get src is: ./src
    Packing ...
    Package complete.
      Start uploading file: /mnt/2bfb748534-tpm20-cn-shenzhen/serverless-devs-django/django/src.zip
      File uploaded successfully: /mnt/2bfb748534-tpm20-cn-shenzhen/serverless-devs-django/django/src.zip
      upload done
    [2021-09-03T11:50:57.611] [INFO ] [FC-NAS] - unzipping file
    [2021-09-03T11:51:12.460] [INFO ] [FC-NAS] - unzip done
      Try container acceleration
    framework-test:
      region: cn-shenzhen
      serviceName: serverless-devs-django
      functionName: django
      customDomains:
        - >-
          http://django.serverless-devs-django. 188077086902****.cn-shenzhen.fc.devsapp.net

执行结果

成功部署该项目后,您可以在执行输出中查看到函数计算生成的临时域名,通过该临时域名可以访问刚部署的应用,例如执行以下命令访问应用:

curl http://django.serverless-devs-django.188077086902****.cn-shenzhen.fc.devsapp.net
说明 临时域名仅用作演示以及开发,具有时效性。如需用作生产,请绑定已经在阿里云备案的域名。详细信息,请参见绑定自定义域名