JavaScript "AH01215: (8)Exec format error" "End of script output before headers" on Apache

Richard T asked:

Finally time to apply JavaScript to a particular web site, so I wrote some code that works fine in a test sub-directory, but when I went to deploy this to the web site I ran into problem(s). The problem(s) appears to be unique to when I’m trying to use a script file that’s not in the directory or a sub-directory to where the page that’s calling it is located. And this would be a show-stopper because the new functionality is needed site wide and the directory structure dynamically changes based on other mechanisms we don’t need to get into here; the ability to call a top-level script is vital.

So, I stripped it all down to two lines of very simple JavaScript to help illuminate the issues; when it is contained in a file in the directory where the calling html resides, it works, or a subdirectory from there, OR if I refer to it in the same directory via a link to another location, that’s fine too. But if I try and refer to the script file in any other way, it fails with:

[Thu Dec 08 12:08:26.643037 2016] [cgi:error] [pid 27403] [client xx.xx.xx.xx:46247] AH01215: (8)Exec format error: exec of ‘/usr/www/http/js/test.js’ failed, referer: [Thu Dec 08 12:08:26.643548 2016] [cgi:error] [pid 27403] [client xx.xx.xx.xx:46247] End of script output before headers: test.js, referer:

Here, of course, xx.xx.xx.xx is the IP of the server, “” is the domain in question, and the path ‘/usr/www/http’ is the top level directory Apache serves this domain’s web site from.

I’ve read a lot of materials on this and I know the Apache configuration files matter… I’ve tried setting a ScriptAlias and have a few questions about it I can’t seem to find clarity on:

  1. Just how are scripts in the “ScriptAlias” directory refered to in
    the HTML code – start with a slash? Is there some special character
    to say “top of site?” And;
  2. Is there only one unique ScriptAlias directory permitted for the
    whole site, or can one have multiple directories that are searched –
    and if so, how do you specify them, multiple definitions of
    ScriptAlias, comma separated list, etc? And;
  3. Can we set one per VirtualHost? And do we do this by putting a
    ScriptAlias inside the Virtual Host definition?

To make testing a little easier I made multiple instances of almost the same code in files of the same name in various directories, and altered the contents slightly so I could easily determine which location had taken effect with what kind of a call, all the while looking at the Apache logs. Here’s an example from one of these files:

var myHeading = document.querySelector('h2');
myHeading.textContent = 'website top level js dir';

This code takes the first tag and replaces whatever the content was with whatever textContent is provided in the second line. Obviously, I was changing the textContent to match what the location was.

In the HTML, any reference like any of these worked so long as the calling page was either in the same directory as test.js or in a parent directory or if there was a link to test.js that was either in the same directory or a subdirectory:

<script src="test.js"></script>
<script src="js/test.js"></script>
<script src=""></script>

In the HTML, any reference like these always failed no matter what I tried so long as the calling html was not in the same directory or a parent directory:

<script src="/js/test.js"></script>
<script src="/usr/WWW/http/js/test.js></script>
<script src=""></script>

The directories and files involved are all owned by my personal UID and GID (as they always have been), and their permissions are all 755.

There’s a global ScriptAlias defined as (also tried variations on this):

ScriptAlias /js/ "/usr/www/js/"

There’s another ScriptAlias entry in the Virtual Host definition – no idea if it does anything! No effect so far as I can yet see. … And, there are Directory entries like this in the main config file for all the directories I tried:

<Directory /usr/www/http/js/>
   AllowOverride None
   Options ExecCGI
   Require all granted

Fredi wanted a tree. Fine:

The tree starts at the Virtual host’s document root – who cares where? From there:

├── index.html
├── js
│   └── test.js
└── test
    ├── js
    │   └── test.js
    └── testjs.html

3 directories, 4 files

My answer:

This is not what ScriptAlias is intended for. Remove it from your configuration.

ScriptAlias is an Apache configuration directive telling it to attempt to execute files in that directory as programs and return the programs’ output, instead of simply transmitting a static file to the user agent. This is very obviously not what you want. It’s the browser that is supposed to run the JavaScript, not the server.

View the full question and answer on Server Fault.

Creative Commons License
This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.