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
ufwis 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 statusStatus: 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
aptsudo 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 - Tomcat will be saved to the
/opt/tomcatdirectory, 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-amd64JAVA_HOME, which will be set in the next step. - Create a systemd
.servicefile 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.targetJAVA_HOMEto the path from the last step, and keep/jreat 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 tomcatsudo systemctl status tomcat - Allow Tomcat through the firewall:
And then test the installation withsudo ufw allow 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
aptsudo apt updateInstall
mod_jk:sudo apt install libapache2-mod-jkEnable the redirect port on Tomcat:
sudo vi /opt/tomcat/server.xmlUncomment the line that looks like this:
<Connector port="8009" protocol="AJP/1.3" redirectPort="8443" />Create a
workers.propertiesfile for Apache:sudo vi /etc/apache2/workers.propertiesPaste 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=8009Instruct Apache to use the file:
sudo vi /etc/apache2/mods-available/jk.confFind the
JkWorkersFileproperty and set it to the file you just created:JkWorkersFile /etc/apache2/workers.propertiesFinally, ensure Apache passes through to Tomcat:
sudo vi /etc/apache2/sites-enabled/000-default.confThen add this to your
<VirtualHost *:80>configuration:JkMount /api worker1Restart 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.
3