In the next article we will take a look at how can we install tomcat 10 on Ubuntu 20.04. Apache Tomcat works as a servlet container developed under the Jakarta project at the Apache Software Foundation. It is developed and updated by members of the Apache Software Foundation and independent volunteers.
Although today this is not as popular as other servers, Tomcat continues to be useful in many projects. Tomcat requires Java SE 8 or later to be installed in the system so that it works correctly.
How to install Tomcat 10 on Ubuntu 20.04
Install OpenJDK on Ubuntu
As I said lines above, Tomcat requires that Java JDK be installed on our system. For this we can both install Oracle Java JDK as its open source alternative OpenJDK.
For install the OpenJDK that we can find in the Ubuntu repositories, we just have to open a terminal (Ctrl + Alt + T) and execute:
sudo apt update; sudo apt install default-jdk
Once installed, we will only need verify installation checking java version:
Create a user and group for Tomcat
First we are going to create a new group for Tomcat which we are going to call tomcat. We will do this with the command:
sudo groupadd tomcat
Then it’s time to create a new user for Tomcat which we are going to call tomcat. Then we will make it a member of the tomcat group that we created earlier. In addition we will also do / opt / tomcat the home folder for the user we are going to create. To do all this, in the same terminal we will only have to execute:
sudo useradd -s /bin/false -g tomcat -d /opt/tomcat tomcat
At this point, we are ready to download and configure Tomcat. At the time of this writing, the latest released version of the 10 series is 10.0.12, and can be downloaded from the project website.
In addition to using the web browser, we can also get this latest package published today by opening a terminal (Ctrl + Alt + T) and executing the commands in it:
When the download is finished, we will create tomcat home folder in / opt / tomcat. That is where we are going to unzip the downloaded file. To do this we will only have to execute the commands:
sudo mkdir /opt/tomcat
sudo tar xzvf apache-tomcat-10*tar.gz -C /opt/tomcat/
Now we are going to give the Tomcat user control of the entire directory, and we will make all the scripts in the bin location executable:
sudo chown -R tomcat: /opt/tomcat
sudo sh -c 'chmod +x /opt/tomcat/apache-tomcat-10.0.12/bin/*.sh'
Configure the Tomcat service
Now that we have the extracted package in the location we want, we are going to execute the following command to open tomcat configuration file for default user:
sudo vim /opt/tomcat/apache-tomcat-10.0.12/conf/tomcat-users.xml
Inside the file we are going to create an account with password for the user admin and save it inside the file. We can do this by copying and pasting the following lines in the file, just before:
<role rolename="manager-gui"/> <role rolename="admin-gui"/> <user username="admin" password="escribe-la-contraseña-para-admin" roles="manager-gui,admin-gui"/>
After change the option “password” for our password, we save and close the editor. Next, we will execute the following command to create a server account for Tomcat:
sudo vim /etc/systemd/system/tomcat.service
When the editor opens, let’s paste the following lines within. Then we will save the file.
[Unit] Description=Tomcat servlet container After=network.target [Service] Type=forking User=tomcat Group=tomcat Environment="JAVA_HOME=/usr/lib/jvm/default-java" Environment="JAVA_OPTS=-Djava.security.egd=file:///dev/urandom" Environment="CATALINA_BASE=/opt/tomcat/apache-tomcat-10.0.12" Environment="CATALINA_HOME=/opt/tomcat/apache-tomcat-10.0.12" Environment="CATALINA_PID=/opt/tomcat/apache-tomcat-10.0.12/temp/tomcat.pid" Environment="CATALINA_OPTS=-Xms512M -Xmx1024M -server -XX:+UseParallelGC" ExecStart=/opt/tomcat/apache-tomcat-10.0.12/bin/startup.sh ExecStop=/opt/tomcat/apache-tomcat-10.0.12/bin/shutdown.sh [Install] WantedBy=multi-user.target
When we are back in the terminal, we are going to execute the following commands to reload systemd profiles and enable tomcat service:
sudo systemctl daemon-reload sudo systemctl start tomcat.service sudo systemctl enable tomcat.service
After these commands, to check if tomcat is running or not, we will only need to execute:
sudo systemctl status tomcat.service
Start the Tomcat GUI
At this point, it will only be necessary open our browser and go to the local server IP or host name. This should show us the default Tomcat page:
Once in the program interface, you will have to click on the option Manager to login to the backend page. Here we will have how to use as username admin and as password the one we indicate in the file tomcat-users.xml.
If you want to access the Tomcat server remotely, it will be necessary to whitelist the remote IP address to which access will be allowed. To change the address constraints, you will need to open the appropriate context.xml files. For the Manager application, the file to edit will be:
sudo nano /opt/tomcat/apache-tomcat-10.0.12/webapps/manager/META-INF/context.xml
For the Host Manager application, the file to edit would be this:
sudo vim /opt/tomcat/apache-tomcat-10.0.12/webapps/host-manager/META-INF/context.xml
Inside both files, Discuss the restriction of the IP address to allow connections from anywhere. If you want to allow access only to connections coming from your own IP address, you can add your public IP address to the list.
The context.xml files for Tomcat web applications should look similar to the following:
After saving the context.xml files, you need restart Tomcat service running the command:
sudo systemctl restart tomcat