Updated on 2024-01-08 GMT+08:00

Installing Tomcat

Introduction

Apache Tomcat (or simply Tomcat) is an open source web server and servlet container developed by the Apache Software Foundation. This tutorial describes how you can deploy Tomcat in Huawei Cloud EulerOS 2.0.

Preparations

  • Prepare an ECS and assign a public IP address or EIP to the ECS.
  • Ensure that inbound security group rules allow traffic to flow to the ECS over port 8080.

Procedure

  1. Install Tomcat.

    1. Run the following command to install Java:
      dnf install java-1.8.0-openjdk
    2. Run the following command to check whether the installation is successful:
      java -version
    3. Run the following command to install Tomcat:
      dnf install tomcat

      The Tomcat is installed in the /usr/share/tomcat directory.

  2. Configure Tomcat.

    1. Add the following content to the /etc/profile file:
      JAVA_HOME=/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.342.b07-0.hce2.x86_64/jre
      PATH=$PATH:$JAVA_HOME/bin
      CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
      export JAVA_HOME CLASSPATH PATH
    2. Run the following command to activate the preceding environment variables:
      source /etc/profile
    3. Clear the /usr/share/tomcat/conf/server.xml file and paste 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 %s %b" /> </Host> </Engine> </Service> </Server>

      Save the settings and exit. You can customize the preceding configuration as required.

    4. Run the following command to create the directories specified by appbase and docbase in the preceding configuration:
      mkdir -p /data/wwwroot/default
    5. Run the following command to set tomcat as the owner of the preceding directory:
      chown -R tomcat.tomcat /data/wwwroot/ 
    6. Create the /usr/share/tomcat/bin/setenv.sh file and enter the following information to set JVM memory parameters:
      JAVA_OPTS='-Djava.security.egd=file:/dev/./urandom -server -Xms256m -Xmx496m -Dfile.encoding=UTF-8'
    7. Run the following command to start Tomcat:
      systemctl start tomcat
    8. Run the following command to check the Tomcat status:
      systemctl status tomcat

      If active (running) is displayed, Tomcat is started.

  3. Check whether Tomcat is installed.

    1. Run the following command to create a test page:
      echo Tomcat test > /data/wwwroot/default/index.jsp
    2. Enter http://<Public IP address of the Tomcat service>:8080 in the address box of the browser. If the following page is displayed, the installation is successful.

The preceding configuration is used only for tests. Exercise caution when using the configuration in the service environment.