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: http://tomcat.apache.org/download-70.cgi. 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/tomcat.pid
    
    JAVA_HOME=/usr/local/java
    export JAVA_HOME
    PATH=$PATH:$JAVA_HOME/bin
    export PATH
    
    case $1 in
    start)
            sh /usr/local/tomcat/bin/startup.sh
            ;;
    stop)
            sh /usr/local/tomcat/bin/shutdown.sh
            ;;
    restart)
            sh /usr/local/tomcat/bin/shutdown.sh
            sh /usr/local/tomcat/bin/startup.sh
            ;;
    esac
    exit 0

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

  4. Download the mod_jk.so apache module, copy it into the /usr/lib/apache2/modules folder. The module can be downloaded from here: http://tomcat.apache.org/download-connectors.cgi (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/mod_jk.so
  6. Create /etc/apache2/mods-available/jk.conf with the following content:
  7. <IfModule mod_jk.c>
      # Where to find workers.properties
      # Update this path to match the tomcat conf directory location
      JkWorkersFile "/usr/local/tomcat/conf/workers.properties"
    
      # 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] "
    </IfModule>
  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
    </IfModule>
  10. Create /usr/local/tomcat/conf/workers.propertieswith the following content:
    # Define 1 real worker using ajp13
    worker.list=worker1
    # Set properties for worker1 (ajp13)
    worker.worker1.type=ajp13
    worker.worker1.host=localhost
    worker.worker1.port=8009
  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 *