All Products
Search
Document Center

Elastic Compute Service:Deploy a Java Web environment

Last Updated:Jul 18, 2024

Java Web is a technology stack that integrates the Java programming language and various technologies and frameworks to allow the development of dynamic websites and web applications. Developers can use Java Web to develop complex, high-performance web applications that can be deployed across platforms. Apache Tomcat is the most popular web server environment that is used to deploy and run Java web applications. This topic describes how to deploy a Java web environment on an Elastic Compute Service (ECS) instance that runs Alibaba Cloud Linux 2, Alibaba Cloud Linux 3, or CentOS 7.x.

Prerequisites

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

  • The instance is assigned a public IP address by the system or is associated with an elastic IP address (EIP). For information about how to associate an EIP with an instance, see Associate or disassociate an EIP.

  • The instance runs Alibaba Cloud Linux 2, Alibaba Cloud Linux 3, or CentOS 7.x.

  • An inbound 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 ECS instance on which you want to deploy a Java web environment.

  2. Disable the firewall.

    1. Run the following command to view the status 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 you disable the firewall. 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 modify 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 changes take effect.

        Note

        You can re-enable SELinux after you disable SELinux. For more information, see the official SELinux documentation.

Step 2: Install JDK

In this example, Java Development Kit (JDK) 1.8.0_412 is used.

Note

JDK versions vary based on the operating system. Use a JDK version that is suitable for the operating system of your ECS instance.

  1. Run the following command to query the 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_412"
    OpenJDK Runtime Environment (build 1.8.0_412-b08)
    OpenJDK 64-Bit Server VM (build 25.412-b08, 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.412.b08-2.0.1.1.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.412.b08-2.0.1.1.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 path in which JDK is installed. For 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 9.0.91 is used.

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

    wget https://archive.apache.org/dist/tomcat/tomcat-9/v9.0.91/bin/apache-tomcat-9.0.91.tar.gz  --no-check-certificate 
    tar -zxvf apache-tomcat-9.0.91.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:

    sudo mv apache-tomcat-9.0.91 /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 configure the 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.

      Important

      The following script is maintained by the community and is provided only for reference. Alibaba Cloud does not provide warranty of any kind, expressed or implied, with regard to the performance and reliability of the script or the potential impacts of the script on 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 path in which JDK is installed. Otherwise, Apache Tomcat cannot start. For 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.412.b08-2.0.1.1.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 command 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.412.b08-2.0.1.1.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:

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

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

    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 a rule is added to a security group 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.