In the tutorial, we show you how to install Apache Tomcat server on Ubuntu Remote Server with Vultr Hosting example.
Related post:
– How to install Java in Ubuntu Remote Server – Vutr Hosting VPS Example
Contents
Technologies
– Ubuntu 18.10 x64
– Tomcat 9.0.13
– Vultr Hosting
Install Apache Tomcat
Create Vultr Vps
Follow the link to create a Linux Vultr VPS.
Use Putty to login Vultr VPS:
Install Java
Tomcat requires Java to be installed. We can install Open JDK
or Oracle Java
for development.
Follow below post for Java Installation:
– Install Java in Ubuntu Remote Server – Vutr Hosting VPS Example
Check Java version by cmd:
root@gkz-vps:~# java -version java version "11.0.1" 2018-10-16 LTS Java(TM) SE Runtime Environment 18.9 (build 11.0.1+13-LTS) Java HotSpot(TM) 64-Bit Server VM 18.9 (build 11.0.1+13-LTS, mixed mode) |
Create Tomcat User
We need a new tomcat user for security purposes.
sudo useradd -r -m -U -d /opt/tomcat -s /bin/false tomcat |
Install Tomcat
Download the latest binary release of Tomcat 9 from Tomcat Download page:
wget http://www.apache.org/dist/tomcat/tomcat-9/v9.0.13/bin/apache-tomcat-9.0.13.tar.gz -P /tmp |
Extract the Tomcat archive & move it to the /opt/tomcat
directory:
sudo tar xf /tmp/apache-tomcat-9*.tar.gz -C /opt/tomcat |
Create a symbolic link latest
which will point to the Tomcat installation directory:
sudo ln -s /opt/tomcat/apache-tomcat-9.0.13 /opt/tomcat/latest |
-> For upgrade Tomcat installation, just simply unpack the newer version and change the symlink to point to the latest version.
Change the directory /opt/tomcat/latest
ownership to user and group tomcat
:
sudo chown -RH tomcat: /opt/tomcat/latest sudo chmod o+x /opt/tomcat/latest/bin/ |
Setup Tomcat as a Service
Create a tomcat.service
as below:
sudo nano /etc/systemd/system/tomcat.service |
Content of tomcat.service
as below:
[Unit] Description=Tomcat 9 servlet container After=network.target [Service] Type=forking User=tomcat Group=tomcat Environment="JAVA_HOME=/usr/local/oracle-java-8/jdk1.8.0_191" Environment="JAVA_OPTS=-Djava.security.egd=file:///dev/urandom -Djava.awt.headless=true" Environment="CATALINA_BASE=/opt/tomcat/latest" Environment="CATALINA_HOME=/opt/tomcat/latest" Environment="CATALINA_PID=/opt/tomcat/latest/temp/tomcat.pid" Environment="CATALINA_OPTS=-Xms512M -Xmx1024M -server -XX:+UseParallelGC" ExecStart=/opt/tomcat/latest/bin/startup.sh ExecStop=/opt/tomcat/latest/bin/shutdown.sh [Install] WantedBy=multi-user.target |
Use below cmd to findout JAVA_HOME
:
root@gkz-vps:~# echo $JAVA_HOME /usr/local/oracle-java-11/jdk-11.0.1/bin/java |
Save the file and notify systemd for the new tomcat.service
changed:
sudo systemctl daemon-reload |
Start Tomcat service:
sudo systemctl start tomcat |
Check the service status:
sudo systemctl status tomcat |
-> Output:
To be automatically started at boot time, Enable the Tomcat service:
sudo systemctl enable tomcat |
Configure Tomcat Web Management
Add new user to tomcat-users.xml
to access the Tomcat web interface:
– Open tomcat-users.xml
by cmd ->
sudo nano /opt/tomcat/latest/conf/tomcat-users.xml
– Add admin
user ->
<tomcat-users> // // Comments // ... <role rolename="admin-gui"/> <role rolename="manager-gui"/> <user username="admin" password="admin" roles="admin-gui,manager-gui"/> </tomcat-users> |
To access the web interface from any IPs address,
-> Manager app, open file: sudo nano /opt/tomcat/latest/webapps/manager/META-INF/context.xml
-> For the Host Manager app, open file: sudo nano /opt/tomcat/latest/webapps/host-manager/META-INF/context.xml
Then change context.xml
as below:
<Context antiResourceLocking="false" privileged="true" > <!-- <Valve className="org.apache.catalina.valves.RemoteAddrValve" allow="127\.\d+\.\d+\.\d+|::1|0:0:0:0:0:0:0:1" /> --> </Context> |
We can control specific address IPs for accessing with |
operator as below:
<Context antiResourceLocking="false" privileged="true" > <Valve className="org.apache.catalina.valves.RemoteAddrValve" allow="127\.\d+\.\d+\.\d+|::1|0:0:0:0:0:0:0:1|140.82.15.242" /> </Context> |
Now restart Tomcat service to take the effect: sudo systemctl restart tomcat
Check Results
Access Tomcat ->
Server Status ->
Tomcat Web Application Manager ->
Tomcat Virtual Host Manager ->