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

The following instance type and software versions are used in this topic. The operations 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
  • JDK: 1.8.0_292
  • Apache Tomcat: 8.5.69
    Note In this example, Tomcat 8.5.69 is used. The source code is upgraded on a regular basis, and you can manually obtain a version appropriate to your requirements.

Step 1: Prepare for building a Java web environment

  1. Add inbound rules to the security group of the ECS instance to allow traffic on the required ports.
    For more information, see Add security group rules. In this example, traffic on SSH port 22, HTTP port 80, HTTPS port 443, and Tomcat port 8080 is allowed.
  2. Connect to the instance.
  3. Disable the firewall.
    1. Run the systemctl status firewalld command to check the state of the firewall.
      Check the state of the firewall
      • If the firewall is disabled, it is in the inactive state.
      • If the firewall is enabled, it is in the active state. 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 already disabled.
      • To temporarily disable the firewall, run the following command:
        systemctl stop firewalld
        Note After you run the preceding command, the firewall is disabled. The next time you restart the Linux operating system, the firewall is re-enabled and enters the active state.
      • To permanently disable the firewall, run the following commands in turn.
        1. To disable the running firewall, run the following command:
          systemctl stop firewalld
        2. To stop the firewall service and configure the service not to automatically start on instance startup, run the following command:
          systemctl disable firewalld
        Note After you run the preceding commands, the firewall is disabled. The next time you restart the instance, the firewall remains disabled by default. You can re-enable the firewall. For more information, see Firewalld documentation.
  4. Disable SELinux.
    1. Check the state of SELinux.
      getenforce
      Example command output:Check the state of SELinux
      • If SELinux is disabled, it is in the Disabled state.
      • If SELinux is enabled, it is in the Enforcing state. In this example, SELinux is in the Enforcing state and must be disabled.
    2. Disable SELinux. Skip this step if SELinux is already disabled.
      • To temporarily disable SELinux, run the following command:
        setenforce 0
        Note After you run this command, SELinux is disabled. The next time you restart the Linux operating system, SELinux is re-enabled and 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. Press the Esc key, enter :wq, and then press the Enter key to save and close the SELinux configuration file.

        Note You can re-enable SELinux. For more information, see Enable or disable SELinux.
    3. Restart the system for the changes to take effect.
  5. Create a user named www to run Tomcat.
    useradd www
  6. Create a website root directory:
    mkdir -p /data/wwwroot/default
  7. Set the owner of the website root directory to www.
    chown -R www.www /data/wwwroot

Step 2: Install JDK 1.8

  1. Search for the Java Development Kit (JDK) 1.8 package.
    yum -y list java*
  2. Install the JDK 1.8 package displayed in the software list.
    yum -y install java-1.8.0-openjdk-devel.x86_64
  3. Check the JDK version.
    java -version
    JDK version information in the sample command output:
    openjdk version "1.8.0_292"
    OpenJDK Runtime Environment (build 1.8.0_292-b10)
    OpenJDK 64-Bit Server VM (build 25.292-b10, mixed mode)
  4. Set 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 content.
      Note The JAVA_HOME value is the path where JDK is installed. In this example, run the cd /usr/lib/jvm/ command to go to the jvm path and then run the ls command to check the path where JDK is installed.
      JAVA_HOME=/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.292.b10-1.1.al7.x86_64
      PATH=$PATH:$JAVA_HOME/bin
      CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
      export JAVA_HOME CLASSPATH PATH
    4. Press the Esc key to exit the edit mode. Enter :wq and press the Enter key to save and close the file.
    5. Immediately apply the environment variables.
      source /etc/profile

Step 3: Install Apache Tomcat

  1. Download the Tomcat 8 installation package.
    wget https://apache.claz.org/tomcat/tomcat-8/v8.5.69/bin/apache-tomcat-8.5.69.tar.gz
    Note The download address of Tomcat may change. For information about the download address of the new version, visit the official Tomcat website.
  2. Decompress the Tomcat 8 installation package.
    tar -zxvf apache-tomcat-8.5.69.tar.gz
  3. Move the Tomcat files to the /user/local/tomcat directory.
    mv apache-tomcat-8.5.69 /usr/local/tomcat/
  4. Set the owner of the files to www.
    chown -R www.www /usr/local/tomcat/

    The /usr/local/tomcat/ directory contains the following subdirectories:

    • bin: stores Tomcat script files, such as those used to enable and disable Tomcat.
    • conf: stores various global configuration files of the Tomcat server, among which server.xml and web.xml are the most important files.
    • webapps: serves as the main web publishing directory of Tomcat to store web application files by default.
    • logs: stores Tomcat operation log files.
  5. Configure the server.xml file.
    1. Switch to the /usr/local/tomcat/conf/ directory:
      cd /usr/local/tomcat/conf/
    2. Rename the server.xml file:
      mv server.xml server.xml_bk
    3. Create a server.xml file.
      1. Run the following command to create and open the server.xml file:
        vi server.xml
      2. Press the I key to add the following content:
        <?xml version="1.0" encoding="UTF-8"?>
        <Server port="8006" shutdown="SHUTDOWN">
        <Listener className="org.apache.catalina.core.JreMemoryLeakPreventionListener"/>
        <Listener className="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener"/>
        <Listener className="org.apache.catalina.core.ThreadLocalLeakPreventionListener"/>
        <Listener className="org.apache.catalina.core.AprLifecycleListener"/>
        <GlobalNamingResources>
        <Resource name="UserDatabase" auth="Container"
         type="org.apache.catalina.UserDatabase"
         description="User database that can be updated and saved"
         factory="org.apache.catalina.users.MemoryUserDatabaseFactory"
         pathname="conf/tomcat-users.xml"/>
        </GlobalNamingResources>
        <Service name="Catalina">
        <Connector port="8080"
         protocol="HTTP/1.1"
         connectionTimeout="20000"
         redirectPort="8443"
         maxThreads="1000"
         minSpareThreads="20"
         acceptCount="1000"
         maxHttpHeaderSize="65536"
         debug="0"
         disableUploadTimeout="true"
         useBodyEncodingForURI="true"
         enableLookups="false"
         URIEncoding="UTF-8"/>
        <Engine name="Catalina" defaultHost="localhost">
        <Realm className="org.apache.catalina.realm.LockOutRealm">
        <Realm className="org.apache.catalina.realm.UserDatabaseRealm"
          resourceName="UserDatabase"/>
        </Realm>
        <Host name="localhost" appBase="/data/wwwroot/default" unpackWARs="true" autoDeploy="true">
        <Context path="" docBase="/data/wwwroot/default" debug="0" reloadable="false" crossContext="true"/>
        <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
        prefix="localhost_access_log." suffix=".txt" pattern="%h %l %u %t &quot;%r&quot; %s %b" />
        </Host>
        </Engine>
        </Service>
        </Server>
      3. Press the Esc key to exit the edit mode. Enter :wq and press the Enter key to save and close the configuration file.
  6. Configure the Java Virtual Machine (JVM) memory parameters.
    1. Run the following command to create and open the /usr/local/tomcat/bin/setenv.sh file:
      vi /usr/local/tomcat/bin/setenv.sh
    2. Press the I key to add the following content:
      Specify the JAVA_OPTS parameter to set the JVM memory information and encoding format.
      JAVA_OPTS='-Djava.security.egd=file:/dev/./urandom -server -Xms256m -Xmx496m -Dfile.encoding=UTF-8'
    3. Press the Esc key to exit the edit mode. Enter :wq and press the Enter key to save and close the configuration file.
  7. Configure a script to run Tomcat on system startup.
    1. Download the script.
      Note The script originates from the community and is for reference only. Alibaba Cloud does not make any guarantee, express or implied, with respect to the reliability of the script, as well as potential impacts of operations on the script. If you cannot download the script by running the wget command, you can use a browser to access https://raw.githubusercontent.com/oneinstack/oneinstack/master/init.d/Tomcat-init to obtain the script content.
      wget https://raw.githubusercontent.com/oneinstack/oneinstack/master/init.d/Tomcat-init
    2. Move and rename Tomcat-init.
      mv Tomcat-init /etc/init.d/tomcat
    3. Grant the execute permissions on the /etc/init.d/tomcat file.
      chmod +x /etc/init.d/tomcat
    4. Configure the JAVA_HOME script to run Tomcat on system startup.
      Notice The JDK version in the script must be the same as the one that was installed. Otherwise, Tomcat cannot start.
      sed -i 's@^export JAVA_HOME=.*@export JAVA_HOME=/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.292.b10-1.1.al7.x86_64@' /etc/init.d/tomcat
  8. Run the following commands in sequence to enable Apache Tomcat to run on system startup:
    1. chkconfig --add tomcat
    2. chkconfig tomcat on
  9. Run the following command to start Apache Tomcat:
    service tomcat start             

Step 4: Deploy and verify the test project

Upload the WAR package of Java web project files to the website root directory and change the owner of files in the root directory to www. You can use a remote connection tool that has a file transfer feature or build an FTP site to upload project files. In this example, the website root directory is /data/wwwroot/default. Perform the following operations to create a Tomcat test page in the website root directory and access the page.

  1. Deploy the test file.
    echo Tomcat test > /data/wwwroot/default/index.jsp
  2. Open your browser and enter a URL in the http://<Public IP address of the ECS instance>:8080 format in the address bar to connect to the instance.
    The following page indicates that Apache Tomcat is installed. Response