Last Modified 2019.5.30

How To Install the Apache on Ubuntu

To follow the steps below, you need to have Java and Tomcat installed.

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 file:

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.

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/javaschool/src/main/webapp/favicon.ico favicon.ico
ln -s /home/kim/javaschool/src/main/webapp/resources resources
ln -s /home/kim/javaschool/src/main/webapp/examples examples

Modify the virtual host file so that Apache serves the static elements of the Tomcat root application as follows:

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 /resources/* ajp13_worker
	JkUnMount /examples/* ajp13_worker
	JkMount /* ajp13_worker
</VirtualHost>

Restart Apache.

sudo service apache2 restart

Restart Tomcat

sudo service tomcat9 restart

Visit http://127.0.0.1 again.

References