# Apache: CGI scripts not working

## evilAngel

Hey all,

I emerged Apache2.

I would like to use a CGI script.

When I enter the URL in my browser, I get the source code.

I did not configure CGI anywhere in my config file.

I am sur there is probably somewhere something to change allow CGI; but I don't know where...

```
cat /etc/conf.d/apache2 

# /etc/conf.d/apache2: config file for /etc/init.d/apache2

# When you install a module it is easy to activate or deactivate the modules

# and other features of apache using the APACHE2_OPTS line. Every module should

# install a configuration in /etc/apache2/modules.d. In that file will have an

# <IfDefine NNN> directive where NNN is the option to enable that module.

#

# Here are the options available in the default configuration:

#

#  AUTH_DIGEST  Enables mod_auth_digest

#  AUTHNZ_LDAP  Enables authentication through mod_ldap (available if USE=ldap)

#  CACHE        Enables mod_cache

#  DAV          Enables mod_dav

#  ERRORDOCS    Enables default error documents for many languages.

#  INFO         Enables mod_info, a useful module for debugging

#  LANGUAGE     Enables content-negotiation based on language and charset.

#  LDAP         Enables mod_ldap (available if USE=ldap)

#  MANUAL       Enables /manual/ to be the apache manual (available if USE=docs)

#  MEM_CACHE    Enables default configuration mod_mem_cache

#  PROXY        Enables mod_proxy

#  SSL          Enables SSL (available if USE=ssl)

#  SUEXEC       Enables running CGI scripts (in USERDIR) through suexec.

#  USERDIR      Enables /~username mapping to /home/username/public_html

#

#

# The following two options provide the default virtual host for the HTTP and

# HTTPS protocol. YOU NEED TO ENABLE AT LEAST ONE OF THEM, otherwise apache

# will not listen for incomming connections on the approriate port.

#

#  DEFAULT_VHOST      Enables name-based virtual hosts, with the default

#                     virtual host being in /var/www/localhost/htdocs

#  SSL_DEFAULT_VHOST  Enables default vhost for SSL (you should enable this

#                     when you enable SSL)

#

APACHE2_OPTS="-D DEFAULT_VHOST -D INFO -D SSL -D SSL_DEFAULT_VHOST -D LANGUAGE"

# Extended options for advanced uses of Apache ONLY

# You don't need to edit these unless you are doing crazy Apache stuff

# As not having them set correctly, or feeding in an incorrect configuration

# via them will result in Apache failing to start

# YOU HAVE BEEN WARNED.

# PID file

#PIDFILE=/var/run/apache2.pid

# timeout for startup/shutdown checks

#TIMEOUT=10

# ServerRoot setting

#SERVERROOT=/usr/lib64/apache2

# Configuration file location

# - If this does NOT start with a '/', then it is treated relative to

# $SERVERROOT by Apache

#CONFIGFILE=/etc/apache2/httpd.conf

# Location to log startup errors to

# They are normally dumped to your terminal.

#STARTUPERRORLOG="/var/log/apache2/startuperror.log"

# A command that outputs a formatted text version of the HTML at the URL

# of the command line. Designed for lynx, however other programs may work.

#LYNX="lynx -dump"

# The URL to your server's mod_status status page.

# Required for status and fullstatus

#STATUSURL="http://localhost/server-status"

# Method to use when reloading the server

# Valid options are 'restart' and 'graceful'

# See http://httpd.apache.org/docs/2.2/stopping.html for information on

# what they do and how they differ.

#RELOAD_TYPE="graceful"
```

Any ideas ?

Thanks

----------

## eccerr0r

Looks like the devs pulled a fast one on us.  Changed defaults on us...  One day I pulled up my website and saw all my source code. EEEK.

Think the correct fix is adding "cgi" to my APACHE2_MODULES in /etc/make.conf and re-emerge apache.

----------

## evilAngel

Here (https://forums.gentoo.org/viewtopic-t-836353-start-0.html) , the guy precise that 

 *Quote:*   

> Ok, the solution was to add cgid to APACHE2_MODULES in /etc/make.conf and re-emerge apache. 

 

But isn't there a directive like ExecCGI to add somewhere in a configuration file?

THanks

----------

## chiefbag

You need to add the below in /etc/conf.d/apache2 if its not already there after the re emerge with the cgi flag

APACHE2_OPTS="-D CGI"

----------

## chiefbag

Also in for example your default_vhost.include 

ScriptAlias /nagios/cgi-bin /usr/lib/nagios/cgi-bin

<Directory "/usr/lib/nagios/cgi-bin">

AllowOverride AuthConfig

Options +ExecCGI

Order allow,deny

Allow from all

</Directory>

----------

## evilAngel

Thanks!

I will try this and comment back.

----------

## evilAngel

 *Quote:*   

> the re emerge with the cgi flag

 

It is this one:

```
apache2_modules_cgi
```

 ?

http://gentoo-portage.com/www-servers/apache/USE#ptabs

Thanks

----------

## eccerr0r

In /etc/make.conf, make sure there's "cgi" in the variable APACHE2_MODULES.

```
APACHE2_MODULES="cgi"

```

Need to re-emerge apache (with --newuse).

I think I forgot the other aspects of this issue as others pointed out.  I ended up adding

```
LoadModule cgi_module modules/mod_cgi.so

```

into /etc/apache2/httpd.conf ... This makes sure the module gets loaded.  Since I've been using an old httpd.conf, this may likely be obsoleted by adding -D CGI as if there's some <ifdefine CGI> in the new httpd.conf.

The third aspect as stated is the directory configuration.  In the <Directory xxxx> clause the option for ExecCGI needs to be enabled.

I ended up allowing CGI to be enabled for my root web directory (for all vhosts), so in /etc/apache2/vhosts.d/default_vhost.include I have:

```
<Directory "/var/www/localhost/htdocs">

        Options Indexes FollowSymLinks ExecCGI Includes

        AddType text/html .shtml

        AddHandler server-parsed .shtml

        AddHandler server-parsed .html

        AddHandler cgi-script .cgi

        DirectoryIndex index.shtml index.cgi index.php index.html

        AllowOverride All

        Order allow,deny

        Allow from all

</Directory>

```

The "AllowOverride" is needed if you don't want to put ExecCGI here... (AllowOverride Options or All)  If you want your home directory to allow CGI execution, you'll need to make sure CGI is allowed there too.  /etc/apache2/modules.d/00_mod_userdir.conf needs to allow ExecCGI or allow it to be overridden.

----------

## evilAngel

 *Quote:*   

> This makes sure the module gets loaded. Since I've been using an old httpd.conf, this may likely be obsoleted by adding -D CGI as if there's some <ifdefine CGI> in the new httpd.conf. 

 

I suppose, the file to put the 

```
-D CGI 
```

 is 

```
/etc/conf.d/apache2
```

?

Thanks

----------

## eccerr0r

Yes, in /etc/conf.d/apache2 you should see something like

```
APACHE2_OPTS="-D DEFAULT_VHOST -D INFO -D LANGUAGE -D SSL -D SSL_DEFAULT_VHOST -D USERDIR -D SUEXEC -D BW -D PHP5 -D PROXY"

```

Since I have the LoadModule hard coded manyally in httpd.conf, I don't need it here.  But if you have a newer httpd.conf that detects the CGI module, you should add -D CGI.  I have no idea if the new httpd.conf does it but I suspect it should.

----------

## evilAngel

Do I need to re-emerge Apache when I change the 

```
/etc/conf.d/apache2
```

 file?

I mean, I will modify this in order to add the 

```
-D CGI
```

 directive.

Will I need to re-ermege then?

Thanks

----------

## huuan

in case someone else finds this unanswered tail:

if you already emerged apache with APACHE2_MODULES="cgi"  set in make.conf  then you only need to restart apache after adding -D CGI to /etc/conf.d/apache2

----------

