Monday 11 March 2019

How to install Tomcat on EC2 Server by Raj Gupta

Pre-requisites

  1. EC2 instance with Java 11
[root@ip-172-31-88-92 ~]# amazon-linux-extras install java-openjdk11

Install Apache Tomcat

  1. Download tomcat packages from https://tomcat.apache.org/download-90.cgi onto /opt on EC2 instance

    Note: Make sure you change <version> with the tomcat version which you download.

    # Create tomcat directory
    cd /opt
    wget https://dlcdn.apache.org/tomcat/tomcat-9/v9.0.65/bin/apache-tomcat-9.0.65.tar.gz
    tar -xvzf /opt/apache-tomcat-<version>.tar.gz
  2. give executing permissions to startup.sh and shutdown.sh which are under bin.
    chmod +x /opt/apache-tomcat-<version>/bin/startup.sh 
    chmod +x /opt/apache-tomcat-<version>/bin/shutdown.sh

    Note: you may get below error while starting tomcat incase if you dont install Java
    Neither the JAVA_HOME nor the JRE_HOME environment variable is defined At least one of these environment variable is needed to run this program

  3. create link files for tomcat startup.sh and shutdown.sh
    ln -s /opt/apache-tomcat-<version>/bin/startup.sh /usr/local/bin/tomcatup
    ln -s /opt/apache-tomcat-<version>/bin/shutdown.sh /usr/local/bin/tomcatdown
    tomcatup

Check point :

access tomcat application from browser on port 8080

  • http://<Public_IP>:8080

Using unique ports for each application is a best practice in an environment. But tomcat and Jenkins runs on ports number 8080. Hence lets change tomcat port number to 8090. Change port number in conf/server.xml file under tomcat home

cd /opt/apache-tomcat-<version>/conf
# update port number in the "connecter port" field in server.xml
# restart tomcat after configuration update
tomcatdown
tomcatup

Check point :

Access tomcat application from browser on port 8090

  • http://<Public_IP>:8090
  1. now application is accessible on port 8090. but tomcat application doesnt allow to login from browser. changing a default parameter in context.xml does address this issue
    #search for context.xml
    find / -name context.xml
  2. above command gives 3 context.xml files. comment () Value ClassName field on files which are under webapp directory. After that restart tomcat services to effect these changes. At the time of writing this lecture below 2 files are updated.
    /opt/tomcat/webapps/host-manager/META-INF/context.xml
    /opt/tomcat/webapps/manager/META-INF/context.xml
    
    # Restart tomcat services
    tomcatdown  
    tomcatup
  3. Update users information in the tomcat-users.xml file goto tomcat home directory and Add below users to conf/tomcat-users.xml file
     <role rolename="manager-gui"/>
     <role rolename="manager-script"/>
     <role rolename="manager-jmx"/>
     <role rolename="manager-status"/>
     <user username="admin" password="admin" roles="manager-gui, manager-script, manager-jmx, manager-status"/>
     <user username="deployer" password="deployer" roles="manager-script"/>
     <user username="tomcat" password="s3cret" roles="manager-gui"/>
  4. Restart serivce and try to login to tomcat application from the browser. This time it should be Successful



[ec2-user@ip-172-31-88-92 ~]$ sudo -i
[root@ip-172-31-88-92 ~]# clear
[root@ip-172-31-88-92 ~]# cd /opt/
[root@ip-172-31-88-92 opt]# ls
apache-tomcat-9.0.65.tar.gz  aws  rh  tomcat
[root@ip-172-31-88-92 opt]# cd tomcat/
[root@ip-172-31-88-92 tomcat]# ls
bin  BUILDING.txt  conf  CONTRIBUTING.md  lib  LICENSE  logs  NOTICE  README.md  RELEASE-NOTES  RUNNING.txt  temp  webapps  work
[root@ip-172-31-88-92 tomcat]# find / -name context.xml
/opt/tomcat/conf/context.xml
/opt/tomcat/webapps/examples/META-INF/context.xml
/opt/tomcat/webapps/host-manager/META-INF/context.xml
/opt/tomcat/webapps/manager/META-INF/context.xml
[root@ip-172-31-88-92 tomcat]# vi /opt/tomcat/webapps/host-manager/META-INF/context.xml
[root@ip-172-31-88-92 tomcat]# vi /opt/tomcat/webapps/manager/META-INF/context.xml
[root@ip-172-31-88-92 tomcat]# cd bin/
[root@ip-172-31-88-92 bin]# ls -ltr
total 892
-rwxr-x--- 1 root root   1908 Jul 14 12:28 version.sh
-rw-r----- 1 root root   2026 Jul 14 12:28 version.bat
-rwxr-x--- 1 root root   5540 Jul 14 12:28 tool-wrapper.sh
-rw-r----- 1 root root   4574 Jul 14 12:28 tool-wrapper.bat
-rw-r----- 1 root root 436593 Jul 14 12:28 tomcat-native.tar.gz
-rw-r----- 1 root root  47050 Jul 14 12:28 tomcat-juli.jar
-rwxr-x--- 1 root root   1904 Jul 14 12:28 startup.sh
-rw-r----- 1 root root   2022 Jul 14 12:28 startup.bat
-rwxr-x--- 1 root root   1902 Jul 14 12:28 shutdown.sh
-rw-r----- 1 root root   2020 Jul 14 12:28 shutdown.bat
-rwxr-x--- 1 root root   3708 Jul 14 12:28 setclasspath.sh
-rw-r----- 1 root root   3460 Jul 14 12:28 setclasspath.bat
-rwxr-x--- 1 root root   3382 Jul 14 12:28 makebase.sh
-rw-r----- 1 root root   3606 Jul 14 12:28 makebase.bat
-rwxr-x--- 1 root root   1965 Jul 14 12:28 digest.sh
-rw-r----- 1 root root   2091 Jul 14 12:28 digest.bat
-rwxr-x--- 1 root root   9100 Jul 14 12:28 daemon.sh
-rwxr-x--- 1 root root   1922 Jul 14 12:28 configtest.sh
-rw-r----- 1 root root   2040 Jul 14 12:28 configtest.bat
-rw-r----- 1 root root 211777 Jul 14 12:28 commons-daemon-native.tar.gz
-rw-r----- 1 root root  25410 Jul 14 12:28 commons-daemon.jar
-rwxr-x--- 1 root root   1997 Jul 14 12:28 ciphers.sh
-rw-r----- 1 root root   2123 Jul 14 12:28 ciphers.bat
-rw-r----- 1 root root   1664 Jul 14 12:28 catalina-tasks.xml
-rwxr-x--- 1 root root  25294 Jul 14 12:28 catalina.sh
-rw-r----- 1 root root  16840 Jul 14 12:28 catalina.bat
-rw-r----- 1 root root  34700 Jul 14 12:28 bootstrap.jar
[root@ip-172-31-88-92 bin]# ./shutdown.sh
Using CATALINA_BASE:   /opt/tomcat
Using CATALINA_HOME:   /opt/tomcat
Using CATALINA_TMPDIR: /opt/tomcat/temp
Using JRE_HOME:        /
Using CLASSPATH:       /opt/tomcat/bin/bootstrap.jar:/opt/tomcat/bin/tomcat-juli.jar
Using CATALINA_OPTS:
NOTE: Picked up JDK_JAVA_OPTIONS:  --add-opens=java.base/java.lang=ALL-UNNAMED --add-opens=java.base/java.io=ALL-UNNAMED --add-opens=java.base/java.util=ALL-UNNAMED --add-opens=java.base/java.util.concurrent=ALL-UNNAMED --add-opens=java.rmi/sun.rmi.transport=ALL-UNNAMED
[root@ip-172-31-88-92 bin]# ./startup.sh
Using CATALINA_BASE:   /opt/tomcat
Using CATALINA_HOME:   /opt/tomcat
Using CATALINA_TMPDIR: /opt/tomcat/temp
Using JRE_HOME:        /
Using CLASSPATH:       /opt/tomcat/bin/bootstrap.jar:/opt/tomcat/bin/tomcat-juli.jar
Using CATALINA_OPTS:
Tomcat started.
[root@ip-172-31-88-92 bin]# cd .
[root@ip-172-31-88-92 bin]# cd ..
[root@ip-172-31-88-92 tomcat]# ls
bin  BUILDING.txt  conf  CONTRIBUTING.md  lib  LICENSE  logs  NOTICE  README.md  RELEASE-NOTES  RUNNING.txt  temp  webapps  work
[root@ip-172-31-88-92 tomcat]# cd conf/
[root@ip-172-31-88-92 conf]# ls
Catalina         catalina.properties  jaspic-providers.xml  logging.properties  tomcat-users.xml  web.xml
catalina.policy  context.xml          jaspic-providers.xsd  server.xml          tomcat-users.xsd
[root@ip-172-31-88-92 conf]# vi tomcat-users.xml
[root@ip-172-31-88-92 conf]# vi tomcat-users.xml
[root@ip-172-31-88-92 conf]# cat tomcat-users.xml
<?xml version="1.0" encoding="UTF-8"?>
<!--
  Licensed to the Apache Software Foundation (ASF) under one or more
  contributor license agreements.  See the NOTICE file distributed with
  this work for additional information regarding copyright ownership.
  The ASF licenses this file to You under the Apache License, Version 2.0
  (the "License"); you may not use this file except in compliance with
  the License.  You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
-->
<tomcat-users xmlns="http://tomcat.apache.org/xml"
              xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
              xsi:schemaLocation="http://tomcat.apache.org/xml tomcat-users.xsd"
              version="1.0">
<!--
  By default, no user is included in the "manager-gui" role required
  to operate the "/manager/html" web application.  If you wish to use this app,
  you must define such a user - the username and password are arbitrary.

  Built-in Tomcat manager roles:
    - manager-gui    - allows access to the HTML GUI and the status pages
    - manager-script - allows access to the HTTP API and the status pages
    - manager-jmx    - allows access to the JMX proxy and the status pages
    - manager-status - allows access to the status pages only

  The users below are wrapped in a comment and are therefore ignored. If you
  wish to configure one or more of these users for use with the manager web
  application, do not forget to remove the <!.. ..> that surrounds them. You
  will also need to set the passwords to something appropriate.
-->
<!--
  <user username="admin" password="<must-be-changed>" roles="manager-gui"/>
  <user username="robot" password="<must-be-changed>" roles="manager-script"/>
-->
<!--
  The sample user and role entries below are intended for use with the
  examples web application. They are wrapped in a comment and thus are ignored
  when reading this file. If you wish to configure these users for use with the
  examples web application, do not forget to remove the <!.. ..> that surrounds
  them. You will also need to set the passwords to something appropriate.
-->
<role rolename="manager-gui"/>
<role rolename="manager-script"/>
<role rolename="manager-jmx"/>
<role rolename="manager-status"/>
<user username="admin" password="admin" roles="manager-gui, manager-script, manager-jmx, manager-status"/>
<user username="deployer" password="deployer" roles="manager-script"/>
<user username="tomcat" password="s3cret" roles="manager-gui"/>
<!--
  <role rolename="tomcat"/>
  <role rolename="role1"/>
  <user username="tomcat" password="<must-be-changed>" roles="tomcat"/>
  <user username="both" password="<must-be-changed>" roles="tomcat,role1"/>
  <user username="role1" password="<must-be-changed>" roles="role1"/>
-->
</tomcat-users>
[root@ip-172-31-88-92 conf]# ln -s /opt/tomcat/bin/startup.sh /usr/local/bin/tomcatup
[root@ip-172-31-88-92 conf]# echo $PATH
/usr/local/sbin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin
[root@ip-172-31-88-92 conf]# ln -s /opt/tomcat/bin/shutdown.sh /usr/local/bin/tomcatdown
[root@ip-172-31-88-92 conf]# tomcatdown
-bash: tomcatdown: command not found
[root@ip-172-31-88-92 conf]# tomcatdown
-bash: tomcatdown: command not found
[root@ip-172-31-88-92 conf]# cd  /usr/local/bin
[root@ip-172-31-88-92 bin]# ls -l
total 0
lrwxrwxrwx 1 root root 27 Sep 13 04:14 tomcatdown -> /opt/tomcat/bin/shutdown.sh
lrwxrwxrwx 1 root root 26 Sep 13 04:12 tomcatup -> /opt/tomcat/bin/startup.sh
[root@ip-172-31-88-92 bin]# tomcatdown
-bash: tomcatdown: command not found
[root@ip-172-31-88-92 bin]# cd
[root@ip-172-31-88-92 ~]# ls
[root@ip-172-31-88-92 ~]# cd /opt/
[root@ip-172-31-88-92 opt]# ls
apache-tomcat-9.0.65.tar.gz  aws  rh  tomcat
[root@ip-172-31-88-92 opt]# cd tomcat/
[root@ip-172-31-88-92 tomcat]# ls
bin  BUILDING.txt  conf  CONTRIBUTING.md  lib  LICENSE  logs  NOTICE  README.md  RELEASE-NOTES  RUNNING.txt  temp  webapps  work
[root@ip-172-31-88-92 tomcat]# cd conf/
[root@ip-172-31-88-92 conf]# ls
Catalina         catalina.properties  jaspic-providers.xml  logging.properties  tomcat-users.xml  web.xml
catalina.policy  context.xml          jaspic-providers.xsd  server.xml          tomcat-users.xsd
[root@ip-172-31-88-92 conf]# tomcatdown
-bash: tomcatdown: command not found
[root@ip-172-31-88-92 conf]# cd ..
[root@ip-172-31-88-92 tomcat]# cd bin/
[root@ip-172-31-88-92 bin]# ls
bootstrap.jar       ciphers.bat                   configtest.bat  digest.sh         setclasspath.sh  startup.sh            tool-wrapper.sh
catalina.bat        ciphers.sh                    configtest.sh   makebase.bat      shutdown.bat     tomcat-juli.jar       version.bat
catalina.sh         commons-daemon.jar            daemon.sh       makebase.sh       shutdown.sh      tomcat-native.tar.gz  version.sh
catalina-tasks.xml  commons-daemon-native.tar.gz  digest.bat      setclasspath.bat  startup.bat      tool-wrapper.bat
[root@ip-172-31-88-92 bin]# ./shutdown.sh
Using CATALINA_BASE:   /opt/tomcat
Using CATALINA_HOME:   /opt/tomcat
Using CATALINA_TMPDIR: /opt/tomcat/temp
Using JRE_HOME:        /
Using CLASSPATH:       /opt/tomcat/bin/bootstrap.jar:/opt/tomcat/bin/tomcat-juli.jar
Using CATALINA_OPTS:
NOTE: Picked up JDK_JAVA_OPTIONS:  --add-opens=java.base/java.lang=ALL-UNNAMED --add-opens=java.base/java.io=ALL-UNNAMED --add-opens=java.base/java.util=ALL-UNNAMED --add-opens=java.base/java.util.concurrent=ALL-UNNAMED --add-opens=java.rmi/sun.rmi.transport=ALL-UNNAMED
[root@ip-172-31-88-92 bin]# ./startup.sh
Using CATALINA_BASE:   /opt/tomcat
Using CATALINA_HOME:   /opt/tomcat
Using CATALINA_TMPDIR: /opt/tomcat/temp
Using JRE_HOME:        /
Using CLASSPATH:       /opt/tomcat/bin/bootstrap.jar:/opt/tomcat/bin/tomcat-juli.jar
Using CATALINA_OPTS:
Tomcat started.
[root@ip-172-31-88-92 bin]#


=====================================================
Install java and set the path

yum install java-1.8*

then set the envirmement variable in file sudo vi .bash_profile

[root@ip-172-31-80-137 ~]# sudo vi .bash_profile

JAVA_HOME=/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.191.b12-1.el7_6.x86_64

PATH=$PATH:$JAVA_HOME:$HOME/bin

export PATH

[root@ip-172-31-80-137 ~]# echo $PATH
/usr/local/sbin:/sbin:/bin:/usr/sbin:/usr/bin:/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.191.b12-1.el7_6.x86_64:/root/bin


Now make the directory 
[root@ip-172-31-80-137 ~]# mkdir app
[root@ip-172-31-80-137 ~]# cd app

Now download and install it
yum install wget -y
tar -zvxf apache-tomcat-8.5.37.tar.gz

Now goto
cd apache-tomcat-8.5.37
ls
cd bin
ls
ps -ef | grep tomcat
ls -ltr

Now change the permission for below file
chmod +x startup.sh
chmod +x shutdown.sh

Now set the soft link
[root@ip-172-31-80-137 bin]# ln -s /root/app/apache-tomcat-8.5.37/bin/startup.sh /usr/local/bin/tomcatup
[root@ip-172-31-80-137 bin]# ln -s /root/app/apache-tomcat-8.5.37/bin/shutdown.sh /usr/local/bin/tomcatdown
[root@ip-172-31-80-137 bin]# cd /usr/local/bin/
[root@ip-172-31-80-137 bin]# ll
total 0
lrwxrwxrwx. 1 root root 46 Jan 11 15:21 tomcatdown -> /root/app/apache-tomcat-8.5.37/bin/shutdown.sh
lrwxrwxrwx. 1 root root 45 Jan 11 15:20 tomcatup -> /root/app/apache-tomcat-8.5.37/bin/startup.sh

