Introduction

 Do you know, The Apache Software Foundation developed Tomcat, an open-source web server. It is frequently used to host web apps that are built on Java. Built on Java technologies, Tomcat implements the Java Servlet and JavaServer Pages (JSP) specifications, acting as a bridge between web servers and Java applications. It efficiently handles dynamic content and client requests.

Tomcat is favored for its scalability, robustness, and strong community support. Its modular design and compliance with industry standards make it easy for developers to build and deploy web applications.

In this blog, we explain how to set up Tomcat on Linux. If you need instructions for other operating systems, please visit the official Tomcat website.

Step 1 – Install Java

The Java runtime environment, or JRE, needs to be installed on your computer. You must have JRE 8 or later installed on your system before you can use Tomcat 10. Use the following command to install OpenJDK to fulfil the requirements.

sudo apt update

Use the following command to download JDK Java to your system
sudo apt install default-jdk -y

After the installation, check the Java version with the following command. If you can’t see the version, there may be an issue with your Java installation that you’ll need to resolve.
java -version

Step 2 – Create Tomcat User

We recommended running a Tomcat server with a dedicated user account. Create a new user, which is recommended for security purposes mainly for production deployments.

sudo useradd -m -d /opt/tomcat -U -s /bin/false tomcat

The aforementioned command will add a user and group to your system called “tomcat.”

Step 3 – Install Tomcat 10

Use the below command to download Tomcat 10.

wget https://dlcdn.apache.org/tomcat/tomcat-10/v10.0.20/bin/apache-tomcat-10.0.20.tar.gz

Once the download is complete, move all of the contents to the Tomcat home directory and extract the archive.

sudo tar xzvf apache-tomcat-10*tar.gz -C /opt/tomcat --strip-components=1
sudo chown -R tomcat:tomcat /opt/tomcat/ sudo chmod -R u+x /opt/tomcat/bin

Step 4 – Create Tomcat User

Now, set up user accounts in your Tomcat to allow safe access to the admin and manager sites. . To do this, edit conf/tomcat-users.xml file in your editor and paste the following code inside <tomcat-users> </tomcat-users> tags. We advise replacing the password in the setup below with a more secure one.

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

Add the following values. Don’t forget to update the manager and admin passwords.

—————————————————————————————–

<!– 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.

Step 5 – Enable Remote Tomcat Access

The default Tomcat manager and host-manager applications are accessible for localhost only. To enable access to these pages from the remote system, the following configuration files need to be modified.

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

To enable connections from any location, comment out the portion that was added to restrict IP addresses.

—————————————————————————————-

<Context antiResourceLocking=”false” privileged=”true” >
<CookieProcessor className=”org.apache.tomcat.util.http.Rfc6265CookieProcessor”

sameSiteCookies=”strict” />
<!– <Valve className=”org.apache.catalina.valves.RemoteAddrValve”

allow=”127\.\d+\.\d+\.\d+|::1|0:0:0:0:0:0:0:1″ /> –> </Context>

——————————————————————————————

To modify the host manager application’s context.xml, use the text editor sudo nano /opt/tomcat/webapps/host-manager/META-INF/context.xml.

Comment out the same section to allow connections from anywhere.

—————————————————————————————— |

<Context antiResourceLocking=”false” privileged=”true” >
<CookieProcessor className=”org.apache.tomcat.util.http.Rfc6265CookieProcessor”

|

</Context> ——————————————————————————————-| Save all files and close it.

Step 6 – Generate a Systemd Unit File for Tomcat

Tomcat provides bash scripts to start, stop service. Write a startup script to manage Tomcat as a systemd service, though, to make things easier. The following information should be included in a tomcat.service file that we create:

sudo nano /etc/systemd/system/tomcat.service —————————————————————————————

[Unit] Description=Tomcat

sameSiteCookies=”strict” />
<!–<Valve className=”org.apache.catalina.valves.RemoteAddrValve”

allow=”127\.\d+\.\d+\.\d+|::1|0:0:0:0:0:0:0:1″ /> –>
… |

After=network.target

[Service]
Type=forking AmbientCapabilities=CAP_NET_BIND_SERVICE

User=tomcat Group=tomcat

Environment=”JAVA_HOME=/usr/lib/jvm/java-1.11.0-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

—————————————————————————————-

*Note : AmbientCapabilities=CAP_NET_BIND_SERVICE helps to bind to port 80. Also change /opt/tomcat/conf/server.xml connector port from 8080 to 80 .

Reload the daemon using the following command
sudo systemctl daemon-reload

Let’s start the Tomcat service using the following command:
sudo systemctl start tomcat.service

And enable the Tomcat service to start automatically after the system reboots with the following command
sudo systemctl enable tomcat.service

Check the status of the Tomcat service. It should be in a running state if the setup is working correctly
sudo systemctl status tomcat.service

Step 7 – Access the Tomcat Web Interface

The default Tomcat server runs on port 8080. You may access the web interface from your system since you have set up Tomcat. You can access tomcat interfaces by entering your server’s IP address or a domain name pointed to that server, followed by port 8080 in your browser:

Change flexicloud.local with your server ip or domain or localhost.

http://flexicloud.local:8080/ http://flexicloud.local:8080/manager/ http://flexicloud.local:8080/host-manager/

Reference : https://tecadmin.net/how-to-install-tomcat-10-on-ubuntu-20-04/



Conclusion

Tomcat is a useful tool for running Java Servlets and JSPs. It lets you run Java code on a web server made with Java. We hope this guide helped you install Tomcat on Linux and set up some basic configurations.