How to Install and Configure Apache Tomcat 9 in CentOS 8/7?


Apache Tomcat is a popular open-source web server and servlet container that is widely used to deploy Java-based web applications. In this article, we will show you how to install and configure Apache Tomcat 9 on CentOS 8/7.

Step 1: Install Java

The first step to installing Apache Tomcat is to install Java. Tomcat requires a Java Development Kit (JDK) version 8 or later to be installed. You can check installed Java version on your system by running following command −

java -version

If Java is not installed on your system, you can install it by running following command −

sudo dnf install java-1.8.0-openjdk-devel

This will install OpenJDK 8 development kit, which is recommended Java version for Tomcat 9.

Step 2: Download and Install Apache Tomcat

After installing Java, you can download latest version of Apache Tomcat 9 from official Apache website. You can use following command to download Tomcat 9 archive −

wget https://downloads.apache.org/tomcat/tomcat-9/v9.0.59/bin/apache-tomcat-9.0.59.tar.gz

Once download is complete, extract Tomcat archive using following command −

tar -xzf apache-tomcat-9.0.59.tar.gz

This will extract Tomcat files to a directory named apache-tomcat-9.0.59.

Step 3: Configure Tomcat

By default, Tomcat listens on port 8080. If you want to change this, you can edit conf/server.xml file and change Connector element as follows −

<Connector port="80" protocol="HTTP/1.1"
           connectionTimeout="20000"
           redirectPort="8443" />

In this example, Tomcat will listen on port 80 instead of 8080.

Next, you may want to configure Tomcat to use a different port for shutdown command. By default, Tomcat listens on port 8005 for shutdown command. You can change this by editing conf/server.xml file and changing Server element as follows −

<Server port="9005" shutdown="SHUTDOWN">

In this example, Tomcat will listen on port 9005 for shutdown command.

Step 4: Start Tomcat

To start Tomcat, navigate to bin directory in Tomcat installation directory and run following command −

./startup.sh

This will start Tomcat in background. You can access Tomcat web interface by opening a web browser and navigating to http://localhost:8080.

Step 5: Configure Tomcat as a System Service

If you want Tomcat to start automatically when system boots up, you can configure it as a system service. To do this, create a new file named tomcat.service in /etc/systemd/system/ directory with following content −

[Unit]
Description=Apache Tomcat 9
After=syslog.target network.target

[Service]
User=tomcat
Group=tomcat
Type=forking
Environment=CATALINA_PID=/opt/tomcat/temp/tomcat.pid
Environment=CATALINA_HOME=/opt/tomcat
Environment=CATALINA_BASE=/opt/tomcat
ExecStart=/opt/tomcat/bin/startup.sh
ExecStop=/opt/tomcat/bin/shutdown.sh

[Install]
WantedBy=multi-user.target

Save file and Next, reload systemd daemon to recognize new service file −

sudo systemctl daemon-reload

Then, enable Tomcat service to start automatically at boot time −

sudo systemctl enable tomcat.service

Finally, start Tomcat service −

sudo systemctl start tomcat.service

You can check status of Tomcat service using following command −

sudo systemctl status tomcat.service

This will show you if Tomcat service is running or not.

Step 6: Configure Tomcat Security

By default, Tomcat does not require authentication to access its web interface. This can be a security risk, especially if Tomcat is running on a public network. To secure Tomcat, you can add a username and password to conf/tomcat-users.xml file. Open file and add following lines between <tomcat-users> and </tomcat-users> tags −

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

In this example, username is admin and password is password. You should replace these values with your own username and password. roles attribute specifies roles that user is assigned to. In this case, user has both manager-gui and admin-gui roles, which allow them to manage Tomcat through web interface.

After saving changes to tomcat-users.xml file, restart Tomcat for changes to take effect −

sudo systemctl restart tomcat.service

Step 7: Configure Tomcat Virtual Hosts

Virtual hosts allow you to run multiple websites on same Tomcat instance. To configure virtual hosts, edit conf/server.xml file and add following lines inside <Host> element −

