Install Tomcat 10.0.27 in Ubuntu 20.04
Setup a Tomcat Server on AWS EC2 Instance & Deploy a Sample Application On it
Table of contents
No headings in the article.
Create a server using AWS
or any cloud or virtual machine for tomcat
installation
If you are using cloud server
, then access it by git bash
or putty
ssh -i <Key Pair Path> <user>@<ip_address_of_server>
Next, update the package manager
cache by running
sudo apt update
sudo apt upgrade -y
Then, install the jdk
by running the following command
sudo apt install openjdk-11-jdk -y
Now check the version of the available Java installation
java -version
Create a directory as /opt/tomcat
sudo mkdir -p /opt/tomcat
Create a tomcat
user for separate user for a tomcat service
sudo useradd tomcat
Change directory to /tmp
directory
cd /tmp
Download tomcat archive using wget
command
wget https://dlcdn.apache.org/tomcat/tomcat-10/v10.0.27/bin/apache-tomcat-10.0.27.tar.gz
Extract the archive file into /opt/tomcat
directory
sudo tar -xvf apache-tomcat-10.0.27.tar.gz -C /opt/tomcat
We can now grant tomcat
ownership over the extracted installation
sudo chown -R tomcat:tomcat /opt/tomcat/
sudo chmod -R u+x /opt/tomcat/bin
Next, open /opt/tomcat/conf/tomcat-users.xml
file for configuring tomcat users
sudo vi /opt/tomcat/conf/tomcat-users.xml
Next, add the following lines before the ending tag /opt/tomcat/conf/tomcat-users.xml
<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="tomcat" roles="manager-gui"/>
Replace password
with your choice, then save and close the file
Here we declare four user roles, manager-gui
, manager-script
, manager-jmx
and manager-status
, which allow access to Manager and Host Manager pages, respectively. You also declare three users, admin
, deployer
and tomcat
with respective roles.
To remove the restriction from Manager page, open its config file
sudo vi /opt/tomcat/webapps/manager/META-INF/context.xml
Comment out the Valve
definition, as shown
...
<Context antiResourceLocking="false" privileged="true" >
<CookieProcessor className="org.apache.tomcat.util.http.Rfc6265CookieProcessor"
sameSiteCookies="strict" />
<!-- <Valve className="org.apache.catalina.valves.RemoteAddrValve"
allow="127\.\d+\.\d+\.\d+|::1|0:0:0:0:0:0:0:1" /> -->
<Manager sessionAttributeValueClassNameFilter="java\.lang\.(?:Boolean|Integer|Long|Number|String)|org\.apache\.catalina\.filters\.Csr>
</Context>
Save and close the file, then repeat for Host Manager:
sudo vi /opt/tomcat/webapps/host-manager/META-INF/context.xml
You have now defined three users
Create a systemd
file, We'll store the tomcat service in a file named tomcat.service
, under /etc/systemd/system
sudo vi /etc/systemd/system/tomcat.service
Add the following lines
[Unit]
Description=Tomcat
After=network.target
[Service]
Type=forking
User=tomcat
Group=tomcat
Environment="JAVA_HOME=/usr/lib/jvm/openjdk-11jdk"
Environment="JAVA_OPTS=-Djava.security.egd=file:///dev/urandom"
Environment="CATALINA_BASE=/opt/tomcat"
Environment="CATALINA_HOME=/opt/tomcat"
Environment="CATALINA_PID=/opt/tomcat/temp/tomcat.pid"
Environment="CATALINA_OPTS=-Xms512M -Xmx1024M -server -XX:+UseParallelGC"
ExecStart=/opt/tomcat/bin/startup.sh
ExecStop=/opt/tomcat/bin/shutdown.sh
RestartSec=10
Restart=always
[Install]
WantedBy=multi-user. Target
Rectify the value of JAVA_HOME
, then save & close
Next, reload the system daemon
to apply changes
sudo systemctl daemon-reload
Start and enable the tomcat.service
sudo systemctl start tomcat
sudo systemctl enable tomcat
Check its status
to confirm that it started successfully:
sudo systemctl status tomcat
Tomcat uses port 8080
, so allow traffic to that port
sudo ufw allow 8080
Now you can access tomcat by navigating to the IP address of your server in the browser
http://your_server_ip:8080
Navigate to the Web Application Manager. Enter the credentials of the admin user that you added to the user’s list.
And you will have access to the page.
Download a sample.war that contains a sample application, using wget.
wget https://tomcat.apache.org/tomcat-10.1-doc/appdev/sample/sample.war
Then move the file to the /opt/tomcat/webapps
directory.
sudo mv sample.war /opt/tomcat/webapps
Now go to https://public_ip:8080/sample
to view the application