Apache: Setting DocumentRoot to cgi directory results in downloading file instead of executing it

apache-2.2cgivirtualhost

I have a c-compiled CGI application that I need to execute from the DocumentRoot of my Apache server. The CGI file is called index.cgi and is located at /usr/lib/cgi-bin/index.cgi.

I have the following Directory definition

<Directory "/usr/lib/cgi-bin/">
    Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
    AllowOverride None
    Order allow,deny
    Allow from all
    DirectoryIndex index.cgi 
</Directory>

I have the following VirtualHost setting:

<VirtualHost *:80>  
    ServerAdmin webmaster@localhost
    DocumentRoot /usr/lib/cgi-bin
#   ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/

    ErrorLog /var/log/apache2/error.log     
    LogLevel warn   
    CustomLog /var/log/apache2/access.log combined

</VirtualHost>

If I go to 127.0.0.1 or 127.0.0.1/index.cgi I get prompted to download the index.cgi file, however if I enable the ScriptAlias in the vhost configuration block and go to 127.0.0.1/cgi-bin/index.cgi I see the output of my CGI application.

I had originally solved this problem with mod_rewrite, however that worked on my test system the target (embedded) doesn't have that module available so I'm looking at another route (again).

Best Answer

You need to specify the cgi handler for the file if you do not use ScriptAlias. IN the directory section, add something like

AddHandler cgi-script .cgi

This is what the documentation says:

Any file that has the handler cgi-script will be treated as a CGI script, and run by the server, with its output being returned to the client. Files acquire this handler either by having a name containing an extension defined by the AddHandler directive, or by being in a ScriptAlias directory.

Related Topic