Configure CGI in Apache2

To enable CGI scripting in our apache2 web server in Ubuntu, run the following command:

ubuntu@ubuntu:~$ sudo a2enmod cgid

Enabling module cgid.

To activate the new configuration, you need to run: systemctl restart apache2 ubuntu@ubuntu:~$

Before we restart the Apache web server to enable CGI scripting, I want to make another change. In this default configuration, the apache web server will only look for CGI scripts in the /usr/lib/cgi-bin directory. You can see this default setting in the /etc/apache2/conf-enabled/serve-cgi-bin.conf file:

ubuntu@ubuntu:~$ cat /etc/apache2/conf-available/serve-cgi-bin.conf

<IfModule mod_alias.c>

        <IfModule mod_cgi.c>

                Define ENABLE_USR_LIB_CGI_BIN

        </IfModule>

        <IfModule mod_cgid.c>

                Define ENABLE_USR_LIB_CGI_BIN

        </IfModule>

        <IfDefine ENABLE_USR_LIB_CGI_BIN>

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

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

                        AllowOverride None

                        Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch

                        Require all granted

                </Directory>

        </IfDefine>

</IfModule>

 

# vim: syntax=apache ts=4 sw=4 sts=4 sr noet

ubuntu@ubuntu:~$

Do you see where /usr/lib/cgi-bin is specified in two places? I want to change this location to /var/www/cgi-bin. This way, we can keep all of our code in the /var/www directory. To do this, use emacs to edit this file and change this location in both places. Note that you will need to use sudo to edit this file, because only Super Users can change configuration files for system services. So your command to edit this file will be: 'sudo emacs -nw /etc/apache2/conf-available/serve-cgi-bin.conf'.

When you are done making these edits, save the file and exit emacs. Now the file should look like this (changes in RED):

ubuntu@ubuntu:~$ cat /etc/apache2/conf-available/serve-cgi-bin.conf

<IfModule mod_alias.c>

        <IfModule mod_cgi.c>

                Define ENABLE_USR_LIB_CGI_BIN

        </IfModule>

        <IfModule mod_cgid.c>

                Define ENABLE_USR_LIB_CGI_BIN

        </IfModule>

        <IfDefine ENABLE_USR_LIB_CGI_BIN>

                ScriptAlias /cgi-bin/ /var/www/cgi-bin/

                <Directory "/var/www/cgi-bin">

                        AllowOverride None

                        Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch

                        Require all granted

                </Directory>

        </IfDefine>

</IfModule>

 

# vim: syntax=apache ts=4 sw=4 sts=4 sr noet

ubuntu@ubuntu:~$

Now we can restart the apache2 service. Remember that you will need to be the Super User to do this:

ubuntu@ubuntu:~$ sudo systemctl restart apache2

[sudo] password for ubuntu:

ubuntu@ubuntu:~$

Copyright 2020
  • Facebook - Black Circle
  • Twitter - Black Circle
  • YouTube - Black Circle
  • Instagram - Black Circle
Follow us on these platforms