[root@ip-172-31-80-137 bin]# ./tomcatup    ---to start tomcat


then go to browser and give




To change the port: -
Go to

cd app/apache-tomcat-8.5.37/   from root user
[root@ip-172-31-41-184 ~]# cd app/apache-tomcat-8.5.37/
[root@ip-172-31-41-184 apache-tomcat-8.5.37]# cd conf
                                                                                          vi server.xml

then in this file change the port for which to open


Now to run app on tomcat server do below

[root@ip-172-31-41-184 conf]# find / -name context.xml
/root/app/apache-tomcat-8.5.37/conf/context.xml
/root/app/apache-tomcat-8.5.37/webapps/host-manager/META-INF/context.xml
/root/app/apache-tomcat-8.5.37/webapps/manager/META-INF/context.xml

Comment the below two line in below two files: -
vi /root/app/apache-tomcat-8.5.37/webapps/host-manager/META-INF/context.xml
vi /root/app/apache-tomcat-8.5.37/webapps/manager/META-INF/context.xml


<!--  <Valve className="org.apache.catalina.valves.RemoteAddrValve"
         allow="127\.\d+\.\d+\.\d+|::1|0:0:0:0:0:0:0:1" /> -->

then when we logging then it will ask username and password


to create user with policy, go to file

[root@ip-172-31-41-184 conf]# vi tomcat-users.xml

Then add below yellow line and save it

<tomcat-users xmlns="http://tomcat.apache.org/xml"
              xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
              xsi:schemaLocation="http://tomcat.apache.org/xml tomcat-users.xsd"
              version="1.0">
<!--
  NOTE:  By default, no user is included in the "manager-gui" role required
  to operate the "/manager/html" web application.  If you wish to use this app,
  you must define such a user - the username and password are arbitrary. It is
  strongly recommended that you do NOT use one of the users in the commented out
  section below since they are intended for use with the examples web
  application.
-->
<!--
  NOTE:  The sample user and role entries below are intended for use with the
  examples web application. They are wrapped in a comment and thus are ignored
  when reading this file. If you wish to configure these users for use with the
  examples web application, do not forget to remove the <!.. ..> that surrounds
  them. You will also need to set the passwords to something appropriate.
-->
<!-- <role rolename="manager-gui"/>
  <user username="tomcat" password="password" roles="manager-gui"/> -->
  <role rolename="manager-script"/>
  <user username="deployer" password="deployer" roles="manager-script"/>
<!--
  <role rolename="tomcat"/>
  <role rolename="role1"/>
  <user username="tomcat" password="<must-be-changed>" roles="tomcat"/>
  <user username="both" password="<must-be-changed>" roles="tomcat,role1"/>
  <user username="role1" password="<must-be-changed>" roles="role1"/>
-->
</tomcat-users>


Then stop and start the tomcat server


[root@ip-172-31-41-184 conf]# vi tomcat-users.xml
[root@ip-172-31-41-184 conf]# /usr/local/bin/tomcatdown
Using CATALINA_BASE:   /root/app/apache-tomcat-8.5.37
Using CATALINA_HOME:   /root/app/apache-tomcat-8.5.37
Using CATALINA_TMPDIR: /root/app/apache-tomcat-8.5.37/temp
Using JRE_HOME:        /
Using CLASSPATH:       /root/app/apache-tomcat-8.5.37/bin/bootstrap.jar:/root/app/apache-tomcat-8.5.37/bin/tomcat-juli.jar
[root@ip-172-31-41-184 conf]# /usr/local/bin/tomcatup
Using CATALINA_BASE:   /root/app/apache-tomcat-8.5.37
Using CATALINA_HOME:   /root/app/apache-tomcat-8.5.37
Using CATALINA_TMPDIR: /root/app/apache-tomcat-8.5.37/temp
Using JRE_HOME:        /
Using CLASSPATH:       /root/app/apache-tomcat-8.5.37/bin/bootstrap.jar:/root/app/apache-tomcat-8.5.37/bin/tomcat-juli.jar
Tomcat started.

No comments:

Post a Comment