Tomcat, also called Apache Tomcat, is an open-source web server and servlet container for deploying Java-based applications. It is lightweight, easy to use, has a robust ecosystem, and powers many large-scale web applications. Moreover, using Apache will boost your website performance and help you to have a better hosting experience.
Step 1 – Install Java OpenJDK
Apache Tomcat is a Java-based application, so you will need to install Java JDK on your server. You can install it using the following command.
apt install openjdk-11-jdk
Once Java is installed, you can verify the Java installation using the following command.
java -version
Output.
openjdk version "11.0.20.1" 2023-08-24 OpenJDK Runtime Environment (build 11.0.20.1+1-post-Ubuntu-0ubuntu122.04) OpenJDK 64-Bit Server VM (build 11.0.20.1+1-post-Ubuntu-0ubuntu122.04, mixed mode, sharing)
Step 2 – Download Apache Tomcat
First, create a dedicated user to run Apache Tomcat.
useradd -r -m -U -d /opt/tomcat -s /bin/false tomcat
Next, download the Apache Tomcat from their official website.
wget https://dlcdn.apache.org/tomcat/tomcat-10/v10.1.15/bin/apache-tomcat-10.1.15.tar.gz
Then, extract the downloaded file.
tar xzf apache-tomcat-10.1.15.tar.gz
Next, move the extracted directory to /opt.
mv apache-tomcat-10.1.15/* /opt/tomcat/
Next, set proper permissions and ownership on the Tomcat directory.
chown -R tomcat: /opt/tomcat sh -c 'chmod +x /opt/tomcat/bin/*.sh'
Step 3 – Create a Tomcat User Account
Next, you must create an admin and manager account to access the Tomcat application. You can create it by editing tomcat-users.xml file.
nano /opt/tomcat/conf/tomcat-users.xml
Add the following lines above the last line.
<role rolename="manager-gui" /> <user username="manager" password="secure-password" roles="manager-gui" /> <role rolename="admin-gui" /> <user username="admin" password="secure-password" roles="manager-gui,admin-gui" />
Save and close the file when you are done.
Step 4 – Allow Remote Hosts to Access Tomcat
By default, Tomcat is accessible only from the local host. To allow access from the remote host, you must modify Tomcat configuration files.
To allow access to the Manager app, edit the context.xml file.
nano /opt/tomcat/webapps/manager/META-INF/context.xml
Remove the following line.
<Valve className="org.apache.catalina.valves.RemoteAddrValve" allow="127\.\d+\.\d+\.\d+|::1|0:0:0:0:0:0:0:1" />
To allow access to the Host Manager app, edit the context.xml file.
nano /opt/tomcat/webapps/host-manager/META-INF/context.xml
Remove the following line.
<Valve className="org.apache.catalina.valves.RemoteAddrValve" allow="127\.\d+\.\d+\.\d+|::1|0:0:0:0:0:0:0:1" />
Save and close the file.
Step 5 – Create a Systemd Service File for Tomcat
Next, you must create a systemd service file to manage the Tomcat service.
nano /etc/systemd/system/tomcat.service
Add the following lines.
[Unit] Description=Apache Tomcat 10 Web Application Server After=network.target [Service] Type=forking User=tomcat Group=tomcat Environment="JAVA_HOME=/usr/lib/jvm/java-11-openjdk-amd64" Environment="CATALINA_HOME=/opt/tomcat" Environment="CATALINA_BASE=/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
Save and close the file, then reload the systemd daemon to apply the changes.
systemctl daemon-reload
Next, start the Tomcat service with the following command.
systemctl start tomcat
You can check the status of Tomcat with the following command.
systemctl status tomcat
Output.
● tomcat.service - Apache Tomcat 10 Web Application Server Loaded: loaded (/etc/systemd/system/tomcat.service; disabled; vendor preset: enabled) Active: active (running) since Sun 2023-10-29 03:15:20 UTC; 3s ago Process: 37759 ExecStart=/opt/tomcat/bin/startup.sh (code=exited, status=0/SUCCESS) Main PID: 37766 (java) Tasks: 15 (limit: 2242) Memory: 113.9M CPU: 3.815s CGroup: /system.slice/tomcat.service └─37766 /usr/lib/jvm/java-11-openjdk-amd64/bin/java -Djava.util.logging.config.file=/opt/tomcat/conf/logging.properties -Djava.util.logging.manager=org.ap> Oct 29 03:15:20 linux systemd[1]: Starting Apache Tomcat 10 Web Application Server... Oct 29 03:15:20 linux startup.sh[37759]: Tomcat started. Oct 29 03:15:20 linux systemd[1]: Started Apache Tomcat 10 Web Application Server.
Step 6 – Access Tomcat Web UI
Now, open your web browser and access the Tomcat UI using the URL http://your-server-ip:8080. You will see the following page.
Click on the manager app, and you will be asked to provide an admin username and password.
Provide your admin username and password and click on Sign in. You will see the manager app dashboard.
Click Host Manager on the Tomcat Home page. You will see the Tomcat Host Manager on the following screen.
Conclusion
Congratulations! You have successfully installed Apache Tomcat 10 on Ubuntu 22.04. You can now build a Java application and deploy it using the Apache Tomcat. Try to deploy the Tomcat web server on dedicated server hosting from Atlantic.Net!