Skip to content

Instantly share code, notes, and snippets.

@heme
Last active April 29, 2020 10:44
Show Gist options
  • Save heme/7383af9f6391fdc137b9 to your computer and use it in GitHub Desktop.
Save heme/7383af9f6391fdc137b9 to your computer and use it in GitHub Desktop.
OSX: Apache & Tomcat Auto Start Port 80 thru AJP

OSX: Apache & Tomcat Auto Start Port 80 thru AJP

Apache

Configure Apache

  1. Backup original httpd.conf cp /etc/apache2/httpd.conf /etc/apache2/httpd.bkp.conf
  2. Create your own apache dir mkdir ~/{PATH}/apache/ ~/{PATH}/apache/conf ~/{PATH}/apache/htdocs ~/{PATH}/apache/logs
  3. Copy httpd.conf to your apache conf dir cp /etc/apache2/httpd.conf ~/{PATH}/apache/conf/
  4. Copy http-vhost.conf to your apache conf dir cp /etc/apache2/extra/httpd-vhost.conf ~/{PATH}/apache/conf/
  5. Edit your new ~/{PATH}/apache/conf/httpd.conf file
    1. Enable (uncomment) the php5_module
    2. Change ServerAdmin to your email
    3. Change DocumentRoot to ~/{PATH}/apache/htdocs
    4. Change the root Directive to <Directory "~/{PATH}/apache/htdocs">
    5. Add index.php to DirectoryIndex
    6. Enable (uncomment) the Virtual hosts config include and change the path to your conf file at ~/{PATH}/apache/conf/httpd-vhost.conf
  6. Make apache's main conf file do nothing other than include your conf file echo -n "Include ~/{PATH}/apache/conf/httpd.conf" > /etc/apache2/httpd.conf
  7. Restart Apache: sudo apachectl restart
  8. You should see an empty directory listing at http://localhost

Auto Start Apache on Boot

  • Auto Start Permanently (-w): sudo launchctl load -w /System/Library/LaunchDaemons/org.apache.httpd.plist
  • Remove Auto Start sudo launchctl unload -w /System/Library/LaunchDaemons/org.apache.httpd.plist

All Apache Commands:

    cp /etc/apache2/httpd.conf /etc/apache2/httpd.bkp.conf
    mkdir ~/{PATH}/apache/ ~/{PATH}/apache/conf ~/{PATH}/apache/htdocs ~/{PATH}/apache/logs
    cp /etc/apache2/httpd.conf ~/{PATH}/apache/conf/
    cp /etc/apache2/extra/httpd-vhost.conf ~/{PATH}/apache/conf/
    echo -n "Include ~/{PATH}/apache/conf/httpd.conf" > /etc/apache2/httpd.conf
    sudo launchctl load -w /System/Library/LaunchDaemons/org.apache.httpd.plist
    sudo vi ~/{PATH}/apache/conf/httpd.conf

