Search code examples
apachecoldfusionhomebrewapple-m1mod-jk

How do I compile Tomcat mod_jk on a MacBook Pro M1 chip?


I have Homebrew Apache installed and trying to connect Coldfusion Server 2016 with Tomcat mod_jk.

I downloaded the source code from https://tomcat.apache.org/download-connectors.cgi

I followed the directions to compile it, tried few different ways, but when I get to the "make" command, I keep getting the same error:

In file included from jk_ajp12_worker.c:26:
In file included from ./jk_ajp12_worker.h:26:
In file included from ./jk_logger.h:26:
In file included from ./jk_global.h:340:
./jk_types.h:56:2: error: Can not determine the proper size for pid_t
#error Can not determine the proper size for pid_t
 ^
./jk_types.h:62:2: error: Can not determine the proper size for pthread_t
#error Can not determine the proper size for pthread_t
 ^
2 errors generated.
make[1]: *** [jk_ajp12_worker.lo] Error 1
make: *** [all-recursive] Error 1

These are the different commands I've tried to compile:

./configure --with-apxs=/opt/homebrew/bin/apxs
./configure CFLAGS='-arch arm64e' APXSLDFLAGS='-arch arm64e' --with-apxs=/opt/homebrew/bin/apxs
./configure CFLAGS='-arch arm64e' APXSLDFLAGS='-arch arm64e' --with-apxs=/opt/homebrew/bin/apxs --host=arm

I recently got this new MacBook Pro 16" and migrated everything over from my 2017 MacBook Pro (Intel chip). I was running stock Apache with Coldfusion Server 2016, but when I tried to start up Apache on the new MacBook, it didn't like my mod_jk.so file and threw an error:

httpd: Syntax error on line 542 of /opt/homebrew/etc/httpd/httpd.conf: Syntax error on line 2 of /opt/homebrew/etc/httpd/mod_jk.conf: 
Cannot load /Applications/ColdFusion2016/config/wsconfig/2/mod_jk.so into server: dlopen(/Applications/ColdFusion2016/config/wsconfig/2/mod_jk.so, 0x000A): 
tried: '/Applications/ColdFusion2016/config/wsconfig/2/mod_jk.so' 
(mach-o file, but is an incompatible architecture (have 'x86_64', need 'arm64e'))

I appreciate any help or input. Thank you.


Solution

  • I've finally installed Apache2 with Tomcat on my M1 and it all works. The one thing you must do is to install a fresh Apache from Macports or HomeBrew. This is because most old installations copied from your old mac to your new one will now be in the read-only part of your file system and SIP won't let you near them. You will find weird and wonderful workarounds (apachectl told me I had to codesign mod_jk.so for example and I wasted a lot of time doing it and in the end it was pointless) and you will attempt to get the old installation to work, but trust me it's not worth it.

    You will need to compile a fresh jk_module (mod_jk.so). This is what I did:

    Download latest connector https://dlcdn.apache.org/tomcat/tomcat-connectors/jk/tomcat-connectors-1.2.48-src.tar.gz, save the .gz and unzip it.

    Change directory to the native folder. run which apxs to tell you the path to apxs for the ./configure command The path mine gave was: /opt/local/bin/apxs. Use it as the path in the ./configure command below.

    The commands are as follows (actually don't bother running them yet because they will fail):

    1. ./configure --with-apxs=/opt/local/bin/apxs
    2. make

    However make will fail with:

    ./jk_types.h:56:2: error: Can not determine the proper size for pid_t
    #error Can not determine the proper size for pid_t
     ^
    ./jk_types.h:62:2: error: Can not determine the proper size for pthread_t
    #error Can not determine the proper size for pthread_t
     ^
    2 errors generated.
    make[1]: *** [jk_ajp12_worker.lo] Error 1
    make: *** [all-recursive] Error 1
    

    This is a problem for M1 macs that has been fixed. So for the moment we will abandon the 1.2.48 source and download the source with the fix. But don't delete the 1.2.48 source because the fix source is missing a few files which you will copy straight over from the 1.2.48 source.

    The page to download the fix for Mac OS is here: https://github.com/apache/tomcat-connectors, which is commit e719874 on Jun 30, 2021.

    Click on the green 'Code' button and then on 'Download ZIP'. Unzip the new source and cd to 'native'

    Run the commands:

    1. ./configure --with-apxs=/opt/local/bin/apxs
    2. make

    And whenever it stops and complains that something is missing, find it in the 1.2.48 source and copy it over to the same position in the new source and try again. It will happen two or three times.

    I got this error at one point:

    /home/myuser/source/mod_auth_cas/mod_auth_cas/missing: line 81: aclocal-1.15: command not found
    WARNING: 'aclocal-1.15' is missing on your system.
             You should only need it if you modified 'acinclude.m4' or
             'configure.ac' or m4 files included by 'configure.ac'.
             The 'aclocal' program is part of the GNU Automake package:
             <http://www.gnu.org/software/automake>
             It also requires GNU Autoconf, GNU m4 and Perl in order to run:
             <http://www.gnu.org/software/autoconf>
             <http://www.gnu.org/software/m4/>
             <http://www.perl.org/>
    make: *** [aclocal.m4] Error 127
    

    Then I read somewhere to run autoreconf -f -i (which fixed it).

    When make finishes, find your nice new mod_jk.so file in the native/apache-2.0 folder and copy it to where all your other modules are. I have a Macports installation so Homebrew is probably different, but my modules are in /opt/local/lib/apache2/modules.

    Don't forget to add the LoadModule line in httpd.conf if it isn't already there:

    LoadModule jk_module /opt/local/lib/apache2/modules/mod_jk.so
    

    You might have some trouble working out which apache2 folders contain the new install, and not an old installation - I found two other installations knocking about trying to confuse me. My config is here: /opt/local/etc/apache2/httpd.conf

    apachectl is very useful for configuration.

    apachectl -t -D DUMP_INCLUDES will find all the configuration files it is using. This totally saved me because it showed me that my httpd.conf file, which I had copied from elsewhere, was still pointing via 'Include' commands at other old config files in the wrong place.

    apachectl configtest will test your config for you and print out any mistakes it finds. It pointed at 4 modules that it didn't like so I just excluded them. Though obviously read the messages carefully and google if you are not sure why apachectl doesn't like something. If it replies 'Syntax OK' you are ready to go.

    This is a mysterious message I got a lot until I worked out that it was because httpd.conf was pointing at the wrong modules folder (an old install of apache2) for each module, so it was loading stuff that presumably was not compiled for 64bit

    httpd: Syntax error on line 76 of /opt/local/etc/apache2/httpd.conf: Cannot load libexec/apache2/mod_authz_owner.so into server: dlopen(/usr/libexec/apache2/mod_authz_owner.so, 0x000A): symbol not found in flat namespace '_apr_stat$INODE64'

    This is my launch command using the plist which Macports automatically created: sudo launchctl load -w /opt/local/etc/LaunchDaemons/org.macports.apache2/org.macports.apache2.plist

    And to unload: sudo launchctl unload /opt/local/etc/LaunchDaemons/org.macports.apache2/org.macports.apache2.plist

    run ps ax|grep httpd to see if it's running.

    Logging: Don't forget to sudo to create the jk folder in /var/log/apache2 if it doesn't already exist, otherwise apache or tomcat will have mysterious problems or won't start or something (the /var/log/apache2/jk folder is needed for jk.log).

    Another problem cropped up just as I thought I had it made: apache was unable to write its pid file on startup. Again this was because the position set in my config for the pid file was from the configuration on my old mac, and the position chosen was in a read-only location. To change this you need to set the PidFile parameter, which I found in the following file: /opt/local/etc/apache2/extra/httpd-mpm.conf

    and it looks like this:

    # PidFile: The file in which the server should record its process
    # identification number when it starts.
    #
    # Note that this is the default PidFile for most MPMs.
    #
    <IfModule !mpm_netware_module>
        PidFile "local/run/apache2/httpd.pid"
    </IfModule>
    

    Don't worry about what the IfModule thing is doing, just set the PidFile to a writeable location, which as you can see is a relative path. You may be wondering what goes in front of the local folder.

    What goes in front is the ServerRoot parameter set in httpd.conf:

    ServerRoot "/usr"
    

    So my pid will be written at /usr/local/run/apache2/httpd.pid. I had to create the run and apache2 folders.

    That's about it. There are various logs that might indicate errors if you are stuck:

    /var/log/apache2/error_log
    

    And the jk.log for the apache/tomcat connector:

    /var/log/apache2/jk/jk.log
    

    And there's always the system log which just might tell you something:

    /var/log/system.log
    

    I hope very much that this helps someone. However it was very long and complicated and I have surely missed something that I did along the way, so if you come across some new problem I will see if I can help.