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 libapache2-mod-jk
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 vi /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 vi /etc/apache2/sites-available/java-school.net.conf
<VirtualHost *:80> # The ServerName directive sets the request scheme, hostname and port that # the server uses to identify itself. This is used when creating # redirection URLs. In the context of virtual hosts, the ServerName # specifies what hostname must appear in the request's Host: header to # match this virtual host. For the default virtual host (this file) this # value is not decisive as it is used as a last resort host regardless. # However, you must set it for any further virtual host explicitly. ServerName java-school.net ServerAlias www.java-school.net ServerAdmin admin@java-school.net DocumentRoot /var/www/java-school/html # Available loglevels: trace8, ..., trace1, debug, info, notice, warn, # error, crit, alert, emerg. # It is also possible to configure the loglevel for particular # modules, e.g. #LogLevel info ssl:warn ErrorLog ${APACHE_LOG_DIR}/error.log CustomLog ${APACHE_LOG_DIR}/access.log combined # For most configuration files from conf-available/, which are # enabled or disabled at a global level, it is possible to # include a line for only one particular virtual host. For example the # following line enables the CGI configuration for this host only # after it has been globally disabled with "a2disconf". #Include conf-available/serve-cgi-bin.conf </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://localhost to confirm that the index.html is running.
Integrating Apache Web Server with Tomcat
Open the workers.properties file and modify the workers.tomcat_home and workers.java_home settings.
Confirm tomcat_home
/usr/share/tomcat10/bin/version.sh Using CATALINA_BASE: /usr/share/tomcat10 Using CATALINA_HOME: /usr/share/tomcat10 Using CATALINA_TMPDIR: /usr/share/tomcat10/temp Using JRE_HOME: /usr Using CLASSPATH: /usr/share/tomcat10/bin/bootstrap.jar:/usr/share/tomcat10/bin/tomcat-juli.jar Using CATALINA_OPTS: Server version: Apache Tomcat/10.1.16 (Ubuntu) Server built: Dec 3 2023 12:31:22 UTC Server number: 10.1.16.0 OS Name: Linux OS Version: 6.11.0-26-generic Architecture: amd64 JVM Version: 21.0.7+6-Ubuntu-0ubuntu124.04 JVM Vendor: Ubuntu
Confirm java_home
mvn -v Apache Maven 3.8.7 Maven home: /usr/share/maven Java version: 21.0.7, vendor: Ubuntu, runtime: /usr/lib/jvm/java-21-openjdk-amd64 Default locale: ko_KR, platform encoding: UTF-8 OS name: "linux", version: "6.11.0-26-generic", arch: "amd64", family: "unix"
sudo vi /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=/usr/share/tomcat10 # # 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-21-openjdk-amd64 # # Defining a worker named ajp13_worker and of type ajp13 # Note that the name and the type do not have to match. # worker.ajp13_worker.port=8009 worker.ajp13_worker.host=localhost worker.ajp13_worker.type=ajp13 # # Specifies the load balance factor when used with # a load balancing worker. # Note: # ----> lbfactor must be > 0 # ----> Low lbfactor means less work done by the worker. worker.ajp13_worker.lbfactor=1 worker.ajp13_worker.secret=testkey # # Specify the size of the open connection cache. #worker.ajp13_worker.cachesize # #------ DEFAULT LOAD BALANCER WORKER DEFINITION ---------------------- #--------------------------------------------------------------------- # # # The loadbalancer (type lb) workers perform wighted round-robin # load balancing with sticky sessions. # Note: # ----> If a worker dies, the load balancer will check its state # once in a while. Until then all work is redirected to peer # workers. worker.loadbalancer.type=lb worker.loadbalancer.balance_workers=ajp13_worker
Add the following to the virtual hosts.
sudo vi /etc/apache2/sites-available/java-school.net.conf
ServerAdmin javaschool@gmail.org ServerName java-school.net ServerAlias www.java-school.net DocumentRoot /var/www/java-school.net/html JkMount /* ajp13_worker
Open the server.xml file.
sudo vi /etc/tomcat10/server.xml
<!-- Define an AJP 1.3 Connector on port 8009 --> <Connector port="8009" protocol="AJP/1.3" redirectPort="8443" /> address="0.0.0.0" secret="testkey" port="8009" redirectPort="8443" maxParameterCount="1000" />
Add the jvmRoute="ajp13_worker" as shown below.
<Engine name="Catalina" defaultHost="localhost" jvmRoute="ajp13_worker">
Restart apache and tomcat.
sudo service apache2 restart sudo service tomcat10 restart
Visit http://localhost to see if Tomcat's root application is responding.
Copy static elements to the /var/www/java-school.net/html
sudo cp favicon.ico /var/www/java-school.net/html/ sudo cp -r static/ /var/www/java-school.net/html/
Modify the virtual host file so that Apache serves the static elements of the Tomcat root application.
sudo vi /etc/apache2/sites-available/java-school.net.conf
ServerAdmin javaschool@gmail.org ServerName java-school.net ServerAlias www.java-school.net DocumentRoot /var/www/java-school.net/html JkUnMount /favicon.ico ajp13_worker JkUnMount /static/* ajp13_worker JkMount /* ajp13_worker
sudo service apache2 restart sudo service tomcat10 restart
Revisit http://localhost.
References