This topic describes how to manually deploy a Java web environment on an Elastic Compute Service (ECS) instance that runs Alibaba Cloud Linux 2. This topic is applicable to individual users who are new to website construction on ECS instances.

Background information

In this topic, the following instance type and software versions are used. Operation may vary based on your instance type and software versions.
  • Instance type: ecs.c6.large
  • Operating system: Alibaba Cloud Linux 2.1903 LTS 64-bit
  • Java Development Kit (JDK): JDK 19
  • Apache Tomcat: Apache Tomcat 8.5.85
    Note In this example, Apache Tomcat 8.5.85 is used. The source code is upgraded on a regular basis. You can manually obtain a version appropriate to your requirements.

Step 1: Make preparations

  1. Add inbound rules to the security group of the instance to allow traffic on the required ports.
    For more information, see Add a security group rule. In this example, inbound rules are added to allow traffic on SSH port 22, HTTP port 80, HTTPS port 443, and Apache Tomcat port 8080.
  2. Connect to the instance.
  3. Disable the firewall.
    1. Run the systemctl status firewalld command to check the status of the firewall.
      Check the status of the firewall
      • If the firewall is in the inactive state, the firewall is disabled.
      • If the firewall is in the active state, the firewall is enabled. In this example, the firewall is in the active state and must be disabled.
    2. Disable the firewall. Skip this step if the firewall is disabled.
      • To temporarily disable the firewall, run the following command:
        systemctl stop firewalld
        Note After you run the preceding command, the firewall is disabled. When you restart the Linux operating system, the firewall is enabled and re-enters the active state.
      • To permanently disable the firewall, perform the following steps:
        1. Run the following command to stop the firewall:
          systemctl stop firewalld
        2. Run the following command to disable the firewall and prevent the firewall from starting on instance startup:
          systemctl disable firewalld
        Note After you run the preceding commands, the firewall is disabled. When you restart the instance, the firewall remains disabled. You can re-enable the firewall. For more information, see Firewalld documentation.
  4. Disable Security-Enhanced Linux (SELinux).
    1. Check the status of SELinux.
      getenforce
      The following figure shows an example command output:Check the status of SELinux
      • If SELinux is in the Disabled state, SELinux is disabled.
      • If SELinux is in the Enforcing state, SELinux is enabled. In this example, SELinux is in the Enforcing state and must be disabled.
    2. Disable SELinux. Skip this step if SELinux is disabled.
      • To temporarily disable SELinux, run the following command:
        setenforce 0
        Note After you run the preceding command, SELinux is disabled. When you restart the instance, SELinux is enabled and re-enters the Enforcing state.
      • To permanently disable SELinux, run the following command to open the SELinux configuration file:
        vi /etc/selinux/config
        In the /etc/selinux/config file, move the pointer on the SELINUX=enforcing line and press the I key to enter the edit mode. Set SELINUX to disabled and press the Esc key to exit the edit mode. Enter :wq and press the Enter key to save and close the file.
        Note You can re-enable SELinux after it is disabled. For more information, see Enable or disable SELinux.
    3. Restart the instance to apply the settings.

Step 2: Install JDK

  1. Create a directory in which to install JDK.
    mkdir /usr/jdk
  2. Download the JDK installation package.
    In this example, the JDK 19 installation package is downloaded. You can visit the official Oracle website to view information about more JDK versions.
    cd /usr/jdk
    wget https://download.oracle.com/java/19/latest/jdk-19_linux-x64_bin.tar.gz
  3. Decompress the JDK installation package to the specified folder.
    tar xzf jdk-19_linux-x64_bin.tar.gz -C /usr/jdk/
  4. Configure environment variables.
    1. Open the configuration file.
      vim /etc/profile
    2. At the end of the configuration file, press the I key to enter the edit mode.
    3. Add the following information.
      Note The JAVA_HOME value is the path where JDK is installed. In this example, the find /usr/jdk -name 'jdk-19.*' command is run to view the path where JDK is installed, and the command output indicates that JDK is installed in the /usr/jdk/jdk-19.0.2 path.
      #set java environment
      export JAVA_HOME=/usr/jdk/jdk-19.0.2
      export CLASSPATH=$JAVA_HOME/lib/tools.jar:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib
      export PATH=$JAVA_HOME/bin:$PATH
    4. Press the Esc key to exit the edit mode. Enter :wq and press the Enter key to save and close the configuration file.
    5. Apply the environment variables.
      source /etc/profile
  5. Check the JDK version.
    java -version
    JDK version information in the sample command output:
    [root@iZuspq7vnxm**** ~]# java -version
    java version "19.0.2" 2023-01-17
    Java(TM) SE Runtime Environment (build 19.0.2+7-44)
    Java HotSpot(TM) 64-Bit Server VM (build 19.0.2+7-44, mixed mode, sharing)

