Tomcat Server Setup & Configuration on EC2 instance
In this article, we will set up an Apache Tomcat server on an EC2 instance.
1. Pre-requisites
- Launch EC2 instance (RedHat Linux server) and install Java 11
- you can follow this article to set up Java on your EC2 instance.
2. Install Apache Tomcat
1. Download tomcat packages from https://tomcat.apache.org/download-80.cgi onto /opt on EC2 instance
- Note: Make sure you change
<version>
with the tomcat version which you download. You can perform this using below commands as well.
sudo su - # make sure you are root user, you can switch to root using this command
cd /opt #after root , chnage path to opt
#if you have not installed wget, you can install with $ yum install wget -y
wget https://downloads.apache.org/tomcat/tomcat-8/v8.5.84/bin/apache-tomcat-8.5.84.tar.gz
tar -zvxf /opt/apache-tomcat-<version>.tar.gz
2. Now open the apache-tomcat-8.5.84 directory
cd apache-tomcat-8.5.84
ls
cd bin
ls
ps -ef | grep tomcat #to check out services are satrted or not
ls -ltr #To check permissions on startup.sh
3. Let’s change the permissions of a startup.sh file, give execute permissions to startup. sh and shutdown. sh which are under the bin.
1.
chmod +x /opt/apache-tomcat-<version>/bin/startup.sh #if you are not in bin directory
OR
chmod +x startup.sh #if you are already in bin directory
2.
chmod +x /opt/apache-tomcat-<version>/bin/shutdown.sh #if you are not in bin directory
OR
chmod +x shutdown.sh #if you are already in bin directory
- Note: you may get the below error while starting tomcat in case if you don't 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
4. Create link files for tomcat startup.sh and shutdown.sh
pwd
echo $PATH
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 #to start tomcat
ps -ef | grep tomcat #to check out services are satrted or not
3. 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
vi server.xml
# restart tomcat after configuration update
tomcatdown
tomcatup
4. Access the tomcat application from the browser on port 8090
- http://<Public_IP>:8090
- Now the application is accessible on port 8090. but the tomcat application doesn't allow login from the 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 that are under webapp directory. After that restart tomcat services to effect these changes. At the time of writing, the below 2 files are updated.
vi /opt/tomcat<version>/webapps/host-manager/META-INF/context.xml
vi /opt/tomcat/webapps/manager/META-INF/context.xml
# Restart tomcat services
tomcatdown
tomcatup
3. Update user's information in the tomcat-users.xml file go to the tomcat home directory and Add below users to conf/tomcat-users.xml file.
Then you will be able to log in.
vi tomcat-users.xml
<role rolename="manager-gui"/>
<role rolename="manager-script"/>
<role rolename="manager-jmx"/>
<role rolename="manager-status"/>
<user username="admin" password="adminpassword" 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 the service and try to log in to the tomcat application from the browser. This time it should be Successful.
We are done with the Tomcat setup. Thanks for reading my article.
You can Also Follow Me on My Social Media Platforms:
Thank You for reading