All Products
Search
Document Center

Elastic Compute Service:Manually deploy a Java web environment (Tomcat)

Last Updated:Jan 02, 2025

Java Web is a technology stack that integrates the Java programming language with various technologies and frameworks to develop dynamic web applications. It utilizes the robust features of the Java platform, allowing developers to build complex, high-performance web applications for cross-platform deployment. Apache Tomcat is an open-source web server frequently used in Java Web development for deploying and running Java web applications. This topic describes the steps to deploy a Java web environment on an Elastic Compute Service (ECS) instance.

Quick deployment

You can click Run with one click to access Terraform Explorer, where you can view and execute Terraform code to automatically deploy a Java web environment (Tomcat) on an ECS instance.

Preparations

Before deploying a Java web environment, ensure the ECS instance satisfies the following criteria:

Procedure

Follow these steps based on your instance's operating system.

Linux

This example uses Apache Tomcat 9.0.91. If you install a different version or use other directories, replace the version and directories in the commands accordingly.

  1. Log on to a Linux server. For additional details, refer to Overview of ECS Remote Connection Methods.

  2. To download and extract the Apache Tomcat 9.0.91 installation package, execute the following commands.

    Note
    • The download URLs for Apache Tomcat may change. If the link provided below is not working, please visit the official Apache Tomcat website to retrieve the latest version.

    • Should you require a different version, the official Apache Tomcat website provides download URLs which you can use to update the URL in the command accordingly.

    wget https://archive.apache.org/dist/tomcat/tomcat-9/v9.0.91/bin/apache-tomcat-9.0.91.tar.gz  --no-check-certificate 
    tar -zxvf apache-tomcat-9.0.91.tar.gz
  3. To relocate the Apache Tomcat installation files, execute the command below to move them to the /usr/local/tomcat/ directory.

    sudo mv apache-tomcat-9.0.91 /usr/local/tomcat/
  4. (Optional) Configure the server.xml file.

    To modify Apache Tomcat configurations, follow these steps:

    1. Open the /usr/local/tomcat/conf/server.xml file by running the following command:

      vim /usr/local/tomcat/conf/server.xml
    2. Press the i key to enter edit mode. By default, Apache Tomcat uses standard configurations. However, you can modify these settings to meet your specific business needs. For instance:

      • The default port for Apache Tomcat is 8080. To change the port number, modify the port property value.

        Note

        Remember to allow inbound traffic on the new port by adding a security group rule to your instance. For more information, see add security group rule.

        image

      • The default website root directory for Apache Tomcat is webapps. By placing a web application, such as a WAR file or a directory containing the WEB-INF folder, into the webapps directory, Apache Tomcat will automatically deploy the application. To change the website root directory in Apache Tomcat, you can alter the value of the appBase property. image

    3. Once modifications are complete, press the Esc key, type :wq, and press Enter to save the changes and exit the file.

  5. (Optional) Configure JVM memory parameters.

    To optimize the performance and stability of Apache Tomcat based on your business needs, you can configure the JVM memory parameters by following these steps.

    1. Execute the command below to create and open the /usr/local/tomcat/bin/setenv.sh file.

      vim /usr/local/tomcat/bin/setenv.sh
    2. Press the i key to enter edit mode and insert the following content.

      Set the JAVA_OPTS parameter to adjust JVM memory settings and specify the encoding format, which in this example is UTF-8.

      Note

      You can modify the content below to better suit your business requirements for JVM memory configuration.

      JAVA_OPTS='-Djava.security.egd=file:/dev/./urandom -server -Xms512m -Xmx512m -Dfile.encoding=UTF-8'
    3. Press the Esc key, type :wq, and hit the Enter key to save the changes and exit the file.

    4. Assign execute permissions to the file.

      sudo chmod +x /usr/local/tomcat/bin/setenv.sh
  6. Set up a script to automatically start Apache Tomcat during system boot.

    1. Download the script by executing the command below.

      wget https://help-static-aliyun-doc.aliyuncs.com/software/Tomcat-init.sh
    2. Use the following command to relocate and rename Tomcat-init.sh.

      sudo mv Tomcat-init.sh /etc/init.d/tomcat
    3. Assign execute permissions to /etc/init.d/tomcat by running the command below.

      chmod +x /etc/init.d/tomcat
    4. Determine the installation path of the Java Development Kit (JDK). Execute the command below to find the directory where the Java binary is located; this directory's parent is the JDK installation path.

      readlink -f $(which java)
      Note

      For instance, if the command output is /usr/lib/jvm/java-1.8.0-openjdk-1.8.0.432.b06-2.0.2.1.al8.x86_64/jre/bin/java, then the JDK installation path is /usr/lib/jvm/java-1.8.0-openjdk-1.8.0.432.b06-2.0.2.1.al8.x86_64.

    5. Configure the startup script's JAVA_HOME by executing the command below.

      Important

      Ensure that the JDK path in the script matches the actual JDK installation path to enable Apache Tomcat to start properly.

      sudo sed -i 's@^export JAVA_HOME=.*@export JAVA_HOME=/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.432.b06-2.0.2.1.al8.x86_64@' /etc/init.d/tomcat
  7. To ensure Apache Tomcat starts automatically upon system boot and to initiate Apache Tomcat, execute the following commands in sequence:

    sudo systemctl daemon-reload
    sudo systemctl start tomcat
    sudo systemctl enable tomcat

    Once started, you can check the current status of Apache Tomcat by running sudo systemctl status tomcat. If the output shows active (running), Apache Tomcat has been successfully launched.image

  8. Verify the environment configuration.

    Enter http://public IP:8080 in your local browser's address bar. If the Tomcat welcome page appears, the environment configuration is successful.

    Note
    • Should the page fail to display after entering http://public IP:8080, verify that the security group settings allow inbound traffic on port 8080.

    • If you have modified the Apache Tomcat port number, use the new port number instead of 8080 and ensure the security group permits inbound traffic on that port.

    • To test a local project, upload the WAR package to Apache Tomcat's website root directory (default is /usr/local/tomcat/webapps) and navigate to http://public IP:8080/package name in your browser. For more information, see how to upload or download files (Linux).

    image

