How to Install Apache Tomcat on Ubuntu

Apache Tomcat is a popular open-source application server that is used to deploy and run Java-based web applications. In this tutorial, we will guide you through the process of installing Apache Tomcat on an Ubuntu-based server. We will also cover the steps to configure Tomcat as a systemd service, allowing you to manage it more easily.


Prerequisites


Before you begin, make sure you have the following:


- An Ubuntu-based server.

- Administrative (sudo) access to the server.

- Java Runtime Environment (JRE) installed. We will use OpenJDK in this tutorial.


Step 1: Update and Upgrade


First, ensure your system is up to date by running the following commands:



sudo apt-get update

sudo apt-get upgrade



This will update the package list and upgrade any existing packages on your system.


Step 2: Install OpenJDK


Apache Tomcat requires a Java Runtime Environment (JRE) to run. You can install OpenJDK, which is an open-source implementation of the Java Platform, Standard Edition (Java SE), with the following command:



sudo apt install openjdk-11-jre-headless



This command installs OpenJDK 11, which is the recommended version for Apache Tomcat 11.


Step 3: Create a Tomcat User and Group


For security reasons, it is not advisable to run Tomcat as the root user. Instead, we will create a dedicated system user and group for running Tomcat.



sudo groupadd tomcat

sudo useradd -s /bin/false -g tomcat -d /opt/tomcat tomcat



Here's what these commands do:


- `groupadd tomcat`: Creates a group named "tomcat."

- `useradd -s /bin/false -g tomcat -d /opt/tomcat tomcat`: Creates a user named "tomcat" with `/bin/false` as the login shell (meaning it cannot log in interactively) and sets its home directory to `/opt/tomcat`.


Step 4: Download and Install Apache Tomcat


Navigate to the `/tmp` directory and download Apache Tomcat using `curl`:



cd /tmp

curl -O https://dlcdn.apache.org/tomcat/tomcat-11/v11.0.0-M11/bin/apache-tomcat-11.0.0-M11.tar.gz



Next, create the `/opt/tomcat` directory and extract the downloaded Tomcat archive while stripping the top-level directory:



sudo mkdir /opt/tomcat

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



Finally, remove the downloaded archive to save disk space:



sudo rm -rf /tmp/apache-tomcat-*tar.gz



Change the ownership of the Tomcat directory to the "tomcat" user and group:



sudo chown -RH tomcat: /opt/tomcat



Make the Tomcat startup and shutdown scripts executable:



sudo sh -c 'chmod +x /opt/tomcat/bin/*.sh'



Step 5: Set Up Environment Variables


To make it easier to manage Tomcat, set up environment variables that define the Tomcat home directory (`CATALINA_HOME`) and add the Tomcat binary directory to your system's `PATH`:



export CATALINA_HOME=/opt/tomcat

export PATH=$PATH:$CATALINA_HOME/bin



You can add these lines to your shell profile or system-wide environment configuration to ensure they are set every time you log in.


Step 6: Start and Stop Tomcat


You can now start Apache Tomcat using the following command:



/opt/tomcat/bin/startup.sh



To stop Tomcat, use the following command:



/opt/tomcat/bin/shutdown.sh



You should see Tomcat starting up and shutting down messages in the terminal.


Step 7: Allow Port 8080 through the Firewall


Apache Tomcat runs by default on port 8080. To access the Tomcat web interface, you need to allow incoming traffic on this port through the firewall:



sudo ufw allow 8080/tcp



Step 8: Check Tomcat Version


To check the Apache Tomcat version, you can use the following commands:



jar -xf /opt/tomcat/lib/catalina.jar org/apache/catalina/util/ServerInfo.properties

cat org/apache/catalina/util/ServerInfo.properties



The `cat` command will display the Tomcat version information contained in the `ServerInfo.properties` file.


Step 9: Create a Systemd Unit File (Optional)


If you want to run Tomcat as a systemd service, you can create a systemd unit file for it. Here are the steps to create and configure the file:


1. Find the JAVA_HOME path by running:


sudo update-java-alternatives -l



Choose the Java version you want to use and note down its location.


2. Create the Tomcat systemd unit file:



sudo nano /etc/systemd/system/tomcat.service



3. Add the following content to the `tomcat.service` file, replacing `"JAVA_HOME"` with the path you found in step 1:


[Unit]

Description=Apache Tomcat Web Application Container

After=network.target


[Service]

Type=forking

User=tomcat

Group=tomcat

Environment="JAVA_HOME=/usr/lib/jvm/java-11-openjdk-amd64"

Environment="JAVA_OPTS=-Djava.security.egd=file:///dev/urandom -Djava.awt.headless=true"

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



4. Save and exit the file (Ctrl+X, followed by Y for Yes, and then Enter).


5. Reload the systemd daemon to apply the changes:



sudo systemctl daemon-reload



6. Start the Tomcat service:



sudo systemctl start tomcat



7. Check the status to verify that Tomcat is running:



sudo systemctl status tomcat



Step 10: Configure Web Management Interface


To configure the web management interface, you'll need to edit the `tomcat-users.xml` file:



sudo nano /opt/tomcat/conf/tomcat-users.xml



Add the following user definition within the `<tomcat-users>` section, replacing `"Your_Password"` with a strong password of your choice:


xml

<user username="admin" password="Your_Password" roles="manager-gui, manager-script, manager-jmx, manager-status, admin-gui, admin-script"/>



Save and exit the file.


Step 11: Configure Remote Access (Optional)


By default, Tomcat is configured to allow access only from the local machine. If you want to access Tomcat remotely, you'll need to configure it to allow connections from your IP address.


Open the `context.xml` file:



sudo nano /opt/tomcat/webapps/manager/META-INF/context.xml



Add your IP address to the `<Valve>` element as shown below:


xml

<Context antiResourceLocking="false" privileged="true">

    <!--

    <Valve className="org.apache.catalina.valves.RemoteAddrValve"

           allow="127\.\d+\.\d+.d+|::1|0000:1|YOUR_IP_ADDRESS" />

    -->

</Context>



Repeat the same process for the `context.xml` file in the `host-manager` directory:



sudo nano /opt/tomcat/webapps/host-manager/META-INF/context.xml



Restart Tomcat for the changes to take effect:



sudo systemctl restart tomcat



Step 12: Access Tomcat Web Interface


You can now access the Tomcat web interface by opening a web browser and navigating to:



http://your_server_ip:8080/



Replace `your_server_ip` with your server's IP address. You should see the Tomcat splash page, indicating that Tomcat is up and running.


Congratulations! You have successfully installed and configured Apache Tomcat on your Ubuntu server. You can now deploy and manage your Java web applications with ease.