Step 3: Install Apache Tomcat

  1. Download the Apache Tomcat 8 installation package.
    wget --no-check-certificate https://dlcdn.apache.org/tomcat/tomcat-8/v8.5.85/bin/apache-tomcat-8.5.85.tar.gz
    Note The download address of Apache Tomcat may change. If the preceding download address is invalid, visit the official Apache Tomcat website to obtain the latest download address.
  2. Decompress the Apache Tomcat 8 installation package.
    tar -zxvf apache-tomcat-8.5.85.tar.gz
  3. Move the Apache Tomcat installation files to the /usr/local/tomcat/ directory.
    mv apache-tomcat-8.5.85 /usr/local/tomcat/
  4. Access the /usr/local/tomcat/bin directory.
    cd /usr/local/tomcat/bin
  5. Edit the setclasspath.sh script.
    vi setclasspath.sh
  6. Press the I key to enter the edit mode, and add the following content to the end of the setclasspath.sh script.
    export JAVA_HOME=/usr/jdk/jdk-19.0.2
    export JRE_HOME=$JAVA_HOME
    Note Set jdk-<Version number> based on the actual JDK version number.
  7. Press the Esc key to exit the edit mode. Enter :wq and press the Enter key to save and close the script.
  8. Start Apache Tomcat.
    ./startup.sh
  9. Check the Tomcat process.
    ps -ef | grep tomcat
    If a command output similar to the following one is returned, Apache Tomcat is started:
    root      2225     1 40 14:46 pts/0    00:00:04 /usr/jdk/jdk-19.0.2/bin/java 
    -Djava.util.logging.config.file=/usr/local/tomcat/conf/logging.properties -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager 
    -Djdk.tls.ephemeralDHKeySize=2048 -Djava.protocol.handler.pkgs=org.apache.catalina.webresources 
    -Dorg.apache.catalina.security.SecurityListener.UMASK=0027 -Dignore.endorsed.dirs= -classpath 
    /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar -Dcatalina.base=/usr/local/tomcat -Dcatalina.home=/usr/local/tomcat 
    -Djava.io.tmpdir=/usr/local/tomcat/temp org.apache.catalina.startup.Bootstrap start
    root      2285  6446  0 14:46 pts/0    00:00:00 grep --color=auto tomcat

Step 4: Deploy and verify the test project

  • If the deployed Java web project files are a WAR package, you must upload the WAR package to the /usr/local/tomcat/webapps/ directory. You can use a connection tool that has a file transfer feature or build an FTP site to upload the project files. After the project files are uploaded, you can enter http://<Public IP address of the instance>:8080/<Project name> in the address bar of a browser to access the files.
  • If only web pages need to be uploaded, you can upload the web pages to the /usr/local/tomcat/webapps/ROOT/ directory. After the web pages are uploaded, you can enter http://<Public IP address of the instance>:8080 in the address bar of a browser to access the pages.

In example, a Tomcat test page is created in the /usr/local/tomcat/webapps/ROOT/ directory. You can enter http://<Public IP address of the instance>:8080 in the address bar of a browser on your computer to access the page.

  1. Create a test file.
    echo Tomcat test > /usr/local/tomcat/webapps/ROOT/index.jsp
  2. Open your browser and enter http://<Public IP address of the ECS instance>:8080 in the address bar to connect to the ECS instance.
    The page shown in the following figure indicates that Apache Tomcat is installed. If http://<Public IP address of the ECS instance>:8080 is inaccessible, check whether rules are added to the security group of the instance to allow access on Apache Tomcat port 8080. For information about how to add security group rules, see Add a security group rule. Response