Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

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

#!/bin/sh
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?

share|improve this question

1 Answer 1

up vote 7 down vote accepted

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
</Directory>

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).

Troubleshooting

If you have problems, consult the official Apache documentation.

share|improve this answer
    
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). –  Jacob Johan Edwards Dec 11 '11 at 19:04

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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