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.53
    Note In this topic, Apache Tomcat 8.5.53 is used. The source code is constantly upgraded, and you can obtain a version appropriate to your requirements.
  • JDK: JDK 1.8.0_241
  • FTP tool: WinSCP

Step 1: Download the source code

  1. Download Apache Tomcat. For more information, visit Index of /apache/tomcat/tomcat-8/.
  2. Download Java Development Kit (JDK).
    1. Download a JDK installation package. For more information, visit Java Downloads.
      Note If you run the wget command on an instance to download the JDK installation package and an error is reported when you decompress the package, you can download the JDK installation package to your local computer and upload it to the instance.
    2. Log on to the ECS console.
    3. In the left-side navigation pane, choose Instances & Images > Instances.
    4. Select the region where the instance is deployed.
    5. On the Instances page, find the instance and view its public IP address in the IP Address column.
    6. In WinSCP, connect to the instance by using the public IP address.
    7. Upload the downloaded Apache Tomcat and JDK installation package to the root directory of the instance.

Step 2: Prepare for installation

  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 examples, inbound rules are added to the security group to allow traffic on SSH port 22 and HTTP port 8080.
  2. Connect to the instance. For more information, see Connect to a Linux instance by using password authentication.
  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. 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
  6. Run the following command to create a root directory for the Java website:
    mkdir -p /data/wwwroot/default
  7. Upload the WAR package of Java web project files to the root directory and change the owner of files under the root directory to www.
    In this example, the following commands are run to create a Apache Tomcat test page under the root directory and change the owner of files under the root directory to www:
    echo Tomcat test > /data/wwwroot/default/index.jsp
    chown -R www.www /data/wwwroot

Step 3: Install JDK

  1. Run the following command to create a directory:
    mkdir /usr/java
  2. Run the following commands in sequence to grant the execute permissions on jdk-8u241-linux-x64.tar.gz and decompress it to /usr/java:
    chmod +x jdk-8u241-linux-x64.tar.gz
    tar xzf jdk-8u241-linux-x64.tar.gz -C /usr/java
  3. Set environment variables.
    1. Run the vi /etc/profile command to open the /etc/profile file.
    2. Press the I key to add the following content:
      # set java environment
      export JAVA_HOME=/usr/java/jdk1.8.0_241
      export CLASSPATH=$JAVA_HOME/lib/tools.jar:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib
      export PATH=$JAVA_HOME/bin:$PATH
    3. Press the Esc key to exit the edit mode. Enter :wq and press the Enter key to save and close the configuration file.
  4. Run the following command to load the environment variables:
    source /etc/profile
  5. Run the following command to view the JDK version:
    java -version
    The following command output indicates that JDK is installed. jdk180241

Step 4: Install Apache Tomcat

  1. Run the following commands in sequence.
    1. Decompress apache-tomcat-8.5.53.tar.gz.
      tar xzf apache-tomcat-8.5.53.tar.gz
    2. Rename the Apache Tomcat directory.
      mv apache-tomcat-8.5.53 /usr/local/tomcat/
    3. Configure the owner of the file.
      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 to store web application files by default.
    • logs: stores Apache Tomcat operation log files.
  2. Configure the server.xml file.
    1. Run the following command to go to the /usr/local/tomcat/conf/ directory:
      cd /usr/local/tomcat/conf/
    2. Run the following command to 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.
  3. 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.
  4. Configure a script to enable Apache Tomcat to run on system startup.
    1. Run the following command to download the script.
      Note This script originates from the community and is 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 operations. 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. 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. Run the following command to configure the JAVA_HOME script to enable Apache Tomcat to run on system startup.
      Notice The JDK version in the script must be the same as that you installed. Otherwise, Apache Tomcat cannot start.
      sed -i 's@^export JAVA_HOME=.*@export JAVA_HOME=/usr/java/jdk1.8.0_241@' /etc/init.d/tomcat                  
  5. Run the following commands in sequence to enable Apache Tomcat to run on system startup:
    1. chkconfig --add tomcat
    2. chkconfig tomcat on
  6. Run the following command to start Apache Tomcat:
    service tomcat start             
  7. 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 response indicates that Apache Tomcat is installed. Responses

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.