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

可以创建一个虚拟机 (VM) 来托管 Azure Stack Hub 中的 Java Web 应用。You can create a virtual machine (VM) to host your Java web app in Azure Stack Hub. 在本文中,你将在 Azure Stack Hub 中的 Linux VM 上安装 Apache Tomcat 服务器,然后打开正确的端口并对该服务器进行配置。In this article, you install, open the right ports, and configure an Apache Tomcat server on a Linux VM in Azure Stack Hub. 然后,将 Java Web 应用程序资源 (WAR) 文件加载到该服务器。You then load a Java Web Application Resource (WAR) file into the server. WAR 文件用于分发 Java 存档 (JAR) 文件,以及包含 Java 资源(例如类、文本、图像、XML 和 HTML)其他用于交付 Web 应用程序的资源的压缩文件。A WAR file is used to distribute a collection of Java archive (JAR) files, compressed files that contain Java resources such as classes, text, images, XML, and HTML, and other resources that are used to deliver a web application.

创建 VMCreate a VM

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

  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.
    80808080 “自定义”Custom Apache Tomcat 服务的默认端口。The default port for the Apache Tomcat service. 对于生产服务器,通过 80 和 443 路由流量。For a production server, you route your traffic through 80 and 443.

安装 JavaInstall Java

  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, run the following command:

        sudo apt-get install default-jdk
  3. 验证安装。Validate your installation. 仍在 SSH 会话中连接到 VM,运行以下命令:Still connected to your VM in your SSH session, run the following command:

        java -version

安装并配置 TomcatInstall and configure Tomcat

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

  2. 执行以下操作创建 Tomcat 用户:Create a Tomcat user by doing the following:

    a.a. 运行以下命令创建新的 Tomcat 组:Create a new Tomcat group by running the following command:

        sudo groupadd tomcat

    b.b. 创建新的 Tomcat 用户。Create a new Tomcat user. 将此用户添加到主目录为 /opt/tomcat 的 Tomcat 组。Add this user to the Tomcat group with a home directory of /opt/tomcat. 将 Tomcat 部署到此目录:You deploy Tomcat to this directory:

        sudo useradd -s /bin/false -g tomcat -d /opt/tomcat tomcat
  3. 执行以下操作安装 Tomcat:Install Tomcat by doing the following:

    a.a. Tomcat 8 下载页获取最新版 Tomcat 8 的 tar 的 URL。Get the URL for the tar for the latest version of Tomcat 8 from the Tomcat 8 download page.

    b.b. 使用 cURL 通过链接下载最新版本。Use cURL to download the latest version by using the link. 运行以下命令:Run the following commands:

        cd /tmp 
        curl -O <URL for the tar for the latest version of Tomcat 8>

    c.c. 将 Tomcat 安装到 /opt/tomcat 目录。Install Tomcat to the /opt/tomcat directory. 创建文件夹,然后打开存档:Create the folder, and then open the archive:

        sudo mkdir /opt/tomcat
        sudo tar xzvf apache-tomcat-8*tar.gz -C /opt/tomcat --strip-components=1
        sudo chown -R tomcat webapps/ work/ temp/ logs/
  4. 运行以下命令更新 Tomcat 的权限:Update the permissions for Tomcat by running the following commands:

        sudo chgrp -R tomcat /opt/tomcat
        sudo chmod -R g+r conf
        sudo chmod g+x conf
  5. 创建 systemd 服务文件,以便以服务的形式运行 Tomcat。Create a systemd service file, so that you can run Tomcat as a service.

    a.a. Tomcat 需要知道 Java 的安装位置。Tomcat needs to know where you installed Java. 此路径通常称为 JAVA_HOMEThis path is commonly referred to as JAVA_HOME. 运行以下命令找到该位置:Find the location by running:

        sudo update-java-alternatives -l

    这会生成如下所示的结果:This produces something like the following:

        java-1.8.0-openjdk-amd64       1081       /usr/lib/jvm/java-1.8.0-openjdk-amd64

    可以使用输出中的路径并添加 /jre,来构造 JAVA_HOME 变量值。You can construct the JAVA_HOME variable value by taking the path from the output and adding /jre. 例如,使用上述示例 /usr/lib/jvm/java-1.8.0-openjdk-amd64/jreFor example, using the preceding example, /usr/lib/jvm/java-1.8.0-openjdk-amd64/jre.

    b.b. 使用服务器中的值创建 systemd 服务文件:Use the value from your server to create the systemd service file:

        sudo nano /etc/systemd/system/tomcat.service

    c.c. 将以下内容粘贴到该服务文件中。Paste the following contents into your service file. 根据需要修改 JAVA_HOME 的值,使之与系统上的值匹配。Modify the value of JAVA_HOME, if necessary, to match the value you found on your system. 还可以修改 CATALINA_OPTS 中指定的内存分配设置:You might also want to modify the memory allocation settings that are specified in CATALINA_OPTS:

        Description=Apache Tomcat Web Application Container
        Environment='CATALINA_OPTS=-Xms512M -Xmx1024M -server -XX:+UseParallelGC'
        Environment='JAVA_OPTS=-Djava.awt.headless=true -Djava.security.egd=file:/dev/./urandom'

    d.d. 保存并关闭该文件。Save and close the file.

    e.e. 重新加载 systemd 守护程序,使其能够识别上述服务文件:Reload the systemd daemon so that it knows about your service file:

        sudo systemctl daemon-reload

    f.f. 启动 Tomcat 服务:Start the Tomcat service:

        sudo systemctl start tomcat

    g.g. 输入以下命令,验证该服务是否已启动且未出错:Verify that it started without errors by entering:

        sudo systemctl status tomcat
  6. 验证 Tomcat 服务器。Verify the Tomcat server. Tomcat 使用端口 8080 来接受传统的请求。Tomcat uses port 8080 to accept conventional requests. 运行以下命令,允许流量流向该端口:Allow traffic to that port by running the following command:

        sudo ufw allow 8080

    如果尚未为 Azure Stack Hub VM 添加入站端口规则,现在请添加这些规则。If you haven't added the Inbound port rules for your Azure Stack Hub VM, add them now. 有关详细信息,请参阅创建 VMFor more information, see Create a VM.

  7. 在 Azure Stack Hub 所在的同一网络中打开浏览器,然后打开服务器 yourmachine.local.cloudapp.azurestack.external:8080Open a browser in the same network as your Azure Stack Hub, and then open your server, yourmachine.local.cloudapp.azurestack.external:8080.

    显示 Apache Tomcat 页的屏幕截图。

    此时会加载服务器上的 Apache Tomcat 页。The Apache Tomcat page on your server loads. 接下来,将服务器配置为允许你访问服务器状态、管理器应用和主机管理器。Next, you configure the server to allow you to access the Server Status, Manager App, and Host Manager.

  8. 启用服务文件,以便在重新启动服务器时自动启动 Tomcat:Enable the service file so that Tomcat automatically starts when you reboot your server:

        sudo systemctl enable tomcat
  9. 若要允许自己访问 Web 管理界面,请配置 Tomcat 服务器。To allow yourself access to the web management interface, configure the Tomcat server.

    a.a. 编辑 tomcat-users.xml 文件,并定义一个角色和用户,以便能够登录。Edit the tomcat-users.xml file, and define a role and user so that you can sign in. 将用户定义为可访问 manager-guiadmin-guiDefine the user to access the manager-gui and admin-gui.

        sudo nano /opt/tomcat/conf/tomcat-users.xml

    b.b. <tomcat-users> 节中添加以下元素:Add the following elements to the <tomcat-users> section:

        <role rolename="tomcat"/>
        <user username="<username>" password="<password>" roles="tomcat,manager-gui,admin-gui"/>

    例如,最终的文件可能如下所示:For example, your final file might look something like:

        <tomcat-users xmlns="http://tomcat.apache.org/xml"
              xsi:schemaLocation="http://tomcat.apache.org/xml tomcat-users.xsd"
        <role rolename="tomcat"/>
        <user username="tomcatuser" password="changemepassword" roles="tomcat,manager-gui,admin-gui"/>

    c.c. 保存并关闭该文件。Save and close the file.

  10. Tomcat 会将“管理器”和“主机管理器”应用的访问权限限制为来自服务器的连接。** **Tomcat restricts access to the Manager and Host Manager apps to connections coming from the server. 由于你要在 Azure Stack Hub 中的 VM 上安装 Tomcat,因此需要解除此限制。Because you're installing Tomcat on a VM in Azure Stack Hub, you'll want to remove this restriction. 通过编辑相应的 context.xml 文件来更改对这些应用的 IP 地址限制。Change the IP address restrictions on these apps by editing the appropriate context.xml files.

    a.a. 在管理器应用中更新 context.xmlUpdate context.xml in the Manager app:

        sudo nano /opt/tomcat/webapps/manager/META-INF/context.xml

    b.b. 注释掉 IP 地址限制以允许从任何位置进行连接,或添加用于连接 Tomcat 的计算机的 IP 地址。Comment out the IP address restriction to allow connections from anywhere, or add the IP address of the machine you're using to connect to Tomcat.

    <Context antiResourceLocking="false" privileged="true" >
        <!--<Valve className="org.apache.catalina.valves.RemoteAddrValve"
                allow="127\.\d+\.\d+\.\d+|::1|0:0:0:0:0:0:0:1" />-->

    c.c. 保存并关闭该文件。Save and close the file.

    d.d. 使用类似的更新命令更新 context.xml 主机管理器应用:Update context.xml the Host Manager app with a similar update:

        sudo nano /opt/tomcat/webapps/host-manager/META-INF/context.xml

    e.e. 保存并关闭该文件。Save and close the file.

  11. 若要使用所做的更改更新服务器,请重启 Tomcat 服务:To update the server with the changes, restart the Tomcat service:

        sudo systemctl restart tomcat
  12. 在 Azure Stack Hub 所在的同一网络中打开浏览器,然后打开服务器 yourmachine.local.cloudapp.azurestack.external:8080Open a browser in the same network as your Azure Stack Hub, and then open your server: yourmachine.local.cloudapp.azurestack.external:8080.

    a.a. 若要查看 Tomcat 服务器的状态并验证你是否有访问权限,请选择“服务器状态”。****To review the status of the Tomcat server and verify that you have access, select Server Status.

    b.b. 使用 Tomcat 凭据登录。Sign in with your Tomcat credentials.

    Azure Stack Hub VM 上的 Apache Tomcat

创建应用Create an app

需要创建一个要部署到 Tomcat 的 WAR。You'll need to create a WAR to deploy to Tomcat. 如果你只是想要检查环境,可以在 Apache Tomcat 站点上找到一个示例 WAR。If you want only to check your environment, you can find an example WAR at the Apache Tomcat site.

有关在 Azure 中开发 Java 应用的指导,请参阅在 Azure 中生成和部署 Java 应用For guidance about developing Java apps in Azure, see Build and deploy Java apps on Azure.

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

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

  2. 若要使用应用包更新服务器,请停止 Tomcat 服务:To update the server with your app package, stop the Tomcat service:

        sudo systemctl stop tomcat
  3. 若要写入 webapps 文件夹,请将 FTP 用户添加到 Tomcat 组。To be able to write to the webapps folder, add your FTP user to the Tomcat group. 该 FTP 用户是在 Azure Stack Hub 中创建 VM 时定义的用户。Your FTP user is the user you define when you create your VM in Azure Stack Hub.

        sudo usermod -a -G tomcat <VM-user>
  4. 若要清除 webapps 文件夹,然后加载新的或已更新的 WAR,请使用 FileZilla 连接到 VM。To clear the webapps folder and then load your new or updated WAR, connect to your VM with FileZilla. 有关说明,请参阅使用 FileZilla 通过 SFTP 进行连接For instructions, see Connect with SFTP with FileZilla.

    a.a. 清除 TOMCAT_HOME/webappsClear TOMCAT_HOME/webapps.

    b.b. 将 WAR 添加到 TOMCAT_HOME/webapps(例如 /opt/tomcat/webapps/ )。Add your WAR to TOMCAT_HOME/webapps (for example, /opt/tomcat/webapps/).

  5. Tomcat 会自动扩展并部署应用程序。Tomcat automatically expands and deploys the application. 可以使用先前创建的 DNS 名称查看该应用程序。You can view it by using the DNS name you created earlier. 例如:For example:


后续步骤Next steps