Assume I have finished installing a modern release of stock Ubuntu, and have the following script.

echo "Content-type: text/html\n"
echo "<html><body>Hello world</body></html>"

How do I install and configure Apache to serve a "Hello world" web page from http://localhost using this script?


The Quick Way

Install the Apache web server

In the command line (Terminal) application:

sudo apt-get install apache2

Make your script executable by Apache

For this example we'll assume the CGI script is named test.sh and is saved in your personal home folder. For Apache to use test.sh, the script first needs to have executable permissions:

chmod 755 $HOME/test.sh

It also needs to be moved to the Apache-designated CGI folder. For Ubuntu (and other Debian-based operating systems), this is /usr/lib/cgi-bin/, while HTML and other content is stored in /var/www.

sudo mv $HOME/test.sh /usr/lib/cgi-bin/

Visit the output at your local server

Apache will serve the test.sh-generated HTML at http://localhost/cgi-bin/test.sh. If Apache is located somewhere besides your own machine, replace 'localhost' with the URL or IP address of the server.

Storing CGI scripts in /var/www/test-cgi rather than /usr/lib/cgi-bin/

You need to modify the vanilla Apache configuration to store CGI scripts at another folder.

sudoedit /etc/apache2/sites-enabled/000-default

Add the following inside the <VirtualHost> directive:

ScriptAlias /test-cgi/ /var/www/test-cgi/
<Directory "/var/www/test-cgi">
        AllowOverride None
        Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
        Order allow,deny
        Allow from all

Then, in the command line:

sudo mkdir /var/www/test-cgi
sudoedit /var/www/test-cgi/test.sh

Then, in the command line, restart Apache:

sudo apachectl restart

As with The Quick Way, the CGI output should be served at http://localhost/test-cgi/test.sh (replace "localhost" with the server domain if you're developing remotely).


If you have problems, consult the official Apache documentation.

  • Note to friendly Apache gurus: It would be nice for this answer to cover a few more configurations besides /usr/lib/cgi_bin and /var/www/test_cgi/ (eg the $HOME/public_html option). – Jjed Dec 11 '11 at 19:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.