Friday, June 20, 2014

Install and Enable CGI-BIN on Apache Server for Ubuntu

It's time to go a little old-school and lay out how to enable the Common Gateway Interface (CGI) for your Apache server. You might be wondering why we need CGI. It's simple: CGI allows the web server to interact with external programs. These programs can range in purpose and scope, but primarily they help to add dynamic content to a website. Most often these applications are Perl programs with the extension .pl, but CGI execution is not limited to Perl.

Get started with CGI

One of the stumbling blocks most people come up against with CGI is getting their Apache server to recognize the CGI directory and to allow for the execution of commands from within that directory. I will demonstrate this process on a Ubuntu 11.04 server running the latest LAMP stack. For the most part, this LAMP stack is default, so a fresh install of LAMP will do just fine.
Let's first talk about the directories. If you look in the /var/www (the document root of Apache), you will find a sub-directory called cgi-bin. This is not where your Perl programs and other various files will be placed. Within the /usr/lib/ directory, you will find another cgi-bin directory; it is the repository for your executables. If that directory does not exist, create it with the command: 

$ sudo mkdir /usr/lib/cgi-bin.

Now, make sure the permissions look like this:
drwxr-xr-x  3 root root         4096 2011-11-23 09:08 cgi-bin
Issue the command ls -l /usr/lib | less and scroll to check your directory's permissions. If it's not as you see above, issue the following commands:
$ sudo chmod 755 /usr/lib/cgi-bin

$ sudo chown root.root /usr/lib/cgi-bin

Now that your directory is ready, it's time to configure Apache. Remember, this is Ubuntu, so you will have to make a few simple modifications if you're using a different distribution with a different take on the Apache web server.

Configure Apache

A directive must be created so Apache knows about CGI -- where its directories are located and what it can do. In some Apache configurations, this is done within the httpd.conf file. Because this is Ubuntu, we are going to add the directive to /etc/apache2/apache2.conf
$ sudo nano /etc/apache2/apache2.conf
Open that file with your favorite text editor and, before you add anything, search for this section:
ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
<Directory "/usr/lib/cgi-bin">
AllowOverride None
Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
Order allow,deny
Allow from all
If the above section does not exist, add it in the <Virtualhost *:80> section just above the line:
ErrorLog ${APACHE_LOG_DIR}/error.log
Save that file and be ready to restart Apache. To restart the server, issue the command:
$ sudo service apache2 restart

Test it out

Now it's time to test this baby out. Let's create a file (call it, and the file's contents should be:
#!/usr/bin/perl -w
print "Content-type: text/html\r\n\r\n";

print "Hello there!<br />\nJust testing .<br />\n";

for ($i=0; $i<10; $i++)


    print $i."<br />";

Save that file in /usr/lib/cgi-bin and give it 755 permissions. Now open that file in your web browser (point the browser to http://IP_OF_SERVER/cgi-bin/ where IP_OF_SERVER is the address of the server), and you should see the following:
Hello there!
Just testing .

If you see that, CGI is working. You can dump your programs into the /usr/lib/cgi-bin/ directory and start using them It's simple and effective.

No comments:

Post a Comment