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

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

创建 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.
    13371337 “自定义”Custom Node.js 使用的端口。The port that's used by Node.js. 对于生产服务器,通过 80 和 443 路由流量。For a production server, you route your traffic through 80 and 443.

安装 NodeInstall Node

  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 the bash prompt on your VM, enter the following command:

      sudo apt install nodejs-legacy
    
  3. 安装 NPM(Node.js 包或模块的包管理器)。Install NPM, a package manager for Node.js packages, or modules. 仍在 SSH 会话中连接到 VM 时,输入以下命令:Still connected to your VM in your SSH session, enter the following command:

       go version
    
  4. 安装 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
    
  5. 验证安装。Validate your installation. 仍在 SSH 会话中连接到 VM 时,输入以下命令:While you're still connected to your VM in your SSH session, enter the following command:

       node -v
    

部署和运行应用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/Azure-Samples/nodejs-docs-hello-world.git
    
       cd nodejs-docs-hello-world
        npm start
    
  2. 启动应用。Start the app. 仍在 SSH 会话中连接到 VM 时,输入以下命令:While you're still connected to your VM in your SSH session, enter the following command:

       sudo node app.js
    
  3. 转到新服务器。Go to your new server. 应会看到你的 Web 应用程序正在运行。You should see your running web application.

       http://yourhostname.chinacloudapp.cn:1337
    

后续步骤Next steps