All Products
Search
Document Center

Elastic Compute Service:Manually deploy a Java web environment on an Alibaba Cloud Linux 3 instance

Last Updated:Sep 26, 2023

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

Prerequisites

Before you deploy a Java web environment on an ECS instance, make sure that the instance meets the following requirements:

  • The instance has a public IP address or an elastic IP address (EIP). For more information, see Associate or disassociate an EIP.

  • The instance runs Alibaba Cloud Linux 3.

  • An inbound security group rule is added to a security group of the instance to allow traffic on ports 22, 80, 443, and 8080. For more information, see Add a security group rule.

Step 1: Disable the firewall and SELinux

  1. Connect to the Linux instance.

  2. Disable the firewall.

    1. Run the following command to check the state of the firewall:

      systemctl status firewalld
      查看防火墙状态
      • If the firewall is in the inactive state, the firewall is disabled.

      • If the firewall is in the active state, the firewall is enabled. In this example, the firewall is in the active state and must be disabled. Perform the following steps:

        • To temporarily disable the firewall, run the following command:

          sudo systemctl stop firewalld
          Note

          After you run the preceding command, the firewall is temporarily 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 command:

          sudo systemctl disable firewalld
          Note

          You can re-enable the firewall after it is disabled. For more information, visit the official firewalld website.

  3. Disable SELinux.

    1. Run the following command to check the state of SELinux:

      getenforce
      • If SELinux is in the Disabled state, SELinux is disabled.

      • If SELinux is in the Enforcing state, SELinux is enabled.

    2. Disable SELinux. If SELinux is disabled, skip this step.

      • To temporarily disable SELinux, run the setenforce 0 command.

        Note

        After you run the preceding command, SELinux is temporarily 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 vi /etc/selinux/config command to edit the SELinux configuration file. Press the Enter key. Move the pointer to the SELINUX=enforcing line and press the I key to enter Insert mode. Replace SELINUX=enforcing with SELINUX=disabled. Press the Esc key, enter :wq, and then press the Enter key to save and close the SELinux configuration file. Restart the operating system to make the settings take effect.

        Note

        You can re-enable SELinux after it is disabled. For more information, see the official SELinux documentation.

Step 2: Install JDK

In this example, JDK 1.8.0_351 is used.

  1. Run the following command to query JDK packages:

    yum -y list java*
  2. Run the following command to install a JDK package.

    In this example, the JDK 1.8.0 package is installed.

    sudo yum -y install java-1.8.0-openjdk-devel.x86_64
  3. Run the following command to check the version of JDK:

    java -version

    Sample command output:

    openjdk version "1.8.0_372"
    OpenJDK Runtime Environment (build 1.8.0_372-b07)
    OpenJDK 64-Bit Server VM (build 25.372-b07, mixed mode)
  4. Run the following command to view the JDK installation path:

    find /usr/lib/jvm -name 'java-1.8.0-openjdk-1.8.0*'

    Sample command output:

    /usr/lib/jvm/java-1.8.0-openjdk-1.8.0.372.b07-4.0.2.al8.x86_64
  5. Configure Java environment variables.

    1. Run the following command to open the configuration file:

      sudo vim /etc/profile
    2. Press the I key to enter Insert mode.

    3. Add the following content to the end of the script:

      JAVA_HOME=/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.372.b07-4.0.2.al8.x86_64
      PATH=$PATH:$JAVA_HOME/bin
      CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
      export JAVA_HOME CLASSPATH PATH

      In the preceding information, set JAVA_HOME to the JDK installation path. For more information about the path, see Step 2.4 of this topic.

    4. Press the Esc key to exit Insert mode. Enter :wq and press the Enter key to save and close the file.

    5. Run the following command to apply the environment variables:

      source /etc/profile

Step 3: Install Apache Tomcat

Apache Tomcat is a web application server. In this example, Apache Tomcat 8.5.83 is used.

  1. Run the following commands to download and decompress the Apache Tomcat 8 installation package:

    wget --no-check-certificate https://dlcdn.apache.org/tomcat/tomcat-8/v8.5.88/bin/apache-tomcat-8.5.88.tar.gz
    tar -zxvf apache-tomcat-8.5.88.tar.gz
    Note

    The download URLs of Apache Tomcat may change. If the preceding download URL is invalid, visit the official Apache Tomcat website to obtain the latest download URL.

  2. Run the following command to move the Apache Tomcat installation files to the /usr/local/tomcat/ directory:

    mv apache-tomcat-8.5.88 /usr/local/tomcat/
  3. 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 file named server.xml.

      1. Run the following command to create and open the server.xml file:

        vim server.xml
      2. Press the I key to enter Insert mode and then add the following content to the file:

        <?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 Insert mode. Enter :wq and press the Enter key to save and close the file.

  4. 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:

      vim /usr/local/tomcat/bin/setenv.sh
    2. Press the I key to enter Insert mode and then add the following content to the file.

      Specify the JAVA_OPTS parameter to set JVM memory information and the encoding format. In this example, the encoding format is set to UTF-8.

      JAVA_OPTS='-Djava.security.egd=file:/dev/./urandom -server -Xms256m -Xmx496m -Dfile.encoding=UTF-8'
    3. Press the Esc key to exit Insert mode. Enter :wq and press the Enter key to save and close the file.

  5. Configure a script to enable Apache Tomcat to automatically start 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 operations. If you cannot download the script by running the wget command, access https://raw.githubusercontent.com/oneinstack/oneinstack/master/init.d/Tomcat-init in your browser 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:

      sudo 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 set JAVA_HOME in the script.

      Important

      The JDK path specified in the script must be the JDK installation path. Otherwise, Apache Tomcat cannot start. For more information about the path, see Step 2.4 of this topic.

      sudo sed -i 's@^export JAVA_HOME=.*@export JAVA_HOME=/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.372.b07-4.0.2.al8.x86_64@' /etc/init.d/tomcat
  6. Run the following commands in sequence to enable Apache Tomcat to automatically start on system startup:

    sudo chkconfig --add tomcat
    sudo chkconfig tomcat on
  7. Run the following command to start Apache Tomcat:

    sudo service tomcat start

    Apache Tomcat is started. Sample output:

    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.372.b07-4.0.2.al8.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: 4517

Step 4: Deploy a test project to check whether the Java web environment is deployed

Upload the WAR package of Java web project files to the website root directory. You can use a remote connection tool that provides 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. Run the following command to create a website root directory:

    mkdir -p /data/wwwroot/default
  2. Run the following command to create a test file:

    echo sh -c Tomcat test > /data/wwwroot/default/index.jsp 'echo "${vSGX_ip_addr} grpc.tf-serving.service.com" >> /etc/hosts'
  3. Open your browser and enter the following address in the address bar to check whether a Java web environment is deployed: http://<Public IP address of the instance>:8080.

    The page shown in the following figure indicates that the Java web environment is deployed.返回结果

FAQ

If the Java web environment is inaccessible, perform the following operations to troubleshoot the issue:

  • Check whether rules are added to the security groups of the instance to allow traffic on Apache Tomcat port 8080. For more information, see Add a security group rule.

  • Check whether the JDK installation path is properly configured. You can run the find /usr/lib/jvm -name 'java-1.8.0-openjdk-1.8.0*' command to check the JDK installation path. Two settings involve the JDK installation path. For more information, see Step 2.5 and Step 3.5 of this topic.