Introduction:
Apache Tomcat is a popular open-source web server and servlet container that allows you to run Java-based web applications. In this blog post, we will guide you through the step-by-step process of installing and configuring Apache Tomcat on an AWS EC2 instance. Additionally, we’ll show you how to manage Tomcat using command-line tools and access the Tomcat web interface.
Step 1: Install Java on the Machine
-
- SSH into your EC2 instance.
- Install Java using the
amazon-linux-extras
command.
#sudo amazon-linux-extras install java-openjdk11 -y
Step 2: Install Apache Tomcat
-
- Switch to the root user.
#sudo su -
-
- Navigate to the /opt directory.
#cd /opt
-
- Download the Apache Tomcat binary.
wget https://dlcdn.apache.org/tomcat/tomcat-10/v10.1.11/bin/apache-tomcat-10.1.11.tar.gz
-
- Unzip the Tomcat binary.
tar -xvzf apache-tomcat-10.1.11.tar.gz
Step 3: Add Execute Permission to Startup and Shutdown Scripts
-
- Change the current directory to the Tomcat bin folder.
cd apache-tomcat-10.1.11/bin
-
- Add execute permission to the ‘startup.sh’ and ‘shutdown.sh’ scripts.
chmod +x startup.sh
chmod +x shutdown.sh
Step 4: Create Links for Tomcat Server Start and Stop
-
- Create symbolic links for easy Tomcat server startup and shutdown.
ln -s /opt/apache-tomcat-10.1.11/bin/startup.sh /usr/local/bin/tomcatup
ln -s /opt/apache-tomcat-10.1.11/bin/shutdown.sh /usr/local/bin/tomcatdown
Step 5: Configure Tomcat
-
- Find all the ‘context.xml’ files within the Tomcat directory.
cd /opt/apache-tomcat-10.1.11
find -name context.xml
-
- Open each context.xml file using a text editor (vim) and comment the line with the RemoteAddrValve.
For Example:
<!-- <Valve className="org.apache.catalina.valves.RemoteAddrValve"
allow="127\.\d+\.\d+\.\d+|::1|0:0:0:0:0:0:0:1" /> -->
Step 6: Update User Information in tomcat-users.xml
-
- Open the ‘tomcat-users.xml’ file for editing.
cd /opt/apache-tomcat-10.1.11/conf
vi tomcat-users.xml
-
- Add the following lines between the
<tomcat-users>
tags to set up user roles.
- Add the following lines between the
<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"/>
-
- Save the file and exit the text editor.
Step 7: Start the Tomcat Server
-
- Start the Tomcat server using the symbolic link.
tomcatup
Step 8: Access the Tomcat Web Interface
-
- Open your web browser and navigate to <http://<your_server_public_ip>:8080.>
- You will see the Tomcat web interface, and you can log in using the credentials you set up in the Tomcat-users.xml file.
Conclusion:
Congratulations! You have successfully installed and configured Apache Tomcat on your AWS EC2 instance. Now you can start deploying Java-based web applications and use Tomcat’s management interface to monitor and control your applications.