In this guide, we will show you how to install Tomcat on an Ubuntu 18.04 VPS. Tomcat is an open source web server and servlet container developed by the Apache Software Foundation (ASF) that executes Java servlets and renders Web pages that include Java server page coding.
In order to run Tomcat on your VPS, the following requirements have to be met:
- Java 8 or higher
- SSH root access to your server
- A VPS with at least 1 GB of RAM
Table of Contents
Update OS packages
Before we can start with the Tomcat installation, we have to make sure that all Ubuntu OS packages installed on the server are up to date. We can do this by executing the following commands:
Install JAVA
In order to install and run Tomcat on the server, we need to have Java installed. We can check if Java is already installed using this command:
If there is no output, it means that Java is not installed on the server yet. We can install Oracle JDK or OpenJDK. We need to have Java version 8 or higher installed on your system to run Tomcat 9. We can install it using the following command:
In order to check the Java version, run the following command on your server:
We should receive the following output:
Install Tomcat
Download the latest stable version of Tomcat from the Apache Tomcat official website at http://tomcat.apache.org/download-90.cgi and extract it in a directory on your server:
It is not a good idea to run Tomcat as root user, so for security reason, we will create a new system user:
Once the tomcat9 system user has been created, change the ownership of the /opt/tomcat9 directory to tomcat user:
Create a new systemd file for Tomcat:
Add the following content:
Save the tomcat9.service file. Run the following command to reload the systemd manager configuration:
Now we can start the Apache Tomcat 9 service:
In order to enable the Tomcat service to start on server boot, run:
We can check the status of the Tomcat 9 service and confirm that it is properly installed by executing the following command:
The output of this command should be similar to this:
Apache Tomcat is listening on port 8080, so open a web browser and navigate to http://IP_address:8080 and you should see the home page of Apache Tomcat.
Create a reverse proxy in Apache
We can also configure Apache2 web server as a reverse proxy for Tomcat 9 web server, so Apache will accept all requests over port 80 in front of Tomcat 9 which is running on port 8080.
We need to enable two Apache modules: proxy and proxy_http. We can use the a2enmod command to enable them:
Then, we have to restart Apache for the changes to take effect:
Create/modify the virtual host configuration about your domain and add/modify the proxy directives (ProxyRequests, ProxyPass and ProxyPassReverse) as shown in our example:
vi /etc/apache2/sites-available/your-domain.com.conf
Enable the ‘your-domain.com.conf’ Apache configuration file if it was not enabled previously, and restart Apache for the changes to take effect:
Now we can access the Tomcat webapp without using port 8080 in a web browser, i.e. we can access it using: http://your-domain.com
That is it. We successfully installed Tomcat on Ubuntu 18.04.
Of course, you don’t have to install Tomcat on Ubuntu 18.04, if you use one of our Managed Tomcat Hosting solutions, in which case you can simply ask our expert Linux admins to set up and configure Apache Tomcat on Ubuntu 18.04 for you. They are available 24×7 and will take care of your request immediately. We also published a guide on How to Install Tomcat on Ubuntu 20.04.
PS. If you liked this post on how to install Tomcat on Ubuntu 18.04, please share it with your friends on the social networks using the buttons on the left or simply leave a reply below. Thanks.