Apache Tomcat is an open-source server for Java servlets. If you need to serve some java apps you should take a look to the newest Tomcat 9. It’s easy to use and set up.
Installation
Java
First of all we need to check if Java is installed. Just run the following command:
$ java -version
. If you see lines like openjdk version "1.8.0_171"
you are on the right way (Java version must be 8.0 and later). If don’t, you should install (or update) Java e.g:
$ sudo apt-get install default-jdk
Tomcat user
Previous Tomcat versions (such as eighth) can be installed via apt-get
util. But here we gonna use newer version 9 so there are some preliminary steps:
First, create a tomcat9
group (the name can be arbitrary)
$ sudo groupadd tomcat9
Then, create a tomcat9
user that will be a member of tomcat9
group we’ve created before and will have home directory of /opt/tomcat9
(you can variate directory) with a shell of /bin/false
to close access of logging into the account.
$ sudo useradd -s /bin/false -g tomcat9 -d /opt/tomcat9 tomcat9
Tomcat installing
Go to the Apache Tomcat 9 Download Page, scroll down to Binary Distributions
and copy link for tar.gz
archive.
In terminal of your server (or the local machine) make cd
to temp folder (or mkdir
it) e.g:
$ sudo mkdir ~/tmp && cd ~/tmp
Download archive using wget
or curl
:
$ wget {copied link to Tomcat 9 tar.gz archive}
Let’s install Tomcat9 to the /opt/tomcat9
directory, but you can variate it. For extracting the Tomcat archive we need to know its name. Use $ ls
to see a list of files of current directory (should be tmp
). You will see something like apache-tomcat-9.0.8.tar.gz
.
Create the directory and extract the archive into that:
$ sudo mkdir /opt/tomcat9
$ sudo tar -ezfv apache-tomcat-9.0.8.tar.gz -C /opt/tomcat9
Configure permissions
We’ve created tomcat9
user and it needs to have an access to the Tomcat folder.
Change directory to unpacked Tomcat 9:
$ cd /opt/tomcat9
Give the tomcat9
group ownership over the entire directory:
$ sudo chgrp -R tomcat9 /opt/tomcat9
Also make the conf
directory readable and its content executable:
$ sudo chmod -R g+r conf
$ sudo chmod g+x conf
And the last step – make the tomcat9
user the owner of some directories:
$ sudo chown -R tomcat webapps/ work/ temp/ logs/
Create a service
It will be convenient to have Tomcat as a service so wee need to create it.
Tomcat needs to know location of Java. This path is commonly referred to as “JAVA_HOME”. One of the ways to know that is run this command:
$ sudo update-java-alternatives -l
Output:
java-1.8.0-openjdk-amd64 1081 /usr/lib/jvm/java-1.8.0-openjdk-amd64
We are interested in the last column: /usr/lib/jvm/java-1.8.0-openjdk-amd64
(may be you’ll get some different result, take a care).
Create and open a file of the service:
$ sudo nano /etc/systemd/system/tomcat9.service
Here you should paste the following content but with your JAVA_HOME that we’ve got above:
[Unit]
Description=Apache Tomcat Web Application Container
After=network.target
[Service]
Type=forking
Environment=JAVA_HOME=/usr/lib/jvm/java-1.8.0-openjdk-amd64
Environment=CATALINA_PID=/opt/tomcat9/temp/tomcat9.pid
Environment=CATALINA_HOME=/opt/tomcat9
Environment=CATALINA_BASE=/opt/tomcat9
Environment='CATALINA_OPTS=-Xms512M -Xmx1024M -server -XX:+UseParallelGC'
Environment='JAVA_OPTS=-Djava.awt.headless=true -Djava.security.egd=file:/dev/./urandom'
ExecStart=/opt/tomcat9/bin/startup.sh
ExecStop=/opt/tomcat9/bin/shutdown.sh
User=tomcat9
Group=tomcat9
UMask=0007
RestartSec=10
Restart=always
[Install]
WantedBy=multi-user.target
Then save the file and exit.
Here we go! To say the system update services run the following command:
$ sudo systemctl daemon-reload
Enable service to restarting at boot:
$ sudo systemctl enable tomcat9
Start Tomcat service:
$ sudo service tomcat9 start
Make sure that Tomcat is running:
$ sudo service tomcat9 status
You should see in one of lines something like Active: active (running)
.
Configuration
Server configuration file is located in conf
folder. Let’s change it:
$ sudo nano /opt/tomcat9/conf/server.xml
In the middle of file you’ll find the following lines:
<Connector port="8080" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443" />
If you want to change the port Tomcat is running at, change the port
attribute. Make sure that it doesn’t override another service ports.
In the end of the file you’ll find hosts configurations. Here you can add your custom hosts. For example I need to serve my application and make it accessible on subdomain. Then I create the following host configuration:
<Host name="apps.blizzed.ru" appBase="{path_to_apps_folder}"
unpackWARs="true" autoDeploy="true">
...
</Host>
Save the file and exit.
If you changed the Tomcat port you have to add it to the Firewall. Example for port 12345
:
$ sudo ufw allow 12345
After every change of configuration file tomcat9
service should be restarted:
$ sudo service tomcat9 restart
Now you have installed and configured Tomcat 9 on your machine! You can add your first war
in path_to_apps_folder
and look onto results. Don’t forget that start point of every application must be called ROOT.war
.
Kind regards,
BlizzedRu