What steps do I follow to run a stock Hello World CGI 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
</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.