Linux – Apache/httpd /var/www/html/ .cgi scripts throw 500 internal server error

apachecentoscgilinux

I installed a new CentOS 7 x86_64 LAMP server today.

I compiled a simple CGI script in c and i called it test.cgi, and I enabled the AddHandler for .cgi scripts. However everytime i try to load the /test.cgi page from my /var/www/html directory any simple .cgi script will throw me a 500 internal server error page.

I tested that the script is working fine from the /var/www/cgi-bin directory.
My server is running selinux and apache/httpd is using suEXEC.

EDIT: also I didn't create any extra users after the lamp installation so here I'm using root to do everything for now. However I tried to fix giving the /var/www/html directory ownership to the apache user, that didn't fix sadly.

Here's the error log, as you can see it gives me a 'Permission Denied' error:

[Mon Jul 21 15:28:14.336626 2014] [core:notice] [pid 22704] SELinux policy enabled; httpd running as context system_u:system_r:httpd_t:s0
[Mon Jul 21 15:28:14.339766 2014] [suexec:notice] [pid 22704] AH01232: suEXEC mechanism enabled (wrapper: /usr/sbin/suexec)
[Mon Jul 21 15:28:14.495631 2014] [auth_digest:notice] [pid 22704] AH01757: generating secret for digest authentication ...
[Mon Jul 21 15:28:14.498690 2014] [lbmethod_heartbeat:notice] [pid 22704] AH02282: No slotmem from mod_heartmonitor
[Mon Jul 21 15:28:14.765072 2014] [mpm_prefork:notice] [pid 22704] AH00163: Apache/2.4.6 (CentOS) OpenSSL/1.0.1e-fips PHP/5.4.16 configured -- resuming normal operations
[Mon Jul 21 15:28:14.765186 2014] [core:notice] [pid 22704] AH00094: Command line: '/usr/sbin/httpd -D FOREGROUND'
[Mon Jul 21 15:28:16.027553 2014] [cgi:error] [pid 22706] [client 192.168.0.68:52930] AH01215: (13)Permission denied: exec of '/var/www/html/index.cgi' failed
[Mon Jul 21 15:28:16.030595 2014] [cgi:error] [pid 22706] [client 192.168.0.68:52930] End of script output before headers: index.cgi
[Mon Jul 21 15:45:01.586229 2014] [mpm_prefork:notice] [pid 22704] AH00170: caught SIGWINCH, shutting down gracefully

This is my /var/www/html apache config:

<Directory "/var/www/html">
    #                                                                                                                                                                                
    # Possible values for the Options directive are "None", "All",                                                                                                                   
    # or any combination of:                                                                                                                                                         
    #   Indexes Includes FollowSymLinks SymLinksifOwnerMatch ExecCGI MultiViews                                                                                                      
    #                                                                                                                                                                                
    # Note that "MultiViews" must be named *explicitly* --- "Options All"                                                                                                            
    # doesn't give it to you.                                                                                                                                                        
    #                                                                                                                                                                                
    # The Options directive is both complicated and important.  Please see                                                                                                           
    # http://httpd.apache.org/docs/2.4/mod/core.html#options                                                                                                                         
    # for more information.                                                                                                                                                          
    #                                                                                                                                                                                
    Options ExecCGI FollowSymLinks

    #                                                                                                                                                                                
    # AllowOverride controls what directives may be placed in .htaccess files.                                                                                                       
    # It can be "All", "None", or any combination of the keywords:                                                                                                                   
    #   Options FileInfo AuthConfig Limit                                                                                                                                            
    #                                                                                                                                                                                
    AllowOverride All

    #                                                                                                                                                                                
    # Controls who can get stuff from this server.                                                                                                                                   
    #                                                                                                                                                                                
    Require all granted
</Directory>

And of course I activated CGI using: AddHandler cgi-script .cgi .pl

This is my simple test.c file:

#include <stdio.h>

int main(void) {
  puts("Content-Type: text/html; charset=ISO-8859-1\n");
  fputs("Hello, World!", stdout);

  return 0;
}

The output is correctly: Content-Type: text/html; charset=ISO-8859-1\n\nHello, World!

Also i compiled it with gcc and then gave it permissions 777 to test.cgi
Do you know what I need to do to fix this?

Thanks in advance,
Zorgatone

Best Answer

I'm not sure if this is a viable solution for you, but I got it working by changing SELinux to permissive. Here are the steps in case you're interested.

vi /etc/selinux/config

Change the following line:

SELINUX=enforcing

to:

SELINUX=permissive
Related Topic