Important
This article is obsolete and may contain outdated information. Please consult the latest documentation for up-to-date information.
Apache Tomcat is a free, open-source Java Servlet that provides an HTTP web server environment for running Java applications. It is simple, easy to use, and one of the most widely used web servers in the world. Tomcat acts as a development server that can be used to test and build applications using JSF 2, servlets/JSP, or other Java-based dynamic web technologies.
In this tutorial, we will explain how to install the latest version of Apache Tomcat using an Ubuntu 22.04 LTS Cloud server.
Requirements
- A Cloud VPS server running Ubuntu 22.04 LTS or later.
- A static IP configured on your server.
- A root password configured on your VPS server in Spain.
Install Java
Apache Tomcat requires you to install Java version 8 or later. If you do not have it installed, you can install it with the following command:
# apt install default-jdk -yOnce installed, check the Java version with the following command:
# java --versionYou should see a similar result:
openjdk 11.0.15 2022-04-19
OpenJDK Runtime Environment (build 11.0.15+10-Ubuntu-0ubuntu0.22.04.1)
OpenJDK 64-Bit Server VM (build 11.0.15+10-Ubuntu-0ubuntu0.22.04.1, mixed mode, sharing)Download Tomcat
Before you begin, use the command:
# apt updateYou will need to create a user and grant it the appropriate permissions:
# useradd -m -d /opt/tomcat -U -s /bin/false tomcatNext, download your desired version of Apache Tomcat; you can check it on the official Tomcat website. Example using the latest available version:
# wget https://dlcdn.apache.org/tomcat/tomcat-10/v10.0.23/bin/apache-tomcat-10.0.23.zipOnce downloaded, extract the archive with the following command:
# unzip apache-tomcat-10.0.23.zip && mv apache-tomcat-10.0.23/* /opt/tomcatThen, change ownership of the /opt/tomcat directory to Tomcat with the following command:
# chown -R tomcat:tomcat /opt/tomcat/
# chmod -R 755 /opt/tomcat/Open the tomcat configuration file:
# nano /opt/tomcat/conf/tomcat-users.xmlAdd the following lines above the </tomcat-users> line:
<role rolename="manager-gui"/>
<role rolename="admin-gui"/>
<user username="admin" password="your-password" roles="manager-gui,admin-gui"/>You will now have an administrator user to access Tomcat.
Configure the Tomcat Web Management Interface
Next, you will need to allow Tomcat Manager and Host Manager to be accessible from your remote IP.
For the host management application, edit the following file:
# nano /opt/tomcat/webapps/host-manager/META-INF/context.xmlAdd the IP address of the remote virtual machine from which you want access for host management.
<Context antiResourceLocking="false" privileged="true" >
<Valve className="org.apache.catalina.valves.RemoteAddrValve"
allow="127\.\d+\.\d+\.\d+|::1|0:0:0:0:0:0:0:1|your-remote-system-ip" />Save and close the file when you are finished.
Create the Systemd Service File for Tomcat
Next, you will need to create the systemd service file to manage the Tomcat service. You can create it with the following command:
# nano /etc/systemd/system/tomcat.serviceAdd the following lines:
[Unit]
Description=Tomcat servlet container
After=network.target
[Service]
Type=forking
User=tomcat
Group=tomcat
Environment="JAVA_HOME=/usr/lib/jvm/default-java"
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.targetSave and close the file. Then, reload the systemd daemon and start the Tomcat service with the following commands:
# systemctl daemon-reload
# systemctl start tomcatYou can also check the Tomcat service with the following command:
# systemctl status tomcatYou should see the following result:
● tomcat.service - Tomcat servlet container
Loaded: loaded (/etc/systemd/system/tomcat.service; disabled; vendor preset: enabled)
Active: active (running) since Sun 2019-10-06 06:57:39 UTC; 19s ago
Process: 17893 ExecStart=/home/tomcat/bin/startup.sh (code=exited, status=0/SUCCESS)
Main PID: 17903 (java)
Tasks: 43 (limit: 1114)
CGroup: /system.slice/tomcat.service
└─17903 /usr/lib/jvm/default-java/bin/java -Djava.util.logging.config.file=/home/tomcat/conf/logging.properties -Djava.util.logging.
Oct 06 06:57:39 ubuntu1804 systemd[1]: Starting Tomcat servlet container...
Oct 06 06:57:39 ubuntu1804 startup.sh[17893]: Tomcat started.
Oct 06 06:57:39 ubuntu1804 systemd[1]: Started Tomcat servlet container.Access the Tomcat Web Interface
Note: we will need to open port 8080 in the client panel firewall.
Tomcat is now installed and running on port 8080. Open your web browser and go to the URL http://your-server-ip:8080. You will be redirected to the Tomcat panel as shown below:
Now, click App Manager or Host Manager. You will be asked to provide a username and password as shown on the following screen:
Provide your admin username and click the Sign In button. You will be redirected to the Manager App or Host Manager panel:
Configure NGINX as a Reverse Proxy
You can also configure NGINX as a reverse proxy for the Tomcat server. This way, Nginx will accept requests on port 80 in front of the Tomcat server.
First, install the NGINX web server with the following command:
# apt install nginx -yOnce installed, create a new virtual host configuration file for Tomcat.
# nano /etc/nginx/sites-available/tomcat.confAdd the following lines:
upstream tomcat {
server 127.0.0.1:8080 weight=100 max_fails=5 fail_timeout=5;
}
server {
listen 80;
server_name ejemplo.com;
location / {
proxy_set_header X-Forwarded-Host $host;
proxy_set_header X-Forwarded-Server $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://tomcat/;
}
}Save and close the file. Then, enable the NGINX web server's virtual host file to apply the configuration:
# ln -s /etc/nginx/sites-available/tomcat.conf /etc/nginx/sites-enabled/
# systemctl restart nginxObtain a Let's Encrypt SSL Certificate
Now, if you need an SSL certificate, you only need to install Let's Encrypt. You can do this with the command:
# apt install certbot python3-certbot-nginxAnd configure the certificate with the command:
# certbot --nginx -d ejemplo.com -d www.ejemplo.comYou can find more information in our article: Obtain Let's Encrypt Certificates with Certbot
Congratulations! You can now access the Tomcat server through the URL http://ejemplo.com without specifying Tomcat port 8080.
Remember, if you have any questions about this or any other issue related to your servers at Clouding, feel free to write to soporte@clouding.io We are here for whatever you need. Just ask us!