The Apache Tomcat 9 is the latest version available for installation. Tomcat is an open-source web server for Java-based applications developed by the Apache Foundation. We use Tomcat for deploying Java Servlet and JSP applications. To know more about the Apache Tomcat visit apache official site http://tomcat.apache.org/.
Prerequisites
A running Ubuntu 20.04 system with shell access of root or sudo privileged account access.
Installing Java
You must have Java installed on your system to run the tomcat server. Tomcat 9 is required to have Java 8 or a higher version installed on your system. Use the following command to install OpenJDK on your system or skip if already installed.
sudo apt install openjdk-11-jdk
Check the current active Java version:
java -version openjdk version "11.0.7" 2020-04-14 OpenJDK Runtime Environment (build 11.0.7+10-post-Ubuntu-3ubuntu1) OpenJDK 64-Bit Server VM (build 11.0.7+10-post-Ubuntu-3ubuntu1, mixed mode, sharing)
Create A Tomcat Account
We recommend creating a separate user account to run the tomcat web server on the Ubuntu system. This will be good for security purposes.
To create account execute following command:
sudo useradd -m -d /opt/tomcat -U -s /bin/false tomcat
The above command will create a user and group with the name “tomcat” on your system.
Download Tomcat Archive
The Apache Tomcat development team releases the latest version of Tomcat from time to time. So it will be good check download latest Tomcat version from the official download server. Use the below command to download Tomcat 9.0.58.
wget https://dlcdn.apache.org/tomcat/tomcat-9/v9.0.58/bin/apache-tomcat-9.0.58.tar.gz
Once the file is downloaded, extracted the archive file and copy all content to the tomcat home directory.
tar xzf apache-tomcat-9.0.58.tar.gz sudo mv apache-tomcat-9.0.58/* /opt/tomcat/
Also, set the proper ownership of all files.
sudo chown -R tomcat:tomcat /opt/tomcat/
You can also download Tomcat 8 instead of Tomcat 9 and change above commands accordingly. Remaining steps will be same for both.
Enable Host/Manager Remote Access
By default Tomcat manager and host-manager, pages are accessible from the localhost system only. To allow access to these pages from the remote system, you need to create the following configuration files.
First create manager xml file:
sudo nano /opt/tomcat/conf/Catalina/localhost/manager.xml
Add the following content
<Context privileged="true" antiResourceLocking="false" docBase="{catalina.home}/webapps/manager"> <Valve className="org.apache.catalina.valves.RemoteAddrValve" allow="^.*$" /> </Context>
Then create host-manager xml file:
vim /opt/tomcat/conf/Catalina/localhost/host-manager.xml
Add the following content
<Context privileged="true" antiResourceLocking="false" docBase="${catalina.home}/webapps/host-manager"> <Valve className="org.apache.catalina.valves.RemoteAddrValve" allow="^.*$" /> </Context>
Save both files and close.
Create Tomcat Access Credentials
Now, configure your tomcat with user accounts to secure access of admin/manager pages. To do this, edit conf/tomcat-users.xml file in your editor and paste the following code inside <tomcat-users> </tomcat-users> tags. We recommend changing the password in the below configuration with high secured password.
sudo nano /opt/tomcat/conf/tomcat-users.xml
Add the following values. Make sure to change the password for admin and manager access.
<!-- user manager can access only manager section --> <role rolename="manager-gui " /> <user username="manager " password="_SECRET_PASSWORD_ " roles="manager-gui " /><!-- user admin can access manager and admin section both --> <role rolename="admin-gui " /> <user username="admin " password="_SECRET_PASSWORD_ " roles="manager-gui,admin-gui " />
Save file and close.
Create A Tomcat Startup Script
Tomcat provides bash scripts to start, stop service. But, to make it simpl, create a startup script to manage Tomcat as systemd service. Let’s create a tomcat.service file with the following content:
sudo nano /etc/systemd/system/tomcat.service
[Unit] Description=Tomcat 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" 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
Reload the systemd daemon service to apply changes
sudo systemctl daemon-reload
Then, enable and start Tomcat service on your system
sudo systemctl enable tomcat sudo systemctl start tomcat
Accessing Tomcat
Tomcat server works on port 8080 default. To access Tomcat on the web browser by connecting your server on port 8080.
If you are connecting from the local machine then use http://localhost or use the IP address for the remote system with port:
http://host.tecadmin.net:8080
http://host.tecadmin.net:8080/manager/html
http://host.tecadmin.net:8080/host-manager/html
Conclusion
You have a running Tomcat server on Ubuntu system. You may need to create a Virtual host or configure a SSL certificate in Tomcat.
1 Comment
Why go this route instead of ‘apt install tomcat9 tomcat9-admin’?
This method fails at the step where one creates the manager.xml: permissions and no catalina dir in the conf dir.
All in this wasn’t especially helpful