Windows

This example uses Apache Tomcat 9.0.97.

  1. Log on to a Windows server. For more information, see the referenced document.

  2. Download Apache Tomcat.

    Visit the official Apache Tomcat website, select the appropriate version, and download the installation package.

    image

  3. Extract the Apache Tomcat installation package.

    Unzip the downloaded package to your desired installation directory, for example, C:\Program Files.

  4. Locate the Apache Tomcat installation directory.

    Navigate to the directory where you unzipped the package and find the installation directory of Apache Tomcat in the address bar.

    image

  5. Set up environment variables.

    1. Right-click This PC and select Properties.image

    2. Scroll to the bottom of the About page and click Advanced System Settings.

      image

    3. Click Environment Variables.

      image

    4. In the System Variables section, you can click New to create a system variable. Once created, click OK. Name the variable CATALINA_HOME and set its value to the Apache Tomcat installation directory, such as C:\Program Files\apache-tomcat-9.0.97.

      image

    5. Locate the Path variable under System Variables, select it, and click Edit.

      image

    6. In the Edit Environment Variables window, click New and add %CATALINA_HOME%\bin to the list.

      image

    7. Click OK twice to save the changes to the environment variables.

      image

  6. Launch Apache Tomcat.

    1. Navigate to the bin directory within the Apache Tomcat installation folder.image

    2. Type cmd in the address bar and press Enter to open a command prompt window.image

    3. Run the startup.bat command to start Apache Tomcat. A new command prompt window will open displaying the startup log. Keep this window open.

      image

      image

  7. Verify Apache Tomcat installation.

    Open a local browser and enter http://Public IP:8080 to access the Apache Tomcat welcome page. If the page displays, the installation is successful.

    Note
    • Should the page fail to load upon entering http://Public IP:8080, consider the following troubleshooting steps:

      • Ensure the instance's security group allows inbound traffic on port 8080.

      • Ensure that the command prompt window remains open after executing the startup.bat command to start Apache Tomcat. If the window has closed, re-execute the startup.bat command to reopen it.

    • To test a local project, upload the WAR file to the Apache Tomcat webapps directory (e.g., C:\Program Files\apache-tomcat-9.0.97\webapps) and navigate to http://Public IP:8080/Package Name in your browser. For more information, see the referenced document.

    image