Help Center> Elastic Cloud Server> Best Practices> Setting Up Tomcat-based Java Web Environment (CentOS 7.4)
Updated on 2023-11-13 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:
  • Protocol/Application: ICMP
  • Port: All
  • Source: 0.0.0.0/0
Inbound rule 2:
  • Protocol/Application: TCP
  • Port: 8080
  • Source: 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 GB
  • EIP: Auto assign
  • EIP type: Dynamic BGP
  • Billed by: Traffic
  • Bandwidth: 5 Mbit/s

The following resources incur costs:

  • ECSs
  • EVS disks
  • EIPs

For billing details, see Billing.

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 obtain 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 obtain 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.
  • To obtain and update the system and software, update the image source 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)?
  • 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 switch to the jdk directory:

        cd /home/webDemo/jdk

      2. Running 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.

        Check the available jdk17 software package versions. The JDK installation package jdk-17_linux-x64_bin.tar.gz is used as an example. Run the following command to download the package:

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

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

        cd /home/webDemo/tomcat

      4. Running 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 v8.5.95 is used as an example. Run the following command to download the package:

        wget https://mirrors.tuna.tsinghua.edu.cn/apache/tomcat/tomcat-8/v8.5.95/bin/apache-tomcat-8.5.95.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 set up Java Web environment, 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. Therefore, add a rule to allow ICMP traffic to the ECS.

      Figure 3 Adding the security group rule
    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 configuration 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

    JDK is successfully installed if the following information is displayed:

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

    tar -xvf apache-tomcat-8.5.95.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.95, run the following commands:

    cd /home/webDemo/tomcat/apache-tomcat-8.5.95/

    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 content in the address box of a browser:

http://EIP bound to the ECS:8080

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

Figure 6 Accessing port 8080