练习 - 在 VM 上安装软件

已完成

我们最不希望在 VM 上尝试的便是安装 Web 服务器。 最易于安装的包之一是 nginx

注释

本练习可选做。 若要完成本练习,则需要在开始之前创建 Azure 订阅。 如果没有 Azure 帐户或不想暂时创建一个帐户,可以通读说明,以便了解所显示的信息。

注释

在本单元中,你将使用 Azure Cloud Shell 作为终端。 可以通过 Azure 门户Cloud Shell 登录访问 Cloud Shell。 无需在电脑或笔记本电脑上安装任何内容即可使用它。

注释

在本练习中,请将示例中的 myResourceGroupName 替换为现有资源组的名称或为此练习创建的资源组的名称。

安装 NGINX Web 服务器

  1. 查找 SampleVM Linux 虚拟机的公共 IP 地址。

    az vm list-ip-addresses --name SampleVM --output table
    
  2. 接下来,请使用上一步中的公共 IP 地址打开与 ssh连接。

    ssh azureuser@<PublicIPAddress>
    
  3. 登录虚拟机后,运行下面的命令以安装 nginx Web 服务器。 该命令需要几分钟时间才能完成。

    sudo apt-get -y update && sudo apt-get -y install nginx
    
  4. 退出安全外壳:

    exit
    

检索默认页面

  1. 在 Azure Cloud Shell 中,使用 curl,通过运行以下命令(将 <PublicIPAddress> 替换为之前找到的公共 IP),从 Linux Web 服务器读取默认页面。 也可以打开新的浏览器标签页,并尝试转到公共 IP 地址。

    curl -m 80 <PublicIPAddress>
    

    此命令会失败,因为 Linux 虚拟机不会通过可保护与虚拟机的网络连接的网络安全组公开端口 80 (http)。 可运行 Azure CLI 命令 vm open-port 修复该故障。

  2. 在 Cloud Shell 中输入以下命令以打开端口 80:

    az vm open-port \
        --port 80 \
        --resource-group "myResourceGroupName" \
        --name SampleVM
    

    需要一段时间,才能添加网络规则,并通过防火墙打开端口。

  3. 再次运行命令 curl

    curl -m 80 <PublicIPAddress>
    

    这次它应返回类似下面的数据。 也可以在浏览器中查看该页面。

    <!DOCTYPE html>
    <html>
    <head>
    <title>Welcome to nginx!</title>
    <style>
    body {
        width: 35em;
        margin: 0 auto;
        font-family: Tahoma, Verdana, Arial, sans-serif;
    }
    </style>
    </head>
    <body>
    <h1>Welcome to nginx!</h1>
    <p>If you see this page, the nginx web server is successfully installed and
    working. Further configuration is required.</p>
    
    <p>For online documentation and support, refer to
    <a href="http://nginx.org/">nginx.org</a>.<br/>
    Commercial support is available at
    <a href="http://nginx.com/">nginx.com</a>.</p>
    
    <p><em>Thank you for using nginx.</em></p>
    </body>
    </html>