Reputation: 41
In moving a website from older webservers running Apache 2.2 to newer webservers running Apache 2.4 I encountered a weird problem with CGI. Basically no CGI scripts work on the new webservers. They return 500 errors. However in the ScriptLog there is no "%error" section and the "%response" is empty. Scripts appear to be running but returning absolutely nothing! Since nothing implies no header the result is a 500 error.
The mod_cgi module is loaded (confirmed by running "apachectl -M"). We are using a prefork MPM so this is the correct module.
Most of the CGI scripts are Perl but we also have one which is compiled C which shows exactly the same pattern of behavior. Even a basic test script like this does not work:
#!/usr/bin/perl
print STDOUT "Content-type: text/html\n\n";
print STDOUT "Hello, World.";
I temporarily assigned a shell to the "apache" user, switched to that user, and was able to run several of these scripts. Not all produce meaningful output when run that way but they do run. Yes, /usr/bin/perl does exist, is the only copy of Perl on the system, and perl-CGI is installed.
All of these scripts are on an NFS share which is used by both the old and new webservers. The old webservers can still serve up these scripts as CGI with no problems. So in case it wasn't already clear the issue here is not with the CGI scripts themselves. It is a configuration problem with the new webservers.
The NFS share is mounted at /mnt/cgi/ with subdirectories for each user. There are sections in a file included in our Apache config which look like this:
Alias /cgi-bin/usera /mnt/cgi/usera
<Directory /mnt/cgi/usera>
Options +ExecCGI
AddHandler cgi-script .cgi .pl
Require all granted
</Directory>
A script in this directory would be accessed at http://server.example.com/cgi-bin/usera/first.pl . When I connect to this page this is appended to the log file specified in ServerLog (with the correct IP addresses... I xxx-ed those out):
%% [Fri Nov 11 12:00:00 2016] GET /cgi-bin/usera/first.pl HTTP/1.1
%% 500 /mnt/cgi/usera/first.pl
%request
Host: xxx.xxx.xxx.xxx
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101 Firefox/45.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-US,en;q=0.5
Accept-Encoding: gzip, deflate
DNT: 1
Connection: keep-alive
Via: 1.1 xxx.xxx.xxx.xxx:80
X-Forwarded-For: xxx.xxx.xxx.xxx
X-Forwarded-For-Port: 51380
%response
The permissions on these scripts are all 755, so that's not the problem. If I remove the AddHandler line from the Directory definition for the script directory then I can download the script, so Apache is definitely able to access them.
The new servers are RHEL7. SELinux is in Permissive mode, not Enforcing. The booleans httpd_enable_cgi and httpd_use_nfs are both "on" anyway.
Among the things which I have tried which do not help are:
I should also add that in general the new webservers work fine. PHP-based webapps run just fine on them, and of course static content is no problem.
So that's a lot of detail but in the end the issue is this: How can Apache be executing CGI scripts but getting no output at all from them? Any thoughts?
Upvotes: 2
Views: 1801
Reputation: 181
If your perl script is
#!/usr/bin/perl
print STDOUT "Content-type: text/html\n\n";
print STDOUT "Hello, World.";
Please try removing "STDOUT" from it.
Upvotes: 1
Reputation: 41
Sure enough not too long after I asked this question I found the answer. Basically, we had this line in the config file for this site:
RLimitMEM 2000000 3000000
This limits the memory of processes to 2MB (soft) and 3MB (hard). It is also far too little for CGI scripts. 50MB/80MB worked. We set it even higher just in case.
Here are a few references to people having similar problems for the benefit of those of you who found this page via Google:
PHP out of memory error even though memory_limit not reached
Upvotes: 2