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

Prerequisites

  • An Alibaba Cloud account is created. To create an Alibaba Cloud account, go to the Sign up to Alibaba Cloud page.
  • To use ECS instances that are located in Chinese mainland regions, make sure that you have completed real-name verification for your account.
  • An ECS instance is created. For more information, see Create an instance by using the wizard.

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: CentOS 7.4
  • Apache Tomcat: Apache Tomcat 8.5.82
    Note In this example, Apache Tomcat 8.5.82 is used. The source code is upgraded on a regular basis. You can manually obtain a version appropriate to your requirements.
  • JDK: 1.8.0_342
  • FTP tool: WinSCP

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 and HTTP port 8080.
  2. Connect to the Linux instance. For more information, see Connect to a Linux instance by using a password.
  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.

Step 3: Install JDK

  1. Run the following command to view the JDK version in the YUM repository:
    yum list java*
  2. Run the following command to install JDK1.8.0:
    yum -y install java-1.8.0-openjdk*
  3. Run the following command to check whether the JDK is installed:
    java -version
    If a command output similar to the following one is returned, the JDK is installed.
    [root@iZuspq7vnxm**** ~]# java -version
    openjdk version "1.8.0_342"
    OpenJDK Runtime Environment (build 1.8.0_342-b07)
    OpenJDK 64-Bit Server VM (build 25.342-b07, mixed mode)
  4. Configure environment variables.
    1. Run the following command to view the path where the JDK is installed.
      find /usr/lib/jvm -name 'java-1.8.0-openjdk-1.8.0*'
      A command output similar to the following one is returned:
      [root@test000****~]# find /usr/lib/jvm -name 'java-1.8.0-openjdk-1.8.0*'
      /usr/lib/jvm/java-1.8.0-openjdk-1.8.0.342.b07-1.el7_9.x86_64
    2. Run the following command to open the configuration file:
      vim /etc/profile
    3. At the end of the configuration file, press the I key to enter the edit mode.
    4. Add the following information.
      Note The JAVA_HOME value is the path where JDK is installed.
      JAVA_HOME=/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.342.b07-1.el7_9.x86_64
      PATH=$PATH:$JAVA_HOME/bin
      CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
      export JAVA_HOME CLASSPATH PATH
    5. Press the Esc key to exit the edit mode. Enter :wq and press the Enter key to save and close the configuration file.
    6. Run the following command for the environment variables to take effect.
      source /etc/profile
      Important After you modify the environment variables, issues such as -bash: chmod: command not found may occur when you run other commands. You can run the export PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin command to solve the issue.

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.82/bin/apache-tomcat-8.5.82.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.82.tar.gz
  3. Move the Apache Tomcat installation files to the /usr/local/tomcat/ directory.
    mv apache-tomcat-8.5.82 /usr/local/tomcat/
  4. Set the owner of the file to www.
    chown -R www.www /usr/local/tomcat/

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

    • bin: stores Apache Tomcat script files, such as scripts used to enable and disable Apache Tomcat.
    • conf: stores various global configuration files of the Apache Tomcat server, among which server.xml and web.xml are the most important files.
    • webapps: serves as the main web publishing directory of Apache Tomcat. Web application files are stored in this directory by default.
    • logs: stores Apache Tomcat operation log files.
  5. Configure the server.xml file.
    1. Go 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 enter the edit mode and 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 enable Apache Tomcat to run on system startup.
    1. Run the following command to download the script.
      Note This script is maintained by the community and is provided for reference only. Alibaba Cloud does not make any guarantee, express or implied, with respect to the performance and reliability of the script or the potential impacts of the script in actual use. If you cannot download the script by running the wget command, you can access https://raw.githubusercontent.com/oneinstack/oneinstack/master/init.d/Tomcat-init in a browser to obtain the contents of the script.
      wget https://raw.githubusercontent.com/oneinstack/oneinstack/master/init.d/Tomcat-init
    2. Run the following command to move and rename Tomcat-init:
      mv Tomcat-init /etc/init.d/tomcat
    3. Run the following command to grant the execute permissions on the /etc/init.d/tomcat file:
      chmod +x /etc/init.d/tomcat
    4. Set JAVA_HOME in the script.
      Important The JDK path specified in the script must be the JDK installation path.
      sed -i 's@^export JAVA_HOME=.*@export JAVA_HOME=/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.342.b07-1.el7_9.x86_64@' /etc/init.d/tomcat                  
  8. Run the following commands in sequence to enable Apache Tomcat to run upon system startup:
    chkconfig --add tomcat
    chkconfig tomcat on
  9. Run the following command to start Apache Tomcat:
    service tomcat start             
    If a command output similar to the following one is returned, Apache Tomcat is started.
    [root@test000**** conf]# service tomcat start
    Starting tomcat
    Using CATALINA_BASE:   /usr/local/tomcat
    Using CATALINA_HOME:   /usr/local/tomcat
    Using CATALINA_TMPDIR: /usr/local/tomcat/temp
    Using JRE_HOME:        /usr/lib/jvm/java-1.8.0-openjdk-1.8.0.342.b07-1.el7_9.x86_64
    Using CLASSPATH:       /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar
    Using CATALINA_OPTS:
    Tomcat started.
    Tomcat is running with pid: 11837            

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 an Apache Tomcat test page in the website root directory and access the page.

  1. To ensure system security, we recommend that you create a standard user to run Apache Tomcat.
    In this example, a standard user named www is created.
    useradd www
  2. Run the following command to create a website root directory.
    mkdir -p /data/wwwroot/default
  3. Run the following command to set the owner of the website root directory to www.
    chown -R www.www /data/wwwroot
  4. Run the following command to create the test file:
    echo Tomcat test > /data/wwwroot/default/index.jsp
  5. 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

What to do next

When Apache Tomcat becomes available, we recommend that you configure websites on the instance and map the domain name of the websites to the public IP address of the instance.