将 Python Web 应用部署到 Azure Stack 中的 VMDeploy a Python web app to a VM in Azure Stack

可以创建一个 VM 来托管 Azure Stack 中的 Python Web 应用。You can create a VM to host your Python web app in Azure Stack. 在本文中,你将设置一个服务器,将该服务器配置为托管 Python Web 应用,然后将该应用部署到 Azure Stack。In this article, you set up a server, configure the server to host your Python web app, and then deploy the app to Azure Stack.

本文使用 Python 3.x 在 Nginx 服务器上的虚拟环境中运行 Flask。This article uses Python 3.x running Flask in a virtual environment on an Nginx server.

创建 VMCreate a VM

  1. 按照部署 Linux VM 以在 Azure Stack 中托管 Web 应用中的说明,在 Azure Stack 中设置 VM。Set up your VM in Azure Stack by following the instructions in Deploy a Linux VM to host a web app in Azure Stack.

  2. 在“VM 网络”窗格中,确保可以访问以下端口:In the VM network pane, make sure that the following ports are accessible:

    端口Port 协议Protocol 说明Description
    8080 HTTPHTTP 超文本传输协议 (HTTP) 是用于从服务器传递网页的协议。Hypertext Transfer Protocol (HTTP) is the protocol that's used to deliver webpages from servers. 客户端使用 DNS 名称或 IP 地址通过 HTTP 进行连接。Clients connect via HTTP with a DNS name or IP address.
    443443 HTTPSHTTPS 安全超文本传输协议 (HTTPS) 是 HTTP 的安全版本,它需要一个安全证书,并允许对信息进行加密传输。Hypertext Transfer Protocol Secure (HTTPS) is a secure version of HTTP that requires a security certificate and allows for the encrypted transmission of information.
    2222 SSHSSH 安全外壳 (SSH) 是一种用于安全通信的加密网络协议。Secure Shell (SSH) is an encrypted network protocol for secure communications. 你在 SSH 客户端上使用此连接来配置 VM 并部署应用。You use this connection with an SSH client to configure the VM and deploy the app.
    33893389 RDPRDP 可选。Optional. 远程桌面协议 (RDP) 允许远程桌面连接使用计算机的图形用户界面。The Remote Desktop Protocol (RDP) allows a remote desktop connection to use a graphic user interface on your machine.
    5000、80005000, 8000 “自定义”Custom 开发中的 Flask Web 框架使用的端口。The ports that are used by the Flask web framework in development. 对于生产服务器,通过 80 和 443 路由流量。For a production server, you route your traffic through 80 and 443.

安装 PythonInstall Python

  1. 使用 SSH 客户端连接到 VM。Connect to your VM by using your SSH client. 有关说明,请参阅使用 PuTTy 通过 SSH 进行连接For instructions, see Connect via SSH with PuTTy.

  2. 在 VM 上的 bash 提示符下,输入以下命令:At your bash prompt on your VM, enter the following command:

    sudo apt-get -y install python3 python3-venv python3-dev
    
  3. 验证安装。Validate your installation. 仍在 SSH 会话中连接到 VM 时,输入以下命令:While you're still connected to your VM in your SSH session, enter the following command:

        python -version
    
  4. 安装 Nginx(一个轻量级 Web 服务器)。Install Nginx, a lightweight web server. 仍在 SSH 会话中连接到 VM 时,输入以下命令:While you're still connected to your VM in your SSH session, enter the following command:

       sudo apt-get -y install nginx git
    
  5. 安装 Git(一种广泛分布的版本控制和源代码管理 (SCM) 系统)。Install Git, a widely distributed version control and source code management (SCM) system. 仍在 SSH 会话中连接到 VM 时,输入以下命令:While you're still connected to your VM in your SSH session, enter the following command:

       sudo apt-get -y install git
    

部署和运行应用Deploy and run the app

  1. 在 VM 上设置 Git 存储库。Set up your Git repository on the VM. 仍在 SSH 会话中连接到 VM 时,输入以下命令:While you're still connected to your VM in your SSH session, enter the following commands:

       git clone https://github.com/mattbriggs/flask-hello-world.git
    
       cd flask-hello-world
    
  2. 创建一个虚拟环境,在其中填充所有包依赖项。Create a virtual environment, and populate it with all the package dependencies. 仍在 SSH 会话中连接到 VM 时,输入以下命令:While you're still connected to your VM in your SSH session, enter the following commands:

    python3 -m venv venv
    source venv/bin/activate
    pip install -r requirements.txt
    
    export FLASK_APP=application.py
    # export FLASK_DEBUG=1 
    flask run -h 0.0.0.0
    
  3. 转到新服务器。Go to your new server. 应会看到你的 Web 应用程序正在运行。You should see your running web application.

       http://yourhostname.chinacloudapp.cn:5000
    

更新服务器Update your server

  1. 在 SSH 会话中连接到 VM。Connect to your VM in your SSH session. 通过键入 Ctrl+C 来停止服务器。Stop the server by typing Ctrl+C.

  2. 输入以下命令:Enter the following commands:

    deactivate
    open the git repo
    git pull
    
  3. 激活虚拟环境并启动应用:Activate the virtual environment and start the app:

    source venv/bin/activate
    export FLASK_APP=application.py
    flask run -h 0.0.0.0
    

后续步骤Next steps