How to Install Apache Tomcat Server on Ubuntu 20.04 (Tutorial)

How to Install Apache Tomcat Server on Ubuntu 20.04. Apache Tomcat is a free and open source application used to run Java based applications on the web. It is developed by the Apache Foundation and maintained by an open community of developers. It allows developers to write dynamic web content based on Java. Tomcat allows the webserver to handle Java-based dynamic web content. Tomcat also supports other Java server technologies such as JavaServer Pages (JSP), Java Expression Language, and Java WebSocket.

Tomcat is very popular and boasts a market share of over 60%. Apache Tomcat is the best solution for you if you are looking to host applications that process thousands of requests.

In this post, we will show you how to install Apache Tomcat with Nginx as a reverse proxy on Ubuntu 20.04.

Install Apache Tomcat Server on Ubuntu 20.04

Install Java JDK

Tomcat is a Java based application. So the default Java Development Kit (JDK) package must be installed on your server. If not installed, you can install it using the following command:

				
					apt-get install default-jdk -y
				
			

Once the Java is installed, verify the Java version using the following command:

				
					java --version
				
			

You should see the Java version in the following output:

				
					openjdk 11.0.11 2021-04-20
OpenJDK Runtime Environment (build 11.0.11+9-Ubuntu-0ubuntu2.20.04)
OpenJDK 64-Bit Server VM (build 11.0.11+9-Ubuntu-0ubuntu2.20.04, mixed mode, sharing)

				
			

Download Apache Tomcat

Our next step of the guide how to Install Apache Tomcat Server on Ubuntu 20.04 we will need to Install Tomcat.  At the time of writing this tutorial, Tomcat 10 is the latest version available on the internet. You can download it directly from the Tomcat download page or use the following command to download it on the terminal:

				
					wget https://dlcdn.apache.org/tomcat/tomcat-10/v10.0.13/bin/apache-tomcat-10.0.13.tar.gz
				
			

Once the Apache Tomcat is downloaded, create a directory for Tomcat and extract the downloaded file to that directory:

				
					mkdir /opt/tomcat
tar xzvf apache-tomcat-10.0.13.tar.gz -C /opt/tomcat --strip-components=1
				
			

Next, create a dedicated user and group to run the Tomcat server.

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

Next, change the ownership of the /opt/tomcat directory to tomcat and set the execution permission to all files and directories.

				
					chown -R tomcat: /opt/tomcat
chmod -R 755 /opt/tomcat
				
			

Once you are finished, you can proceed to create a systemd service file for Apache Tomcat.

Create a Systemd Service File for Apache Tomcat

Instead of starting and stopping the Apache Tomcat service manually. It is a good idea to create a systemd service file to manage the Apache Tomcat service.

You can create it using the following command:

				
					nano /etc/systemd/system/tomcat.service
				
			

Add the following configurations:

				
					[Unit]
Description=Tomcat webs servlet container
After=network.target

[Service]
Type=forking

User=tomcat
Group=tomcat
RestartSec=10
Restart=always
Environment="JAVA_HOME=/usr/lib/jvm/java-1.11.0-openjdk-amd64"
Environment="JAVA_OPTS=-Djava.awt.headless=true -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

				
			

Save the file after you finish then reload the systemd daemon to apply the configuration changes:

				
					systemctl daemon-reload
				
			

Next, start the Tomcat service and enable it to start at system reboot:

				
					systemctl start tomcat
systemctl enable tomcat
				
			

You can check the status of the Tomcat service using the following command:

				
					systemctl status tomcat
				
			

You should see the following output:

				
					● tomcat.service - Tomcat webs servlet container
Loaded: loaded (/etc/systemd/system/tomcat.service; disabled; vendor preset: enabled)
Active: active (running) since Thu 2021-12-02 08:46:33 UTC; 8s ago
Process: 4395 ExecStart=/opt/tomcat/bin/startup.sh (code=exited, status=0/SUCCESS)
Main PID: 4402 (java)
Tasks: 19 (limit: 2353)
Memory: 116.1M
CGroup: /system.slice/tomcat.service
└─4402 /usr/lib/jvm/java-1.11.0-openjdk-amd64/bin/java -Djava.util.logging.config.file=/opt/tomcat/conf/logging.properties -Djav>

Dec 02 08:46:33 server systemd[1]: Starting Tomcat webs servlet container...
Dec 02 08:46:33 server startup.sh[4395]: Tomcat started.
Dec 02 08:46:33 server systemd[1]: Started Tomcat webs servlet container.

				
			

At this point, Tomcat is started and listens on port 8080. You can check it using the following command:

				
					ss -antpl | grep 8080
				
			

You should see the Tomcat listening port in the following output:

				
					LISTEN 0 100 *:8080 *:* users:(("java",pid=4402,fd=43))
				
			

