All Products
Search
Document Center

Elastic Compute Service:Manually deploy a Java web environment on a CentOS 7 instance

Last Updated:Aug 09, 2023

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.88

    Note

    In this example, Apache Tomcat 8.5.88 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 ECS 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 or key.

  3. Disable the firewall.
    1. Run the systemctl status firewalld command to check the status of the firewall.
      Check the status of the firewall
      • 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.
    2. Disable the firewall. Skip this step if the firewall is disabled.
      • To temporarily disable the firewall, run the following command:
        systemctl stop firewalld
        Note After you run the preceding command, the firewall is disabled. When you restart the Linux operating system, the firewall is enabled and re-enters the active state.
      • To permanently disable the firewall, perform the following steps:
        1. Run the following command to stop the firewall:
          systemctl stop firewalld
        2. Run the following command to disable the firewall and prevent the firewall from starting on instance startup:
          systemctl disable firewalld
        Note After you run the preceding commands, the firewall is disabled. When you restart the instance, the firewall remains disabled. You can re-enable the firewall. For more information, see Firewalld documentation.
  4. Disable Security-Enhanced Linux (SELinux).
    1. Check the status of SELinux.
      getenforce
      The following figure shows an example command output:Check the status of SELinux
      • If SELinux is in the Disabled state, SELinux is disabled.
      • If SELinux is in the Enforcing state, SELinux is enabled. In this example, SELinux is in the Enforcing state and must be disabled.
    2. Disable SELinux. Skip this step if SELinux is disabled.
      • To temporarily disable SELinux, run the following command:
        setenforce 0
        Note After you run the preceding command, SELinux is disabled. When you restart the instance, SELinux is enabled and re-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 and press the Esc key to exit the edit mode. Enter :wq and press the Enter key to save and close the file.
        Note You can re-enable SELinux after it is disabled. For more information, see Enable or disable SELinux.
    3. Restart the instance to apply the settings.

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 JDK 1.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 the JDK is installed, a command output similar to the following one is returned.

    [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 JDK installation path:

      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. Open the configuration file.

      vim /etc/profile
    3. At the end of the configuration file, press the I key to enter Insert mode.

    4. Add the following information.

      Note

      Set JAVA_HOME to the JDK installation path that you obtained.

      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, enter :wq, and then press the Enter key to save and close the configuration file.

    6. Run the following command to make environment variables take effect:

      source /etc/profile
      Important

      After environment variables are modified, the following error message may appear when you run other commands: -bash: chmod: command not found. To resolve the error, run the export PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin command.

Step 3: Install Apache Tomcat

  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. For security reasons, perform operations on Apache Tomcat as a regular user.

    In this example, a regular user named www is used.

    useradd www
  4. Run the following command to 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. The most important files are the server.xml and web.xml 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.

  5. 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 the 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 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, enter :wq, and then 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 enter Insert mode and then add the following content to the file.

      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, enter :wq, and then press the Enter key to save and close the configuration file.

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

      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 automatically start on system startup.

      Important

      The JDK version in the script must be the JDK version that you 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.342.b07-1.el7_9.x86_64@' /etc/init.d/tomcat                  
  8. Run the following commands in sequence to enable Apache Tomcat to automatically start on system startup:

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

    service tomcat start             

    If Apache Tomcat is started, a command output similar to the following one is returned.

    [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 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 and change the owner of the 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. Run the following command to create a website root directory:

    mkdir -p /data/wwwroot/default
  2. Run the following command to set the owner of the website root directory to www:

    chown -R www.www /data/wwwroot
  3. Run the following command to create the test file:

    echo Tomcat test > /data/wwwroot/default/index.jsp
  4. Open your browser and enter http://<Public IP address of the ECS instance>:8080 in the address bar to connect to the ECS instance.

    If Apache Tomcat is installed, a page similar to the following one appears. 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 traffic on port 8080. For more information, see Add a security group rule. θΏ”ε›žη»“ζžœ

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.