Apache Tomcat is an open-source Java servlet container that is used to run Java-based web applications. It is a popular choice for developing and deploying web applications due to its robustness and wide array of features.
In this article, we will show you how to install and configure Apache Tomcat 9 on Debian 10 (also known as “Buster”). We will be using the command line for this tutorial, so you will need to have a basic understanding of how to use the Linux command line.
Before we begin, there are a few prerequisites that you will need to have in place in order to install and configure Apache Tomcat on your system:
- A server running Debian 10
- A user account with sudo privileges
- Java installed on your system
To install Java on your system, you can use the following command:
sudo apt install default-jdk
Once you have these prerequisites in place, you can proceed with the tutorial.
- Download Apache Tomcat
To download Apache Tomcat, you will need to visit the Apache Tomcat website and download the latest version of the software. At the time of writing, the latest stable version of Apache Tomcat is 9.0.39.
To download Apache Tomcat, visit the following URL:
On the download page, select the “Core” tar.gz
package under the “9.0.39” heading. This will download a compressed tarball containing the Apache Tomcat software.
Once the download is complete, move the tarball to the directory where you want to install Apache Tomcat. For this tutorial, we will be using the /opt
directory, which is a common location for installing third-party software.
- Extract the Apache Tomcat tarball
To extract the Apache Tomcat tarball, run the following command:
tar -xvzf apache-tomcat-9.0.39.tar.gz -C /opt
This will extract the contents of the tarball into the /opt directory.
- Create a user for Apache Tomcat
For security reasons, it is best practice to run Apache Tomcat as a non-root user. To do this, we will create a new user called “tomcat
” and assign it ownership of the Apache Tomcat files.
To create the “tomcat
” user, run the following command:
sudo useradd -r -m -U -d /opt/apache-tomcat-9.0.39 -s /bin/false tomcat
This will create a new user called “tomcat” and set its home directory to the Apache Tomcat installation directory.
To give the “tomcat” user ownership of the Apache Tomcat files, run the following command:
sudo chown -R tomcat: /opt/apache-tomcat-9.0.39
This will recursively change the owner of the Apache Tomcat files to the “tomcat” user.
- Create a
systemd
service file for Apache Tomcat
To manage the Apache Tomcat service, we will create a systemd service file. This will allow us to easily start, stop, and restart the Apache Tomcat service using the systemctl
command.
To create the systemd
service file for Apache Tomcat, run the following command:
sudo nano /etc/systemd/system/tomcat.service
This will open the nano text editor, allowing you to create the service file.
Add the following lines to the file, replacing the values in brackets with the appropriate values for your system:
[Unit]
Description=Apache Tomcat Web Application Container
After=syslog.target network.target
[Service]
Type=forking
User=tomcat
Group=tomcat
Environment="JAVA_HOME=<path-to-java-installation>"
Environment="JAVA_OPTS=-Djava.security.egd=file:///dev/urandom"
Environment="CATALINA_BASE=/opt/apache-tomcat-9.0.39"
Environment="CATALINA_HOME=/opt/apache-tomcat-9.0.39"
Environment="CATALINA_PID=/opt/apache-tomcat-9.0.39/temp/tomcat.pid"
Environment="CATALINA_OPTS=-Xms512M -Xmx1024M -server -XX:+UseParallelGC"
ExecStart=/opt/apache-tomcat-9.0.39/bin/startup.sh
ExecStop=/opt/apache-tomcat-9.0.39/bin/shutdown.sh
[Install]
WantedBy=multi-user.target
Save the file and exit the editor.
To enable the Apache Tomcat service and start it, run the following commands:
sudo systemctl enable tomcat
sudo systemctl start tomcat
This will enable the Apache Tomcat service and start it, allowing you to access the Apache Tomcat web application server.
To verify that the Apache Tomcat service is running, you can use the following command:
sudo systemctl status tomcat
This will display the status of the Apache Tomcat service, including whether it is active and running.
To configure the Tomcat web management interface, you will need to edit the tomcat-users.xml
file, which is located in the conf directory of your Tomcat installation.
To edit the tomcat-users.xml file, run the following command:
sudo nano /opt/apache-tomcat-9.0.39/conf/tomcat-users.xml
This will open the tomcat-users.xml file in the nano text editor.
To enable the web management interface, you will need to add a user with the appropriate permissions. To do this, add the following lines to the file, replacing the values in brackets with the appropriate values:
<role rolename="manager-gui"/>
<user username="<username>" password="<password>" roles="manager-gui"/>
Save the file and exit the editor.
To apply the changes, you will need to restart the Apache Tomcat service. To do this, run the following command:
sudo systemctl restart tomcat
This will restart the Apache Tomcat service and apply the changes that you made to the tomcat-users.xml
file.
To access the web management interface, open a web browser and navigate to the following URL:
http://<server-ip-address>:8080/manager/html
Replace <server-ip-address>
with the IP address of your server.
You will be prompted to enter the username and password that you specified in the tomcat-users.xml file. Enter the credentials and log in to the web management interface.
From the web management interface, you can manage and configure various aspects of the Apache Tomcat server, including deploying and undeploying web applications, managing users and roles, and monitoring the server’s performance.
Conclusion
In this article, we showed you how to install and configure Apache Tomcat 9 on Debian 10. We covered all of the necessary steps, including downloading and extracting the Apache Tomcat software, creating a user for Apache Tomcat, creating a systemd service file, and starting the Apache Tomcat service. With Apache Tomcat installed and configured on your system, you can now use it to run Java-based web applications.