Create Tomcat Admin User and Password

For security reasons, it is recommended to create an admin user and password to access the Tomcat application. You can create it by editing tomcat-users.xml file.

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

Add the following line before the </tomcat-users> tag:

				
					<role rolename="manager-gui"/>
<user username="tomcatadmin" password="password" roles="manager-gui,admin-gui"/>
				
			

Note: Replace the username and password value with your preferred value.

Save and close the file when you are done.

Enable Tomcat Remote Access

By default, the Tomcat manager app and the host-manager app can be accessed only from the localhost. If you want to enable remote access for both applications then you will need to edit the context.xml file for both applications

To enable remote access for the Tomcat manager application, edit the context.xml file as shown below:

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

Remove the following lines:

				
					<Valve className="org.apache.catalina.valves.RemoteAddrValve"
allow="127\.\d+\.\d+\.\d+|::1|0:0:0:0:0:0:0:1" />
				
			

To enable remote access for the Tomcat host manager application, edit the context.xml file as shown below:

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

Remove the following lines:

				
					<Valve className="org.apache.catalina.valves.RemoteAddrValve"
allow="127\.\d+\.\d+\.\d+|::1|0:0:0:0:0:0:0:1" />
				
			

Save and close the file then restart the Tomcat service to apply the configuration changes.

				
					systemctl restart tomcat
				
			

Configure Nginx as a Reverse Proxy for Apache Tomcat

It is recommended to install and configure the Nginx as a reverse proxy to redirect all the traffics coming on port 80 to Apache Tomcat Server on port 8080.

To do so, install the Nginx server package using the command below:

				
					apt-get install nginx -y
				
			

Once the Nginx is installed, create an Nginx virtual host configuration file for Tomcat.

				
					nano /etc/nginx/conf.d/tomcat.conf
				
			

Add the following configuration to redirect all traffic coming on domain tomcat.yourdomain.com on port 80 to the Apache Tomcat server running on the local host on port 8080.

				
					server {
listen 80;
server_name tomcat.yourdomain.com;

location / {
proxy_redirect off;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header Host $host;
proxy_pass http://localhost:8080;
}
}
				
			

Save and close the file when you are finished. Then, run the following command to verify the Nginx for any syntax error.

				
					nginx -t
				
			

You should see the following output.

				
					nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
				
			

Finally, restart the Nginx service to apply the configuration changes.

				
					systemctl restart nginx
				
			

You can also verify the status of the Nginx service using the command below:

				
					systemctl status nginx
				
			

You should see the following output:

				
					● nginx.service - A high performance web server and a reverse proxy server
Loaded: loaded (/lib/systemd/system/nginx.service; enabled; vendor preset: enabled)
Active: active (running) since Thu 2021-12-02 08:50:17 UTC; 4s ago
Docs: man:nginx(8)
Process: 5088 ExecStartPre=/usr/sbin/nginx -t -q -g daemon on; master_process on; (code=exited, status=0/SUCCESS)
Process: 5104 ExecStart=/usr/sbin/nginx -g daemon on; master_process on; (code=exited, status=0/SUCCESS)
Main PID: 5105 (nginx)
Tasks: 2 (limit: 2353)
Memory: 2.7M
CGroup: /system.slice/nginx.service
├─5105 nginx: master process /usr/sbin/nginx -g daemon on; master_process on;
└─5106 nginx: worker process

Dec 02 08:50:17 server systemd[1]: Starting A high performance web server and a reverse proxy server...
Dec 02 08:50:17 server systemd[1]: Started A high performance web server and a reverse proxy server.

				
			

Access Apache Tomcat

In the next section of this guide of how to Install Apache Tomcat Server on Ubuntu 20.04 we will open your favorite web browser and access the Tomcat web interface using the URL http://tomcat.yourdomain.com. You should see the Tomcat web UI on the following screen:

Click on the Manager App to access the Tomcat Manager application, you will be asked to provide a Tomcat admin username and password:

Provide your Tomcat admin username, password and click on the Sign In button. You should see the Tomcat Manager application dashboard.

If you want to access the Host Manager application, click on the Host Manager. You should see the following screen:

How to Install Apache Tomcat Server on Ubuntu 20.04 Conclusion

In this post, you learned how to install Apache Tomcat with Nginx as a reverse proxy on Ubuntu 20.04 server. You can now start creating your Java application and host it using Tomcat.

Avatar for Hitesh Jethva
Hitesh Jethva

I am a fan of open source technology and have more than 10 years of experience working with Linux and Open Source technologies. I am one of the Linux technical writers for Cloud Infrastructure Services.

5 1 vote
Article Rating
Subscribe
Notify of
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x