Apache Tomcat is a widely used web server and servlet container that's essential for deploying Java applications. In this tutorial, we will guide you through the installation of Apache Tomcat 10 on an Ubuntu 20.04 server. We'll also set up user authentication, configure access to Tomcat's admin interface, and ensure that your server is ready to serve Java applications.
## Prerequisites
Before we begin, make sure you have the following:
- A server running Ubuntu 20.04.
- Root or sudo access to the server.
Step 1: Install Tomcat and JDK
1.1. Create a Separate User for Tomcat:
For security purposes, it's best to run Tomcat under a separate user. Create a user named "tomcat" with the following command:
# useradd -m -d /opt/tomcat -U -s /bin/false tomcat
1.2. Install the Java Development Kit (JDK):
Apache Tomcat requires Java to run. Install the default JDK package with the following command:
# apt update
# apt install default-jdk
Check the installed Java version:
# java -version
Step 2: Download and Configure Tomcat
2.1. Navigate to the `/tmp` directory:
# cd /tmp
2.2. Download and Extract Tomcat:
Download the latest version of Apache Tomcat 10 using `wget`:
# wget https://archive.apache.org/dist/tomcat/tomcat-10/v10.0.11/bin/apache-tomcat-10.0.11.tar.gz
Extract the downloaded archive to the `/opt/tomcat` directory:
# tar xzvf apache-tomcat-10*tar.gz -C /opt/tomcat --strip-components=1
Set the correct permissions for the Tomcat installation:
# chown -R tomcat:tomcat /opt/tomcat/
# chmod -R u+x /opt/tomcat/bin
Step 3: Configure Admin Users
3.1. Open the `tomcat-users.xml` file for editing:
# vi /opt/tomcat/conf/tomcat-users.xml
3.2. Add the following lines before the closing `</tomcat-users>` tag:
<user username="manager" password="redhat" roles="manager-gui" />
<role rolename="admin-gui" />
<user username="admin" password="redhat" roles="manager-gui,admin-gui" />
3.3. Save and close the file.
Step 4: Remove IP Restrictions for Admin Pages
4.1. Remove IP restrictions for the Manager page:
# vi /opt/tomcat/webapps/manager/META-INF/context.xml
Comment out the Valve definition, like this:
allow="127\.\d+\.\d+\.\d+|::1|0:0:0:0:0:0:0:1" /> -->
4.2. Save and close the file.
Repeat the same process for the Host Manager:
#vi /opt/tomcat/webapps/host-manager/META-INF/context.xml
Comment out the Valve definition, like this:
allow="127\.\d+\.\d+\.\d+|::1|0:0:0:0:0:0:0:1" /> -->
4.3. Save and close the file.
Step 5: Create a systemd Service
5.1. Determine the Java path:
# update-java-alternatives -l
Note the path where Java is located.
5.2. Create a systemd service file for Tomcat:
# vi /etc/systemd/system/tomcat.service
Add the following content, modifying the `JAVA_HOME` path if necessary:
Description=Tomcat
After=network.target
[Service]
Type=forking
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
RestartSec=10
Restart=always
WantedBy=multi-user.target
5.3. Save and close the file.
5.4. Reload the systemd daemon:
# systemctl daemon-reload
5.5. Start Tomcat and enable it to start on boot:
# systemctl start tomcat
# systemctl enable tomcat
Step 6: Configure Firewall
Allow traffic on port 8080 (Tomcat's default port) through the firewall:
# ufw allow 8080
Step 7: Access the Web Interface
A. You can now access the Tomcat web interface using your server's IP address:
http://192.168.2.202:8080
D. Press on the Host Manager button on the right. You’ll be prompted to enter the account credentials that you defined in a previous step.
Tomcat - Web Application Manager The Web Application Manager is used to manage your Java applications. You can start, stop, reload, deploy, and undeploy them from here. You can also run some diagnostics on your apps (for example, to find memory leaks). Information about your server is available at the very bottom of this page.
A. Download the war (https://tomcat.apache.org/tomcat-7.0-doc/appdev/sample/sample.war) on you desktop.
B. Go to the "Manager App" on the Tomcat web interface.
E. After deploying, you will find the "sample" application listed. Click on it to open the web page.
No comments:
Post a Comment