Apache Tomcat is a widely used web server and servlet container for running Java applications. This guide provides detailed instructions on how to install and configure Apache Tomcat on AlmaLinux 9.
Install Apache Tomcat 10 on AlmaLinux
Prerequisites
- An AlmaLinux 9 dedicated server or KVM VPS.
- A user with sudo privileges.
Step 1: Update Your System
Ensure your system is up-to-date.
sudo dnf update -y
Step 2: Install Java
Apache Tomcat requires Java to run. Install OpenJDK, the open-source implementation of the Java Platform.
sudo dnf install java-17-openjdk-devel -y
Verify the installation:
java -version
Step 3: Install and Configure Apache Tomcat
First, let's create a user. For security purposes, create a separate user and group to run Tomcat.
sudo groupadd tomcat
sudo useradd -g tomcat -d /opt/tomcat -s /bin/nologin tomcat
Download the latest version of Apache Tomcat 10 from the official website.
cd /tmp
wget https://dlcdn.apache.org/tomcat/tomcat-10/v10.1.26/bin/apache-tomcat-10.1.26.tar.gz
Now, let's start the installation process of Apache Tomcat 10
Extract the Tomcat package and move it to the /opt/tomcat
directory.
sudo tar -xzf apache-tomcat-10.1.26.tar.gz -C /opt/tomcat --strip-components=1
Set the appropriate permissions for the Apache Tomcat 10 directories.
sudo chown -R tomcat: /opt/tomcat
sudo chmod -R 755 /opt/tomcat
Create a systemd service file to manage the Tomcat service.
sudo vi /etc/systemd/system/tomcat.service
Add the following content to the file:
[Unit]
Description=Apache Tomcat Web Application Container
After=network.target
[Service]
Type=forking
User=tomcat
Group=tomcat
Environment="JAVA_HOME=/usr/lib/jvm/java-17-openjdk"
Environment="CATALINA_PID=/opt/tomcat/temp/tomcat.pid"
Environment="CATALINA_HOME=/opt/tomcat"
Environment="CATALINA_BASE=/opt/tomcat"
Environment="CATALINA_OPTS=-Xms512M -Xmx1024M -server -XX:+UseParallelGC"
Environment="JAVA_OPTS=-Djava.awt.headless=true -Djava.security.egd=file:/dev/./urandom"
ExecStart=/opt/tomcat/bin/startup.sh
ExecStop=/opt/tomcat/bin/shutdown.sh
[Install]
WantedBy=multi-user.target
Save and exit the file.
Reload the systemd daemon and start the Tomcat service.
sudo systemctl daemon-reload
sudo systemctl start tomcat
Enable Tomcat to start on boot:
sudo systemctl enable tomcat
Step 4: Adjust the Firewall
If you have a firewall enabled, allow traffic on port 8080.
sudo firewall-cmd --permanent --add-port=8080/tcp
sudo firewall-cmd --reload
Step 5: Verify the Installation
Open a web browser and go to http://your_server_ip:8080
. You should see the Apache Tomcat welcome page.
Step 6: Configure Tomcat Web Management Interface (Optional)
To access the Tomcat Web Management Interface, configure the users. Edit the tomcat-users.xml file:
sudo vi /opt/tomcat/conf/tomcat-users.xml
Add the following lines before the </tomcat-users>
line:
<role rolename="manager-gui"/>
<role rolename="admin-gui"/>
<user username="admin" password="password" roles="manager-gui,admin-gui"/>
Note: Replace admin and password with your preferred username and password.
Next, configure Access for the Tomcat Manager.
Tomcat's web.xml file for the Manager application controls access. By default, the Manager application is restricted to certain IP addresses. To allow access, you might need to configure the context.xml
file for the Manager application.
Edit the context.xml file for the Manager application:
sudo vi /opt/tomcat/webapps/manager/META-INF/context.xml
Comment out or modify the Valve section to allow access from all IP addresses or specific IP addresses:
<!--
<Valve className="org.apache.catalina.valves.RemoteAddrValve"
allow="127\.\d+\.\d+\.\d+|::1|0:0:0:0:0:0:0:1"/>
-->
Alternatively, if you only want to allow access from specific IP addresses, you can list them in the allow attribute. For example:
<Valve className="org.apache.catalina.valves.RemoteAddrValve"
allow="127\.\d+\.\d+\.\d+|::1|0:0:0:0:0:0:0:1|YOUR_IP_ADDRESS"/>
Replace YOUR_IP_ADDRESS with the actual IP address you want to allow access from.
Step 7: Restart and Access Apache Tomcat 10
Restart the Tomcat service to apply the changes.
sudo systemctl restart tomcat
Now, you can access the Tomcat Manager and Host Manager interfaces at:
Tomcat Manager: http://your_server_ip:8080/manager/html
Host Manager: http://your_server_ip:8080/host-manager/html
Log in with the username and password you configured in the tomcat-users.xml
file.
Conclusion
You have successfully installed and configured Apache Tomcat on your AlmaLinux 9 server. You can now deploy your Java applications on the Tomcat server and manage them through the web management interface.