Last Modified 2022.2.8

How To Install the Apache on Ubuntu

You need to have Java and Tomcat installed to follow the steps below.

Installing Apache

sudo apt update
sudo apt install apache2

Access http://localhost to confirm that the software is running properly.

Setting Up Virtual Hosts

Create the directory for java-school.net, using the -p flag to create any necessary parent directories:

sudo mkdir -p /var/www/java-school.net/html

Assign ownership of the directory:

sudo chown -R $USER:$USER /var/www/java-school.net/html

Create a sample index.html.

sudo nano /var/www/java-school.net/html/index.html
<html>
<body>
Welcome to java-school.net
</body>
</html>

Make a new virtual host file at /etc/apache2/sites-available/java-school.net.conf.

sudo nano /etc/apache2/sites-available/java-school.net.conf
<VirtualHost 127.0.0.1>
  ServerAdmin javaschool@gmail.org
  ServerName java-school.net
  ServerAlias www.java-school.net
  DocumentRoot /var/www/java-school.net/html
  ErrorLog ${APACHE_LOG_DIR}/error.log
  CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

Enable the file with a2ensite:

sudo a2ensite java-school.net.conf

Disable the default site defined in 000-default.conf.

sudo a2dissite 000-default.conf

Restart Apache to implement your changes.

sudo service apache2 restart

Access http://127.0.0.1 to confirm that the index.html is running.

Installing Tomcat Connector

sudo apt install libapache2-mod-jk

Open the workers.properties file and modify the workers.tomcat_home and workers.java_home settings. tomcat_home is /etc/tomcat9 if you install tomcat9 using apt install command. java_home can be checked using mvn -v or sudo update-alternatives --config java commands.

sudo nano /etc/libapache2-mod-jk/workers.properties
#
# workers.tomcat_home should point to the location where you
# installed tomcat. This is where you have your conf, webapps and lib
# directories.
#
workers.tomcat_home=/etc/tomcat9

#
# workers.java_home should point to your Java installation. Normally
# you should have a bin and lib directories beneath it.
#
workers.java_home=/usr/lib/jvm/java-11-openjdk-amd64

Add the following to the virtual hosts.

sudo nano /etc/apache2/sites-available/java-school.net.conf
<VirtualHost 127.0.0.1>
  ServerAdmin javaschool@gmail.org
  ServerName java-school.net
  ServerAlias www.java-school.net
  DocumentRoot /var/www/java-school.net/html
  ErrorLog ${APACHE_LOG_DIR}/error.log
  CustomLog ${APACHE_LOG_DIR}/access.log combined
  JkMount /* ajp13_worker	
</VirtualHost>

Open the server.xml file.

sudo nano /etc/tomcat9/server.xml 

Uncomment as follows.

<!-- Define an AJP 1.3 Connector on port 8009 -->
<Connector port="8009" protocol="AJP/1.3" redirectPort="8443" />

Restart Apache to implement your changes.

sudo service apache2 restart

Restart Tomcat to implement your changes.

sudo service tomcat9 restart

Visit http://127.0.0.1 to see if Tomcat's root application is responding.

Changing the ROOT Application

git clone https://github.com/kimjonghoon/spring-thymeleaf

Edit pom.xml as follows.

  <build>
    <finalName>spring-thymeleaf</finalName>
    <pluginManagement>
      <plugins>
        <plugin>
          <artifactId>maven-compiler-plugin</artifactId>
          <version>3.8.1</version>
          <configuration>
            <source>${jdk.version}</source>
            <target>${jdk.version}</target>
            <encoding>UTF-8</encoding>
          </configuration>
        </plugin>
        <plugin>
          <artifactId>maven-clean-plugin</artifactId>
          <version>3.1.0</version>
          <configuration>
            <filesets>
              <fileset>
                <directory>src/main/webapp/WEB-INF/classes</directory>
              </fileset>
              <fileset>
                <directory>src/main/webapp/WEB-INF/lib</directory>
              </fileset>
            </filesets>
          </configuration>
        </plugin>
      </plugins>
    </pluginManagement>
  </build>
	
  <!-- 
  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.8.1</version>
        <configuration>
          <source>${jdk.version}</source>
          <target>${jdk.version}</target>
          <encoding>UTF-8</encoding>
        </configuration>
      </plugin>
      <plugin>
        <groupId>org.eclipse.jetty</groupId>
        <artifactId>jetty-maven-plugin</artifactId>
        <version>10.0.0</version>
      </plugin>
    </plugins>
  </build>
  -->
mvn clean compile war:inplace
sudo service tomcat9 stop
sudo nano /etc/tomcat9/Catalina/localhost/ROOT.xml
<?xml version="1.0" encoding="UTF-8"?>
<Context
  docBase="/home/kim/spring-thymeleaf/src/main/webapp"
  reloadable="true">
</Context>

Go to the /var/www/java-school.net/html directory and create symbolic links to the static elements in the Tomcat root application.

cd /var/www/java-school.net/html
ln -s /home/kim/spring-thymeleaf/src/main/webapp/favicon.ico favicon.ico
ln -s /home/kim/spring-thymeleaf/src/main/webapp/static static

Modify the virtual host file so that Apache serves the static elements of the Tomcat root application.

sudo nano /etc/apache2/sites-available/java-school.net.conf
<VirtualHost 127.0.0.1>
  ServerAdmin javaschool@gmail.org
  ServerName java-school.net
  ServerAlias www.java-school.net
  DocumentRoot /var/www/java-school.net/html
  ErrorLog ${APACHE_LOG_DIR}/error.log
  CustomLog ${APACHE_LOG_DIR}/access.log combined
  JkUnMount /favicon.ico ajp13_worker
  JkUnMount /static/* ajp13_worker
  JkMount /* ajp13_worker
</VirtualHost>

Restart Apache.

sudo service apache2 restart

Restart Tomcat.

sudo service tomcat9 restart

Revisit http://127.0.0.1.

References