Help Center/ Elastic Cloud Server/ Best Practices/ Setting Up an Environment/ Setting Up a Java Web Environment/ Setting Up Tomcat-based Java Web Environment (CentOS 7.4)
Updated on 2024-11-04 GMT+08:00

Setting Up Tomcat-based Java Web Environment (CentOS 7.4)

Application Scenarios

Tomcat is a widely used Java Web application server. This section describes how to set up Java Web environment on an ECS. To do so, you need to download the Java Web installation packages, upload the packages to the ECS, and set security rules for the ECS. After installing Java Web, you need to configure related software.

The ECS in this chapter uses CentOS 7.4 64bit as OS.

Architecture

Figure 1 Setting up Tomcat-based Java web environment

Resource and Cost Planning

Table 1 Resources and costs

Resource

Description

Cost

VPC

VPC CIDR block: 192.168.0.0/16

Free

Subnet

  • AZ: AZ1
  • CIDR block: 192.168.0.0/24

Free

Security group

Inbound rule 1:
  • Priority: Set it to 1.

  • Action: Select Allow.
  • Type: Select IPv4.
  • Protocol & Port: Set it to ICMP: All.
  • Source: Set it to 0.0.0.0/0.
Inbound rule 2:
  • Priority: Set it to 1.
  • Action: Select Allow.
  • Type: Select IPv4.
  • Protocol & Port: Set it to TCP: 8080.
  • Source: Set it to 0.0.0.0/0.

Free

ECS

  • Billing mode: Yearly/Monthly
  • AZ: AZ1
  • Flavor: c7.large.2
  • Image: CentOS 7.4 64bit
  • System disk: 40 GiB
  • EIP: Auto assign
  • EIP type: Dynamic BGP
  • Billed by: Traffic
  • Bandwidth: 5 Mbit/s

The following resources generate costs:

  • ECSs
  • EVS disks
  • EIPs

For billing details, see Billing Modes.

jdk

A Java development tool software.

You can download it from:

http://www.oracle.com/technetwork/java/javase/downloads

Free

tomcat

An open-source web application server.

You can download it from:

http://tomcat.apache.org/download-80.cgi

Free

PuTTY

A cross-platform remote access tool, which is used to access various nodes from a Windows OS during software installation.

You can download it from:

https://www.chiark.greenend.org.uk/~sgtatham/putty/latest.html

Free

WinSCP

A file transfer across platform, which is used for transferring files between Windows and Linux systems.

You can download it from:

http://winscp.net/

Free

Table 1 lists the official paths to download JDK and Tomcat installation packages. You can also obtain the packages from open-source image paths.

Process

Figure 2 Deployment Process

Procedure

Preparations

  • An ECS is created and has an EIP bound.
  • The image source has been updated to a Huawei Cloud image source. For details, see How Can I Use an Automated Tool to Configure a Huawei Cloud Image Source (x86_64 and Arm)? This facilitates obtaining and updating the system and software.
  • The jdk and tomcat directories have been created on the ECS using the following commands:

    cd /home/

    mkdir webDemo

    cd webDemo/

    mkdir jdk

    mkdir tomcat

  • The installation packages have been downloaded to the local PC and uploaded to the ECS through the file transfer tool. Alternatively, you can run the wget command to download the installation packages to the ECS. The details of both methods are described as follows:
    • Method 1: Upload the installation packages to the ECS using the file transfer tool.
      • Use WinSCP to upload the JDK software package to the jdk directory.
      • Use WinSCP to upload the Tomcat software package to the tomcat directory.
    • Method 2: Run the wget command to download the installation packages to the ECS.
      1. Run the following command to go to the jdk directory:

        cd /home/webDemo/jdk

      2. Run the following command to download the JDK installation package:

        wget JDK package download address

        Download the JDK installation package from the path listed in Table 1 or from other open-source image paths.

        For example, to check the available versions of the jdk17 software package (jdk-17_linux-x64_bin.tar.gz used as an example), run the following command:

        wget https://download.oracle.com/java/17/latest/jdk-17_linux-x64_bin.tar.gz

      3. Run the following command to go to the tomcat directory:

        cd /home/webDemo/tomcat

      4. Run the following command to download the Tomcat installation package:

        Download the Tomcat installation package from the path listed in Table 1 or from other open-source image paths.

        wget http://mirrors.tuna.tsinghua.edu.cn/apache/tomcat/tomcat-x/vx.x.xx/bin/apache-tomcat-x.x.xx.tar.gz

        Find the required version from the open-source image path. The Tomcat installation package of version 8.5.xx is used as an example. Run the following commands to download the package:

        wget https://mirrors.tuna.tsinghua.edu.cn/apache/tomcat/tomcat-8/v8.5.xx/bin/apache-tomcat-8.5.xx.tar.gz --no-check-certificate

Configuring Security Group Rules for the ECS

  1. Click the ECS name to switch to the ECS details page and click Security Groups.
  2. In the upper right corner of the security group rule list, click Modify Security Group Rule.
  3. On the displayed page showing security group details, click Add Rule.
  4. In the Add Inbound Rule dialog box, add a security group rule as prompted.

    To deploy the Java Web environment, you need to add two security group rules for the ECS.

    1. Set Protocol to ICMP.

      If ICMP traffic to an ECS is disabled by default, pinging the ECS EIP will time out. Add a rule to allow ICMP traffic to the ECS first.

      Figure 3 Adding a rule to allow ICMP traffic
    2. Set an appropriate port. Port 8080 is used as an example here.
      Figure 4 Adding port 8080

Installing JDK

  1. Run the following command to go to the jdk directory:

    cd /home/webDemo/jdk

  2. Run the following command to decompress the JDK installation package to the jdk directory:

    tar -xvf jdk-17_linux-x64_bin.tar.gz -C /home/webDemo/jdk/

  3. Run the following command to configure environment variables:

    vim /etc/profile

  4. Add the following content to the end of the file:
    #set java environment
    JAVA_HOME=/home/webDemo/jdk/jdk-17.0.x
    JRE_HOME=$JAVA_HOME
    PATH=$JAVA_HOME/bin:$PATH
    CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JRE_HOME/lib/tools.jar
    export JAVA_HOME JRE_HOME PATH CLASSPATH

    In the preceding command, jdk-17.0.x indicates the version of the JDK installation package that is obtained from the command output in 2.

    Example value: jdk-17.0.9

  5. Run the following command to save the settings and exit:

    :wq

  6. Run the following command to make the /etc/profile configurations take effect:

    source /etc/profile

  7. Run the following command to verify the installation.

    java -version

    If the following information is displayed, JDK is installed.

    [root@ecs-c525-web ~]# java -version
    java version "17.0.9" 2023-10-17 LTS
    Java(TM) SE Runtime Environment (build 17.0.9+11-LTS-201)
    Java HotSpot(TM) 64-Bit Server VM (build 17.0.9+11-LTS-201, mixed mode, sharing)

Installing Tomcat

  1. Run the following command to go to the tomcat directory:

    cd /home/webDemo/tomcat

  2. Run the following command to decompress the Tomcat installation package to the tomcat directory:

    tar -xvf apache-tomcat-x.x.xx.tar.gz -C /home/webDemo/tomcat/

    For example, to decompress the Tomcat installation package of version 8.5.xx, run the following commands:

    tar -xvf apache-tomcat-8.5.xx.tar.gz -C /home/webDemo/tomcat/

  3. Run the following commands to install Tomcat:

    cd /home/webDemo/tomcat/apache-tomcat-x.x.xx/

    cd bin/

    For example, to install the Tomcat installation package of version 8.5.xx, run the following commands:

    cd /home/webDemo/tomcat/apache-tomcat-8.5.xx/

    cd bin/

  4. Run the following command to edit the setclasspath.sh script:

    vi setclasspath.sh

    Add the following content to the end of the setclasspath.sh script:

    Use the java version in Resource and Cost Planning to replace the JDK version in the following script:
    export JAVA_HOME=/home/webDemo/jdk/jdk-17.0.9
    export JRE_HOME=$JAVA_HOME
  5. Run the following command to save the settings and exit:

    :wq

  6. Run the following command to start Tomcat:

    ./startup.sh

  7. Run the following command to check the Tomcat process:

    ps -ef | grep tomcat

    If the following information is displayed, Tomcat is started successfully.

    Figure 5 Checking the Tomcat process

Verifying Java Web Deployment

Enter the following URL in the address bar of the browser:

http://EIP bound to the ECS:8080

If the Tomcat page is displayed, Java Web has been set up. Port 8080 can be accessed over the public network.

Figure 6 Accessing port 8080