How to Install Apache Tomcat on Linux
Introduction
Apache Tomcat is a widely used open-source web server and servlet container that allows Java-based web applications to run. In this guide, we will walk through the steps to install and configure Apache Tomcat on a Linux system.
Prerequisites
Before installing Apache Tomcat, ensure that you have the following prerequisites:
- Java Development Kit (JDK)
Apache Tomcat requires Java to run. If you don’t have the JDK installed, follow this guide. - Administrative Privileges
Ensure you have administrative rights on the Linux machine to install software and configure services.
Step 1: Download and Install Apache Tomcat
- Download Tomcat:
- Visit the Apache Tomcat download page.
- Download the
.tar.gz
file for the Tomcat version you want to install.
- Extract Tomcat:
- Open Terminal and navigate to the directory where you downloaded the
.tar.gz
file. - Extract the file with:
1
tar -xvzf apache-tomcat-<version>.tar.gz
- Move the extracted Tomcat directory to a preferred location (e.g.,
/opt
):1
sudo mv apache-tomcat-<version> /opt
- Open Terminal and navigate to the directory where you downloaded the
Step 2: Configure Environment Variables for Tomcat
Setting environment variables simplifies the process of starting and managing Tomcat.
You have to add the CATALINA_HOME
environment variable to specify the Tomcat installation directory,
which is essential for its proper functioning.
You can set this variable by creating or editing a shell script in the /etc/profile.d/
directory:
1
sudo nano /etc/profile.d/tomcat.sh
Now, paste the following line:
1
export CATALINA_HOME=/opt/apache-tomcat-<version>
Save and close the file (Ctrl + X to save, then Y to confirm, and press Enter to exit). Then, reload the profile by executing the command:
1
source /etc/profile.d/tomcat.sh
Step 3: Start Apache Tomcat
To start Tomcat, navigate to the bin
directory and execute the startup script:
1
2
cd /opt/apache-tomcat-<version>/bin
./startup.sh
You should see a message indicating that Tomcat has started.
Step 4: Access Apache Tomcat
To verify that Tomcat is running, open your web browser and go to:
1
<http://localhost:8080>
If everything is set up correctly, you should see the Tomcat welcome page.
Step 5: Creating a Systemd Service for Tomcat (Optional)
To ensure that Tomcat runs as a service and starts automatically after system reboots,
you can create a systemd
service file.
- Create the service file:
1
sudo nano /etc/systemd/system/tomcat.service
- Add the following configuration, making sure to adjust the JAVA_HOME path to point to your own JDK installation:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
[Unit] Description=Apache Tomcat Web Application Container After=network.target [Service] Type=forking Environment=JAVA_HOME=/usr/lib/jvm/java-11-openjdk-amd64 # Update this path to your JDK installation Environment=CATALINA_PID=/opt/tomcat/temp/tomcat.pid Environment=CATALINA_HOME=/opt/tomcat Environment=CATALINA_BASE=/opt/tomcat Environment='CATALINA_OPTS=-Xms512M -Xmx1024M -server -XX:+UseParallelGC' Environment='JAVA_OPTS=-Djava.awt.headless=true -Djava.security.egd=file:/dev/./urandom' ExecStart=/opt/tomcat/bin/startup.sh ExecStop=/opt/tomcat/bin/shutdown.sh User=tomcat Group=tomcat UMask=0007 RestartSec=10 Restart=always [Install] WantedBy=multi-user.target
Save and close the file (Ctrl + X to save, then Y to confirm, and press Enter to exit).
- Reload
systemd
to register the new service:1
sudo systemctl daemon-reload
- Enable and start the Tomcat service:
1 2
sudo systemctl enable tomcat sudo systemctl start tomcat
- You can check the status of Tomcat with:
1
sudo systemctl status tomcat
Step 6: Secure Tomcat (Optional)
By default, Tomcat’s Manager and Host Manager applications are not secure. To secure them, you need to create a user with the appropriate roles.
- Open the
tomcat-users.xml
file:1
sudo nano /opt/tomcat/conf/tomcat-users.xml
- Add the following lines inside the
<tomcat-users>
tags:1 2 3
<role rolename="manager-gui"/> <role rolename="admin-gui"/> <user username="admin" password="password" roles="manager-gui,admin-gui"/>
Replace
"admin"
and"password"
with your desired username and password. - Restart Tomcat to apply the changes:
1
sudo systemctl restart tomcat
- You can now access the Tomcat Manager and Host Manager via:
1
<http://localhost:8080/manager/html>
Log in with the credentials you set in the
tomcat-users.xml
file.
Conclusion
By following these steps, you should have Apache Tomcat successfully installed and running on your Linux machine. Tomcat is now ready to serve your Java web applications. If you want Tomcat to be more secure or optimized, you can explore additional configuration options, including SSL setup and performance tuning.