Edit your custom http.conf file (#5)

    sudo apachectl restart

Tomcat

Install Tomcat

  1. Install the Homebrew Package Manager http://brew.sh/#install
  2. Install tomcat via Homebrew: brew install tomcat

Configure Tomcat

  1. Create your own tomcat dir mkdir ~/{PATH}/tomcat/ ~/{PATH}/tomcat/conf ~/{PATH}/tomcat/bin ~/{PATH}/tomcat/logs

  2. Allow group write on logs & files inherit directory permissions sudo chmod 775 ~/{PATH}/tomcat/logs sudo chmod +a "admin allow list,add_file,search,add_subdirectory,delete_child,readattr,writeattr,readextattr,writeextattr,readsecurity,file_inherit,directory_inherit" ~/{PATH}/tomcat/logs

  3. Edit webapps directory to Catalina Home in server.xml sudo vi ~/{PATH}/tomcat/conf/server.xml

    This Line

     <Host name="localhost"  appBase="${catalina.home}/webapps"
    
  4. Create global CATALINA environment variables at startup

     sudo touch /etc/launchd.conf
     sudo vi /etc/launchd.conf
    

    Add These lines:

     setenv CATALINA_HOME /{original tomcat install dir i.e. which catalina}
     setenv CATALINA_BASE ~/{PATH}/tomcat
    

Auto Start Tomcat on Boot

  1. Create tomcat Launch Daemon script

     sudo touch ~/{PATH}/tomcat/bin/tomcat-launchd.sh
     sudo chown root:admin ~/{PATH}/tomcat/bin/tomcat-launchd.sh
     sudo chmod a+x ~/{PATH}/tomcat/bin/tomcat-launchd.sh
     sudo vi ~/{PATH}/tomcat/bin/tomcat-launchd.sh
    

    Content:

     #!/bin/bash
     # NOTE: this is an OSX launchd wrapper shell script for Tomcat (to be placed in $CATALINA_HOME/bin)
    
     function shutdown() {
         date
         echo "Shutting down Tomcat"
         $CATALINA_HOME/bin/catalina.sh stop
     }
    
     date
     echo "Starting Tomcat"
     export CATALINA_PID=/tmp/$$
    
     # Uncomment to increase Tomcat's maximum heap allocation
     # export JAVA_OPTS=-Xmx512M $JAVA_OPTS
    
     . $CATALINA_HOME/bin/catalina.sh start
    
     # Allow any signal which would kill a process to stop Tomcat
     trap shutdown HUP INT QUIT ABRT KILL ALRM TERM TSTP
    
     echo "Waiting for `cat $CATALINA_PID`"
     wait `cat $CATALINA_PID`
    
  2. Create property list file for tomcat

     sudo touch /Library/LaunchDaemons/org.apache.tomcat.plist
     sudo vi /Library/LaunchDaemons/org.apache.tomcat.plist
    

    Content:

     <?xml version="1.0" encoding="UTF-8"?>
     <!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
     <plist version="1.0">
         <dict>
             <key>Label</key>
             <string>org.apache.tomcat</string>
             <key>OnDemand</key>
             <false/>
             <key>RunAtLoad</key>
             <true/>
             <key>ProgramArguments</key>
             <array>
                 <string>~/{PATH}/tomcat/bin/tomcat-launchd.sh</string>
             </array>
             <key>ServiceDescription</key>
             <string>Tomcat</string>
             <key>StandardErrorPath</key>
             <string>~/{PATH}/tomcat/logs/tomcat-launchd-errors.log</string>
             <key>StandardOutPath</key>
             <string>~/{PATH}/tomcat/logs/tomcat-launchd-output.log</string>
             <key>UserName</key>
             <string>root</string>
         </dict>
     </plist>
    
  3. Load the property list file as a Launch Daemon at startup

    • Auto Start Permanently (-w): sudo launchctl load -w /System/Library/LaunchDaemons/org.apache.tomcat.plist
    • Remove Auto Start sudo launchctl unload -w /System/Library/LaunchDaemons/org.apache.tomcat.plist

All Tomcat Commands:

    mkdir ~/{PATH}/tomcat/ ~/{PATH}/tomcat/conf ~/{PATH}/tomcat/bin ~/{PATH}/tomcat/logs
    sudo chmod 775 ~/{PATH}/tomcat/logs
    sudo chmod +a "admin allow list,add_file,search,add_subdirectory,delete_child,readattr,writeattr,readextattr,writeextattr,readsecurity,file_inherit,directory_inherit" ~/{PATH}/tomcat/logs
    sudo vi ~/{PATH}/tomcat/conf/server.xml

Edit your custom server.xml file (#3)

    sudo touch /etc/launchd.conf
    sudo vi /etc/launchd.conf

Edit launchd.conf (#4)

    sudo touch ~/{PATH}/tomcat/bin/tomcat-launchd.sh
    sudo chown root:admin ~/{PATH}/tomcat/bin/tomcat-launchd.sh
    sudo chmod a+x ~/{PATH}/tomcat/bin/tomcat-launchd.sh
    sudo vi ~/{PATH}/tomcat/bin/tomcat-launchd.sh

Create tomcat-launchd.sh (#1)

    sudo touch /Library/LaunchDaemons/org.apache.tomcat.plist
    sudo vi /Library/LaunchDaemons/org.apache.tomcat.plist

Create launch deamon (#2)

    sudo launchctl load -w /System/Library/LaunchDaemons/org.apache.tomcat.plist

AJP Connector (Apache as proxy on :80)

Connect Tomcat to Apache

  1. Add an entry to your hosts file for tomcat sudo vi /etc/hosts Add line:

     127.0.0.1   tomcat
    
  2. Edit your http-vhosts.conf file sudo vi ~/{PATH}/apache/conf/http-vhosts.conf Content:

     NameVirtualHost *:80
    
     # Default
     <VirtualHost _default_:80>
         ServerName localhost
     </VirtualHost>
    
     # TOMCAT
     <VirtualHost *:80>
         ServerName tomcat
         ProxyPass / ajp://localhost:8009/
         ErrorLog "~/{PATH}/apache/log/tomcat-error_log"
         CustomLog "~/{PATH}/apache/log/tomcat-access_log" common
     </VirtualHost>
    
  3. Restart apache sudo apachectl restart

  4. Verify that http://tomcat loads the Tomcat management screens

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment