Does anyone know a good guide to set up Apache2 and Tomcat8.5 on an Ubuntu 18.04? (Including the Apache Tomcat Connectors: mod_jk)
1 Answer
There are lots of guides online that go over how to accomplish each of these tasks, but it seems many of them leave out one or two steps that then lead to questions on sites like this one. That said, DigitalOcean puts out some of the better walk-throughs for common software packages.
That said, AskUbuntu isn't really a site where one drops links and walks away. Link rot is a real thing and should be avoided when possible. So, with this in mind, let's install some software.
Apache2 on Ubuntu Server 18.04
This is how one would install Apache on an up-to-date Ubuntu 18.04:
- Connect to the machine
- Update
apt
:sudo apt update
- Install Apache:
sudo apt install apache2
- Allow Apache through the firewall by first listing applications
ufw
is aware of:
This will give you something like:sudo ufw app list
Allow Apache:Available applications: Apache Apache Full Apache Secure OpenSSH
Confirm the status:sudo ufw allow 'Apache'
You should see something similar to this:sudo ufw status
Status: active To Action From -- ------ ---- OpenSSH ALLOW Anywhere Apache ALLOW Anywhere OpenSSH (v6) ALLOW Anywhere (v6) Apache (v6) ALLOW Anywhere (v6)
- Test the web server works in the browser by entering the server's IP address. You should see a stock Apache page with the Ubuntu logo and some configuration information.
Source: DigitalOcean
Install Tomcat 8.5
Tomcat 8.5 was the version available with Ubuntu 16.04 and Tomcat 9 was the version available for 18.04. That said, if you really must have 8.5 on your 18.04 server, this is how you can do it.
(Optional Steps — To be performed only if you have disconnected from the server after installing Apache)
- Connect to the machine
- Update
apt
sudo apt update
Now let's install Tomcat 8.5:
- Install the Java Development Kit:
sudo apt-get install default-jdk
- Create a group and user for Tomcat to use:
sudo groupadd tomcat sudo useradd -s /bin/false -g tomcat -d /opt/tomcat tomcat
- Download Tomcat 8.5 from the Apache Tomcat website. As of this writing, 8.5.65 is the most recent version. You can download this via
curl
:curl -O https://downloads.apache.org/tomcat/tomcat-8/v8.5.65/bin/apache-tomcat-8.5.65.tar.gz
- Tomcat will be saved to the
/opt/tomcat
directory, so let's get that set up:sudo mkdir /opt/tomcat sudo tar xzvf apache-tomcat-8*tar.gz -C /opt/tomcat --strip-components=1
- Now let's set the permissions correctly:
cd /opt/tomcat 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/
- Next we need to get the current location of Java:
You will see something like this:sudo update-java-alternatives -l
The path will be needed forjava-1.8.0-openjdk-amd64 1081 /usr/lib/jvm/java-1.8.0-openjdk-amd64
JAVA_HOME
, which will be set in the next step. - Create a systemd
.service
file for Tomcat using an editor of your choice:
Paste the following into the file:sudo vi /etc/systemd/system/tomcat.service
Remember to change[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/jre 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
JAVA_HOME
to the path from the last step, and keep/jre
at the end of the path. Once done, save and close the file. - Reload the systemd daemon:
sudo systemctl daemon-reload
- Start Tomcat:
Confirm it's working:sudo systemctl start tomcat
sudo systemctl status tomcat
- Allow Tomcat through the firewall:
And then test the installation withsudo ufw allow 8080
http://server_domain_or_IP:8080
- Set Tomcat to automatically start at boot:
sudo systemctl enable tomcat
- Now to configure the management interface. Open the configuration XML file and set a decent login and password:
You'll see something like this:sudo vi /opt/tomcat/conf/tomcat-users.xml
Save the file.<tomcat-users ...> <user username="crazyTown" password="superSecretPassword!123" roles="manager-gui,admin-gui"/> </tomcat-users>
- Restart Tomcat:
sudo service tomcat restart
- Test the web interface again in the browser.
Source: DigitalOcean
mod_jk for Apache
This is the final step.
(Optional Steps — To be performed only if you have disconnected from the server after installing Apache)
Connect to the machine
Update
apt
sudo apt update
Install
mod_jk
:sudo apt install libapache2-mod-jk
Enable the redirect port on Tomcat:
sudo vi /opt/tomcat/server.xml
Uncomment the line that looks like this:
<Connector port="8009" protocol="AJP/1.3" redirectPort="8443" />
Create a
workers.properties
file for Apache:sudo vi /etc/apache2/workers.properties
Paste the following into the new file:
# Define one worker using AJP13 worker.list=worker1 # Set the properties worker.worker1.type=ajp13 worker.worker1.host=localhost worker.worker1.port=8009
Instruct Apache to use the file:
sudo vi /etc/apache2/mods-available/jk.conf
Find the
JkWorkersFile
property and set it to the file you just created:JkWorkersFile /etc/apache2/workers.properties
Finally, ensure Apache passes through to Tomcat:
sudo vi /etc/apache2/sites-enabled/000-default.conf
Then add this to your
<VirtualHost *:80>
configuration:JkMount /api worker1
Restart Tomcat and Apache:
sudo service tomcat restart sudo service apache2 restart
Source: My endless configuration notes in Evernote ...
This should give you everything you need to get these three pieces of software working together. This was tested in a VirtualBox VM running Ubuntu Server 18.04.5 LTS.
-
brilliant, thank you. I will give this a try. (I will need some time implement) May 8, 2021 at 13:53
-
I think I am almost there, but I see this in my apache2 logging. "client denied by server configuration: /var/lib/tomcat8/webapps/ROOT/" And in browser "Forbidden You don't have permission to access this resource. Apache/2.4.29 (Ubuntu) Server at <mydomian> Port 80" What do you think? (my paths are different from the guide but thats ok) May 8, 2021 at 20:01
-
Yeah! "Hello world!".. ha ha. So I got it working. For some reason I had to change "JkMount /api worker1" into "JkMount /* worker1" for it all to click together. I was wondering about this "api". But any way thanks a lot for your help. I don't know if this "api" > "*" needs to be changed in the answer or if this is my situation. Thanks again. May 8, 2021 at 21:49