Share your love
Install and Configure Apache Tomcat on Ubuntu 22.04
In this guide, we intend to teach you to Install and Configure Apache Tomcat on Ubuntu 22.04.
Apache Tomcat is a web container. It allows the users to run Servlet and JAVA Server Pages that are based on web applications. It can be used as an HTTP server. The performance of the Tomcat server is not as good as the designated web server. It can be used as a separate product with its own internal Web-server.
It can also be used mutually with the others Web-servers which include Apache, Microsoft Internet Information Server, and Microsoft Personal Web-server.
Install and Configure Apache Tomcat on Ubuntu 22.04
To install Tomcat on Ubuntu 22.04, you must log in to your server as a non-root user with sudo privileges and set up a basic firewall. To do this, you can follow our guide the Initial Server Setup with Ubuntu 22.04.
Follow the steps below to complete this guide.
Install Java on Ubuntu 22.04
Tomcat is a Java servlet container so you must have Java installed on your server.
First, update your local package index with the following command:
sudo apt update
Then, use the following command to install OpenJDK on your server:
sudo apt install default-jdk -y
Verify your Java installation with the following command:
java -version
It is recommended to run the Tomcat service as a non-root user. To create a new user run the following command:
sudo useradd -m -d /opt/tomcat -U -s /bin/false tomcat
This command will create a user and group named tomcat on your system.
Download Tomcat on Ubuntu 22.04
at this point, you need to visit the Tomcat Downloads page and copy the tar.gz file link address of the latest release.
Now you can use the wget command to download the Tomcat 10 on Ubuntu 22.04:
wget https://dlcdn.apache.org/tomcat/tomcat-10/v10.0.13/bin/apache-tomcat-10.0.13.tar.gz
Remember to replace the latest version of Tomcat in the above command.
When your download is completed, extract the file in the /opt/tomcat home directory with the following command:
sudo tar xzvf apache-tomcat-10*tar.gz -C /opt/tomcat --strip-components=1
Now you need to set the correct permissions for Apache Tomcat on Ubuntu 22.04 with the following commands:
sudo chown -R tomcat:tomcat /opt/tomcat/
$sudo chmod -R u+x /opt/tomcat/bin
Now you have the latest Tomcat on your server.
Configure Tomcat on Ubuntu 22.04
At this point, you need to configure Tomcat with user accounts to secure access to admin/manager pages.
You need to edit the Tomcat user configuration file. Open the file with your favorite text editor, here we use vi:
sudo vi /opt/tomcat/conf/tomcat-users.xml
Now you need to add the following content to the <tomcat-users>. Just remember to replace the password with a strong and secure password for your manager and admin.
</tomcat-users> <!-- user manager can access only manager section --> <role rolename="manager-gui" /> <user username="manager" password="_SECRET_PASSWORD_" roles="manager-gui" /> <!-- user admin can access manager and admin section both --> <role rolename="admin-gui" /> <user username="admin" password="_SECRET_PASSWORD_" roles="manager-gui,admin-gui" />
When you are done, save and close the file.
Note: The default Tomcat manager and host-manager applications are accessible for localhost only.
To allow access to these pages from the remote system, you need to modify the following configuration files.
For the manager open the file with your favorite text editor, here we use vi:
sudo vi /opt/tomcat/webapps/manager/META-INF/context.xml
Here you need to comment on the section added for IP address restriction to allow connections from anywhere as shown below:
<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" />-->
...
When you are done, save and close the file.
Do the same thing for the host:
sudo vi /opt/tomcat/webapps/host-manager/META-INF/context.xml
Comment the same section as the manager:
<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" /> --> ...
When you are done, save and close the file.
Here you need to create a Tomcat systemd unit file. Open and create the file with your favorite text editor, here we use vi:
sudo vi /etc/systemd/system/tomcat.service
Add the following content to the file:
[Unit] Description=Tomcat After=network.target [Service] Type=forking User=tomcat Group=tomcat Environment="JAVA_HOME=/usr/lib/jvm/java-1.11.0-openjdk-amd64" 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 [Install] WantedBy=multi-user.target
When you are done, save and close the file.
Reload the systemd service to apply the new Tomcat systemd unit file:
sudo systemctl daemon-reload
Now start the Tomcat on Ubuntu 22.04 with the following command:
sudo systemctl start tomcat.service
Then, use the flowing command to start the service every time at boot:
sudo systemctl enable tomcat.service
You can verify that your Tomcat is up and running on Ubuntu 22.04 with the following command:
sudo systemctl status tomcat.service
In your output you will see:
Output tomcat.service - Tomcat Loaded: loaded (/etc/systemd/system/tomcat.service; enabled; vendor preset> Active: active (running) Main PID: 4907 (java) Tasks: 21 (limit: 2282) Memory: 207.1M CGroup: /system.slice/tomcat.service └─4907 /usr/lib/jvm/java-1.11.0-openjdk-amd64/bin/java -Djava.util>
Now you can access the Apache Tomcat web interface.
How to Access the Apache Tomcat Web Interface
At this point, you can access your Tomcat web interface by typing your Server’s IP address in your web browser followed by 8080:
http://server-ip-address:8080
You will see:
If you see this page means that you have successfully installed Tomcat on your Ubuntu 22.04.
As you can see in the web interface, you have Manager App and Host Manager.
The Manager interface provides you with the basic functionality you need to manage your deployed web applications.
Tomcat Host Manager App is used to create/remove Virtual Hosts from the Tomcat service.
Conclusion
At this point, you learn to Install and Configure Apache Tomcat on Ubuntu 22.04.
Hope you enjoy it.
You may be interested in these articles: