Integrate apache and tomcat to serve java content from the same server

The following guide allows you to serve both php and java from the same apache server. Since apache doesn’t have native support for java files, tomcat needs to be installed. So, these are the steps that need followed:

  1. Download and install java (if not installed). Create symbolic link /usr/local/java that points to the java installation folder
  2. Download and install tomcat (if not installed). Can be downloaded from here: Create symbolic link /usr/local/tomcat that points to the tomcat installation folder
  3. Create /etc/init.d/tomcatwith the following content:
    # Tomcat auto-start
    # description: Auto-starts tomcat
    # processname: tomcat
    # pidfile: /var/run/
    export JAVA_HOME
    export PATH
    case $1 in
            sh /usr/local/tomcat/bin/
            sh /usr/local/tomcat/bin/
            sh /usr/local/tomcat/bin/
            sh /usr/local/tomcat/bin/
    exit 0

    make it executable: sudo chmod 755 /etc/init.d/tomcat

  4. Download the apache module, copy it into the /usr/lib/apache2/modules folder. The module can be downloaded from here: (go to the “Binary Releases” page)
  5. Create /etc/apache2/mods-available/jk.loadwith the following content:
    # Load mod_jk module
    # Update this path to match your modules location
    LoadModule jk_module /usr/lib/apache2/modules/
  6. Create /etc/apache2/mods-available/jk.conf with the following content:
  7. <IfModule mod_jk.c>
      # Where to find
      # Update this path to match the tomcat conf directory location
      JkWorkersFile "/usr/local/tomcat/conf/"
      # Where to put jk shared memory
      # Update this path to match your local state directory or logs directory
      JkShmFile     /var/log/apache2/mod_jk.shm
      # Where to put jk logs
      # Update this path to match your logs directory location (put mod_jk.log next to access_log)
      JkLogFile     /var/log/apache2/mod_jk.log
      # Set the jk log level [debug/error/info]
      JkLogLevel    info
      # Select the timestamp log format
      JkLogStampFormat "[%a %b %d %H:%M:%S %Y] "
  8. Create symbolic links for the jk.load and jk.conf files in the /etc/apache2/mods-enabled folder
  9. Add the following lines to the virtual host configuration for which you want to enable java. If you don’t have virtual hosts configured then you can add the lines to the end of the httpd.conffile:
    <IfModule jk_module>
    	# Send everything for context /java to worker named worker1 (ajp13)
    	JkMount  /java/* worker1
  10. Create /usr/local/tomcat/conf/workers.propertieswith the following content:
    # Define 1 real worker using ajp13
    # Set properties for worker1 (ajp13)
  11. Create /usr/local/tomcat/webapps/java folder. Place here the java servlets or jsp’s that you want to make available
  12. restart apache
  13. start tomcat by calling sudo /etc/init.d/tomcat start

Now you should be able to serve java content from your web server.

Note. If you are using LAMPP instead of the standard apache installation, the add all the apache configuration sections in one place, at the end of the httpd.conf file.

Leave a Reply

Your email address will not be published. Required fields are marked *