How To Install Apache Tomcat 10 on Ubuntu 20.04

 Apache Tomcat is a widely used web server and servlet container that's essential for deploying Java applications. In this tutorial, we will guide you through the installation of Apache Tomcat 10 on an Ubuntu 20.04 server. We'll also set up user authentication, configure access to Tomcat's admin interface, and ensure that your server is ready to serve Java applications.

## Prerequisites

Before we begin, make sure you have the following:

  • A server running Ubuntu 20.04.
  • Root or sudo access to the server.

Step 1: Install Tomcat and JDK

1.1. Create a Separate User for Tomcat:

For security purposes, it's best to run Tomcat under a separate user. Create a user named "tomcat" with the following command:

# useradd -m -d /opt/tomcat -U -s /bin/false tomcat

1.2. Install the Java Development Kit (JDK):

Apache Tomcat requires Java to run. Install the default JDK package with the following command:

# apt update

# apt install default-jdk

Check the installed Java version:

# java -version

Step 2: Download and Configure Tomcat

2.1. Navigate to the `/tmp` directory:

# cd /tmp

2.2. Download and Extract Tomcat:

Download the latest version of Apache Tomcat 10 using `wget`:

# wget https://archive.apache.org/dist/tomcat/tomcat-10/v10.0.11/bin/apache-tomcat-10.0.11.tar.gz

Extract the downloaded archive to the `/opt/tomcat` directory:

# tar xzvf apache-tomcat-10*tar.gz -C /opt/tomcat --strip-components=1

Set the correct permissions for the Tomcat installation:

# chown -R tomcat:tomcat /opt/tomcat/

# chmod -R u+x /opt/tomcat/bin

Step 3: Configure Admin Users

3.1. Open the `tomcat-users.xml` file for editing:

# vi /opt/tomcat/conf/tomcat-users.xml

3.2. Add the following lines before the closing `</tomcat-users>` tag:

<role rolename="manager-gui" />
<user username="manager" password="redhat" roles="manager-gui" />
<role rolename="admin-gui" />
<user username="admin" password="redhat" roles="manager-gui,admin-gui" />

3.3. Save and close the file.

Step 4: Remove IP Restrictions for Admin Pages

4.1. Remove IP restrictions for the Manager page:

# vi /opt/tomcat/webapps/manager/META-INF/context.xml

Comment out the Valve definition, like this:

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

4.2. Save and close the file.

Repeat the same process for the Host Manager:

#vi  /opt/tomcat/webapps/host-manager/META-INF/context.xml

Comment out the Valve definition, like this:

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

4.3. Save and close the file.

Step 5: Create a systemd Service

5.1. Determine the Java path:

# update-java-alternatives -l

Note the path where Java is located.

5.2. Create a systemd service file for Tomcat:

# vi /etc/systemd/system/tomcat.service

Add the following content, modifying the `JAVA_HOME` path if necessary:

[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
RestartSec=10
Restart=always

[Install]
WantedBy=multi-user.target

5.3. Save and close the file.

5.4. Reload the systemd daemon:

# systemctl daemon-reload

5.5. Start Tomcat and enable it to start on boot:

# systemctl start tomcat

# systemctl enable tomcat

Step 6: Configure Firewall

Allow traffic on port 8080 (Tomcat's default port) through the firewall:

# ufw allow 8080

Step 7: Access the Web Interface

A. You can now access the Tomcat web interface using your server's IP address:

http://192.168.2.202:8080




B. You'll see the default Tomcat welcome page. 
You’ve now verified that the Tomcat service is working.

C. Press on the Server Status button on the right. You’ll be prompted to enter the account credentials that you defined in a previous step.
You should see a page that looks like this:


D. Press on the Host Manager button on the right. You’ll be prompted to enter the account credentials that you defined in a previous step.
You should see a page that looks like this:



E. Press on the Manager App button on the right. You’ll be prompted to enter the account credentials that you defined in a previous step.
You should see a page that looks like this:



Tomcat - Web Application Manager  The Web Application Manager is used to manage your Java applications. You can start, stop, reload, deploy, and undeploy them from here. You can also run some diagnostics on your apps (for example, to find memory leaks). Information about your server is available at the very bottom of this page.

Step 8. Deploy a sample war file via Web Interface
 . 

A. Download the war (https://tomcat.apache.org/tomcat-7.0-doc/appdev/sample/sample.war) on you desktop.  

B. Go to the "Manager App" on the Tomcat web interface.

C. In the "WAR file to deploy" section, click "Choose File" to upload the sample.war file from your desktop.

D. Click the "Deploy" button.

E. 
After deploying, you will find the "sample" application listed. Click on it to open the web page.

Step 9: Deploy a Sample WAR File via Command Line

A. Download the sample WAR file using the wget command: 

# wget https://tomcat.apache.org/tomcat-7.0-doc/appdev/sample/sample.war

B. Copy the downloaded WAR file to the Tomcat webapps directory:
 
# cp sample.war /opt/tomcat/webapp/

C. Reload the Tomcat web page.






These steps provide instructions for deploying the sample WAR file both through the web interface and via the command line. This allows you to test your Tomcat installation by running a sample web application.

Congratulations! You've successfully installed and configured Apache Tomcat 10 on your Ubuntu 20.04 server. You can now use it to deploy and manage your Java applications.

No comments:

Post a Comment