How to Install Tomcat 9 on Ubuntu – Google Cloud. Tomcat is a server for serve Java basis applications. This tutorial will guide you to install latest Tomcat server on ubuntu server on Google Cloud Platform.
Requirements
- Your Compute Engine Instance running.
- For setup Compute Engine, See this :
Install Java
Connect Your Compute engine using SSH and let’s begin with update packages.
sudo apt update
sudo apt upgrade
Now you can Install OpenJDK java. Java is required for tomcat running.
sudo apt install default-jdk
Now let’s proceed with creating tomcat user.
Create and setup Tomcat User
For security reasons we create non root user to run tomcat.
sudo groupadd tomcat
Now you can create a tomcat user and assign it to home directory, where you going to install tomcat.
sudo useradd -s /bin/false -g tomcat -d /opt/tomcat tomcat
Install Tomcat
You can download latest release of tomcat here. Under the Binary Distributions, under Core, copy the link of the file with extension tar.gz
Create a directory for install tomcat
sudo mkdir /opt/tomcat
Download tomcat with the link you copied.
cd /tmp
curl -O http://mirrors.estointernet.in/apache/tomcat/tomcat-9/v9.0.22/bin/apache-tomcat-9.0.22.tar.gz
sudo tar xzvf apache-tomcat-9.0.22.tar.gz -C /opt/tomcat --strip-components=1
Setup Permissions
Goto the directory of the tomcat installed.
cd /opt/tomcat
Now run below commands for give correct permissions.
sudo chgrp -R tomcat /opt/tomcat
sudo chmod -R g+r conf
sudo chmod g+x conf
sudo chown -R tomcat webapps/ work/ temp/ logs/
Create Service
Now you want to locate the java installed folder.
sudo update-java-alternatives -l
Output
java-1.11.0-openjdk-amd64 1081 /usr/lib/jvm/java-1.11.0-openjdk-amd64
Now, create a new file for Tomcat inside /etc/systemd/system
directory.
sudo nano /etc/systemd/system/tomcat.service
Then paste below code. Make sure to modify the JAVA_HOME with the path of your Java installation.
[Unit]
Description=Apache Tomcat Web Application Container
After=network.target
[Service]
Type=forking
Environment=JAVA_HOME=/usr/lib/jvm/java-1.11.0-openjdk-amd64
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
User=tomcat
Group=tomcat
UMask=0007
RestartSec=10
Restart=always
[Install]
WantedBy=multi-user.target
Hit Ctrl + X
followed Y
and Enter
to save and exit the file.
Reload the systemd daemon.
sudo systemctl daemon-reload
Now you can start Tomcat server.
sudo systemctl start tomcat
Finally enable Tomcat to startup on system boot.
sudo systemctl enable tomcat
Configure Tomcat
To use the manager web app you need to login to the server. To setup your username and password edit the tomcat-users.xml
file and edit the username and password.
sudo nano /opt/tomcat/conf/tomcat-users.xml
<tomcat-users>
<user username="admin" password="password" roles="manager-gui,admin-gui"/>
</tomcat-users>
Hit Ctrl + X
followed Y
and Enter
to save and exit the file.
By default Tomcat restricts access to Manager and Host manager. So, to allow connections you need to remove the IP restrictions from the corresponding context.xml
files.
For the Manager app the file that needs be updated is:
sudo nano /opt/tomcat/webapps/manager/META-INF/context.xml
For the Host Manager app the file that needs be updated is:
sudo nano /opt/tomcat/webapps/host-manager/META-INF/context.xml
Comment out the value section to remove the IP restriction as shown 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>
Hit Ctrl + X
followed Y
and Enter
to save and exit the file.
Configure Firewall
By default Tomcat runs on port 8080
, So you need to open port 8080
to allow connections.
In your Google Cloud Console go to VPC Network >> Firewall rules and click Create Firewall rules.
In Name enter tomcat
In Targets select All instances in the network
In Source filter select IP ranges
In Source IP ranges enter 0.0.0.0/0
In Protocols and ports check TCP and enter 8080.
Click Create.
Access Web Interface
Now you can access your Tomcat web manager with your external IP address followed by port 8080
.
http://IP_ADDRESS:8080
You will see the Tomcat welcome page.
Tomcat Web Application Manager page.
Tomcat Virtual Host Manager.
Now you have installed Tomcat 9 on Ubuntu 18.04, configured it, opened Firewall port. You can feel free to deploy your Java applications.