# Installing Apache Tomcat on Ubuntu 22.04

Apache Tomcat is an open-source web server developed by the Apache Software Foundation. Tomcat is one of the most popular choices for running Java web applications and is widely used in the industry.

**<mark>Step 1: Install Java Development Kit (JDK)</mark>**

Update system's package repository

```plaintext
sudo apt update
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1723016959812/6cd75fb0-5fa9-40bc-9823-312e5ec93137.png align="center")

Tomcat requires Java to run. Install the default Java Development Kit (JDK)

```plaintext
sudo apt install default-jdk
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1723017003314/13f416e7-4d39-4e93-b35d-5755487271a4.png align="center")

**<mark>Step 2: Download Apache Tomcat</mark>**

Navigate to the `/opt/` directory where we will install Apache Tomcat:

```plaintext
cd /opt/
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1723017049890/23cd6232-e076-4364-a04e-db010ab7f1ae.png align="center")

Next, download the Apache Tomcat archive using `wget`.

```plaintext
wget https://dlcdn.apache.org/tomcat/tomcat-9/v9.0.93/bin/apache-tomcat-9.0.93.tar.gz
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1723017086382/3b8a12a4-1d8a-45dd-ab48-ca57c7fa7f0a.png align="center")

**<mark>Step 3 : Extract Apache Tomcat</mark>**

Once the download is complete, extract the archive using the `tar` command with the `-xzvf` option:

```plaintext
tar -xzvf apache-tomcat-9.0.93.tar.gz
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1723017121938/23aa553d-c871-4a16-bfe0-c4f3dde13782.png align="center")

**<mark>Step 4 : Move Tomcat to Its Final Location</mark>**

Move the extracted Apache Tomcat directory to `/opt/tomcat/` for better organization:

```plaintext
mv apache-tomcat-9.0.93 /opt/tomcat/
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1723017180512/c53c44f5-3fea-4708-97e8-1634e4711cc5.png align="center")

**<mark>Step 5 : Create a Tomcat User</mark>**

For security purposes, create a dedicated user for running Tomcat:

```plaintext
adduser tomcat
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1723017245600/07a5e198-18b9-40cb-acc5-54b4a8692adb.png align="center")

**<mark>Step 6 : Set Permissions and Environment Variables</mark>**

Set the ownership of the Tomcat directory to the newly created user and group:

```plaintext
chown -R tomcat:tomcat /opt/tomcat/
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1723017293107/8dbcd0ac-ed7b-41f9-a284-db8b9f5cbdb1.png align="center")

Make the Tomcat startup scripts executable:

```plaintext
chmod -R u+x /opt/tomcat/bin/
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1723017332852/3ee57f6a-e7ac-4fa3-9ed7-bcd681a57967.png align="center")

**<mark>Step 7 : Create a Systemd Service File</mark>**

To manage Tomcat as a service, create a systemd service file:

```plaintext
vi /etc/systemd/system/tomcat.service
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1723017399212/73860e23-5b11-4ac6-816c-f8edeffec993.png align="center")

Copy and paste the following configuration into the file:

```plaintext
[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
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1723017369025/0a607b53-f3a1-4f0c-acba-36c7d8a09f47.png align="center")

**<mark>Step 8 : Start Tomcat</mark>**

Start the Tomcat service:

```plaintext
systemctl start tomcat
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1723017429394/c13b43b4-f701-454a-adfc-83856fd32840.png align="center")

Check the status

```plaintext
systemctl status tomcat
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1723017468494/56359ad9-4d29-4fd3-9d0c-d06764a955dc.png align="center")

**<mark>Step 9 : Configure Tomcat Users</mark>**

To manage user access to the Tomcat Manager and Host Manager applications, you need to configure user roles and permissions in the `tomcat-users.xml` file. This file is located in the `conf` directory within your Tomcat installation directory.

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

```plaintext
vi /opt/tomcat/conf/tomcat-users.xml
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1723017633231/e176ae23-afe5-48d5-9878-c81efb983e13.png align="center")

Add the following sample configuration to create a user with manager and admin roles. Replace `your_username` and `your_password` with your desired credentials:

```plaintext
-->
    <role rolename="admin-gui"/>
    <role rolename="manager-gui"/>
    <role rolename="manager-script"/>
    <user username="your_username" password="your_password" roles="admin-gui,manager-gui,manager-script"/>
</tomcat-users>
```

This configuration grants the user the roles required to access the Tomcat Manager and Host Manager applications.

Save and exit the text editor.

Optionally, you can configure additional users and roles as needed by adding more `<user>` elements within the `<tomcat-users>` section.

Now, your Tomcat Manager and Host Manager applications are configured with the specified user credentials and roles. You can use these credentials to access and manage your web applications through the Tomcat Manager web interface.

Remember to keep your credentials secure and choose strong passwords to ensure the security of your Tomcat server.

**<mark>Step 10 : Comment Out Valve Tags</mark>**

To enhance the security of the Tomcat Manager and Host Manager applications, it's a good practice to comment out the Valve tags in their respective `context.xml` files. This helps prevent unauthorized access.

**In Manager:**

Open the `context.xml` file for the Tomcat Manager application:

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

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1723017772873/c78a56cd-f431-42bd-96e5-85a3e7710459.png align="center")

Locate the Valve tag and comment it out by adding `<!--` before the opening tag and `-->` after the closing tag. Here's a sample:

```plaintext
<!-- Comment out the Valve tag for enhanced security -->
<!--
<Valve className="org.apache.catalina.valves.RemoteAddrValve"
       allow="127\.\d+\.\d+\.\d+|::1|0:0:0:0:0:0:0:1|0\.0\.0\.0|0:0:0:0:0:0:0:0"
       deny=""/>
-->
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1723017730807/5aa1dacc-590e-4f76-96da-0a24886779a4.png align="center")

Save and exit the text editor.

**<mark>In Host Manager:</mark>**

Open the `context.xml` file for the Host Manager application:

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

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1723017941495/5fece88c-2b98-43d1-bcdc-7627e8599fc0.png align="center")

Locate the Valve tag and comment it out using after the closing tag. Here's a sample:

```plaintext
<!-- Comment out the Valve tag for enhanced security -->
<!--
<Valve className="org.apache.catalina.valves.RemoteAddrValve"
       allow="127\.\d+\.\d+\.\d+|::1|0:0:0:0:0:0:0:1|0\.0\.0\.0|0:0:0:0:0:0:0:0"
       deny=""/>
-->
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1723017914659/6a4794e0-61d2-4e87-b4b4-f8d4e375d770.png align="center")

Save and exit the text editor.

By commenting out the Valve tags as shown in the samples, you restrict access to the Tomcat Manager and Host Manager applications to specific IP addresses. This added layer of security helps prevent unauthorized access and enhances the overall security of your Tomcat server.

**<mark>Step 11 : Reload systemd and Check Status</mark>**

After making these changes, reload systemd for the changes to take effect:

```plaintext
systemctl daemon-reload
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1723017972558/29d0b758-b65e-4364-ac3b-a9bc23821759.png align="center")

Check the status of the Tomcat service again:

```plaintext
systemctl status tomcat.service
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1723018005464/0deb4372-84c3-4709-b8c0-127af58c24bf.png align="center")

Now, Successfully installed and configured Apache Tomcat on Ubuntu system.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1723019173609/d2d10289-c2c2-43e1-ad80-6ef416733da2.png align="center")

You can now deploy your Java web applications and enjoy the benefits of this powerful application server.