<Host name="example.com" appBase="webapps/example">
   <Context path="" docBase="."/>
</Host>

In this example, we're creating a virtual host for domain example.com and specifying directory webapps/example as application base for this virtual host. <Context> element sets root context for this virtual host.

After saving changes to server.xml file, create directory webapps/example and deploy your web application in that directory.

While steps outlined in this article are specific to CentOS, they can be adapted to other Linux distributions with minor modifications. Additionally, there are a few other things you may want to consider when configuring your Tomcat server −

  • HTTPS − By default, Tomcat listens for HTTP requests on port 8080. To enable HTTPS, you will need to configure Tomcat to use a SSL certificate. You can do this by following instructions provided in Tomcat documentation.

  • Performance Tuning − Tomcat provides a number of configuration options that can be used to optimize its performance. These include adjusting thread pool size, setting JVM memory settings, and optimizing garbage collection settings. You can find more information on performance tuning in Tomcat documentation.

  • Logging − Tomcat provides a comprehensive logging system that can be used to monitor performance and activity of your server. You can configure logging system by editing conf/logging.properties file.

  • Monitoring − To ensure that your Tomcat server is running smoothly, you may want to consider using a monitoring tool such as Nagios or Zabbix. These tools can be used to monitor server performance, track resource usage, and alert you to potential issues.

  • File permissions − When installing and configuring Apache Tomcat, it's important to ensure that proper file permissions are set. You can do this by running following commands −

sudo chown -R tomcat:tomcat /opt/tomcat
sudo chmod +x /opt/tomcat/bin/*.sh

These commands ensure that tomcat user owns Tomcat installation directory, and that scripts in bin directory are executable.

  • Firewall − To ensure security of your server, it's important to configure firewall to allow traffic on appropriate ports. To allow traffic on port 8080, for example, you can run following command −

sudo firewall-cmd --zone=public --add-port=8080/tcp --permanent
sudo firewall-cmd --reload

This will open port 8080 for incoming TCP traffic.

  • Backups − To ensure that your data is safe in case of a server failure or data loss, it's important to create regular backups of your Tomcat installation and configuration files. You can use a tool like tar or rsync to create backups, or you can use a backup tool like rsnapshot or Bacula.

  • Resource limits − To ensure that your Tomcat server doesn't consume too many resources, you can set limits on amount of CPU, memory, and other resources that it can use. You can use ulimit command to set these limits, or you can use a tool like cgroups to manage resource usage.

  • JMX Monitoring − Tomcat provides built-in support for JMX monitoring, which allows you to monitor performance of your Tomcat server using tools like JConsole or VisualVM. To enable JMX monitoring, you can add following line to your Tomcat startup script −

CATALINA_OPTS="$CATALINA_OPTS -Dcom.sun.management.jmxremote"

You can then use a JMX monitoring tool to connect to Tomcat server and monitor its performance.

  • Connection Pooling − Connection pooling can improve performance of your Tomcat server by reducing overhead associated with establishing new database connections. Tomcat provides built-in support for connection pooling through its JNDI resources. You can configure connection pooling by editing conf/context.xml file and adding a Resource element.

  • Session Persistence − By default, Tomcat stores session data in memory, which can be problematic if server crashes or needs to be restarted. To ensure that session data is persisted across server restarts, you can configure Tomcat to use a persistent session manager. Tomcat provides several options for session persistence, including file-based storage, JDBC-based storage, and distributed caching.

By taking these additional steps, you can ensure that your Tomcat server is running smoothly and securely, and is optimized for performance.

Conclusion

In this article, we have shown you how to install and configure Apache Tomcat 9 on CentOS 8/7. We covered steps to install Java, download and install Tomcat, configure Tomcat, start Tomcat, configure Tomcat as a system service, secure Tomcat, and configure Tomcat virtual hosts. With these steps, you can set up a robust and secure Tomcat server to deploy your Java web applications.

Updated on: 12-May-2023

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements