Guide to set up Apache2 and Tomcat8.5 on an Ubuntu 18.04?

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:

  1. Connect to the machine
  2. Update apt:
    sudo apt update
  3. Install Apache:
    sudo apt install apache2
  4. Allow Apache through the firewall by first listing applications ufw is aware of:
    sudo ufw app list
    This will give you something like:
    Available applications: Apache Apache Full Apache Secure OpenSSH
    Allow Apache:
    sudo ufw allow 'Apache'
    Confirm the status:
    sudo ufw status
    You should see something similar to this:
    Status: active
    To Action From
    -- ------ ----
    OpenSSH ALLOW Anywhere
    Apache ALLOW Anywhere
    OpenSSH (v6) ALLOW Anywhere (v6)
    Apache (v6) ALLOW Anywhere (v6)
  5. 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)

  1. Connect to the machine
  2. Update apt
    sudo apt update

Now let's install Tomcat 8.5:

  1. Install the Java Development Kit:
    sudo apt-get install default-jdk
  2. Create a group and user for Tomcat to use:
    sudo groupadd tomcat
    sudo useradd -s /bin/false -g tomcat -d /opt/tomcat tomcat
  3. 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 
  4. 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
  5. 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/
  6. Next we need to get the current location of Java:
    sudo update-java-alternatives -l
    You will see something like this:
    java-1.8.0-openjdk-amd64 1081 /usr/lib/jvm/java-1.8.0-openjdk-amd64
    The path will be needed for JAVA_HOME, which will be set in the next step.
  7. Create a systemd .service file for Tomcat using an editor of your choice:
    sudo vi /etc/systemd/system/tomcat.service
    Paste the following into the file:
    [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
    Remember to change 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.
  8. Reload the systemd daemon:
    sudo systemctl daemon-reload
  9. Start Tomcat:
    sudo systemctl start tomcat
    Confirm it's working:
    sudo systemctl status tomcat
  10. Allow Tomcat through the firewall:
    sudo ufw allow 8080
    And then test the installation with
  11. Set Tomcat to automatically start at boot:
    sudo systemctl enable tomcat
  12. Now to configure the management interface. Open the configuration XML file and set a decent login and password:
    sudo vi /opt/tomcat/conf/tomcat-users.xml
    You'll see something like this:
    <tomcat-users ...> <user username="crazyTown" password="superSecretPassword!123" roles="manager-gui,admin-gui"/>
    </tomcat-users>
    Save the file.
  13. Restart Tomcat:
    sudo service tomcat restart
  14. 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)

  1. Connect to the machine

  2. Update apt

    sudo apt update
  3. Install mod_jk:

    sudo apt install libapache2-mod-jk
  4. Enable the redirect port on Tomcat:

    sudo vi /opt/tomcat/server.xml
  5. Uncomment the line that looks like this:

    <Connector port="8009" protocol="AJP/1.3" redirectPort="8443" />
  6. Create a workers.properties file for Apache:

    sudo vi /etc/apache2/workers.properties
  7. 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
  8. 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
  9. 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
  10. 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.

3

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like