Lets get started:
This guide identifies the following steps
sudo apt-get update sudo apt-get install apache2Apache is now installed and running as a service. Opening firefox at http://127.0.0.1:80should show the default welcome page. If your version of linux mint is different, read the file carefully, because it contains some of the configuration locations that may have changed.
sudo service apache2will show you some commands available for starting, stopping, reloading apache etc.
sudo a2enmod cgiNext force reload to apply the changes:
sudo service apache2 force-reloadTo verify that the module is currently active:
apache2ctl -MThe list should contain "cgid_module" (the rest of this paragraph is optional).
Disabling cgi would be done using
sudo a2dismod cgi
Some background on how the a2enmod script works. Apache ships with a whole bunch of modules, but not all of them are on by default. The complete list of modules can be found in the following directory:
ls /etc/apache2/mods-availableThe list of modules that is enabled can be found in:
ls -l /etc/apache2/mods-enableda2enmod enables mods by creating soft links in the mods-enabled directory To enable(disable) any of these modules manually, simply create(delete) a soft link in the directory mentioned above.
sudo ln -s /etc/apache2/mods-available/cgid.* /etc/apache2/mods-enabled
sudo vi /etc/apache2/sites-enabled/000-default.confAdd the following right before the </VirtualHost> closing tag:
ScriptAlias /cgi-bin/ /var/www/cgi-bin/ <Directory "/var/www/cgi/bin"> AllowOverride None Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch Order allow,deny Allow from all </Directory>Now apache knows that any url starting with /cgi-bin/ should find a file in /var/www/cgi-bin/ and execute it. The (system) output of the script will be sent to the browser. To apply the changes:
sudo service apache2 restartNow before we can test our configuration, the last step is to create a cgi script.
To create the cgi script, first we ensure that the cgi-bin dir exists:
sudo mkdir -p /var/www/cgi-bin/Then we create a new script:
sudo vi /var/www/cgi-bin/hello.cgiAdd the following contents to the file:
#!/bin/sh echo "Content-type: text/html" echo "" echo "<html>" echo "<body>" date echo "<h1>My first cgi script</h1>" echo "script run dir $(pwd)" echo "<h2>Environment variables</h2>" echo "<pre>" env echo "</pre>" echo "</body>" echo "</html>"Lastly, we make sure the script is executable.
sudo chmod a+x /var/www/cgi-bin/*You can always check if a script compiles and runs by running it directly:
/var/www/cgi-bin/hello.cgiIt is time to open the following url to check if we have succeeded:
Now we will examine the script in a little more detail for those who want some extra background.
#!/bin/shThis first line is an instruction to linux, to tell it which excutable (shell or programming language) to use to run the file. Here, the script is run in the bourne shell. For more info: https://en.wikipedia.org/wiki/Shebang_%28Unix%29
#!/bin/bashWould run the script in the bourne again shell instead (it will still work).
echo "Content-type: text/html"Set the Content type / MIME type of the response (https://en.wikipedia.org/wiki/MIME). Next the script prints some raw HTML and some extra info, which might be useful for your scripts. $(pwd) will print the directory the script is run in (/var/www/cgi-bin/). I also printed the environment variables, so you can see that the script has access to the query string. For example, if you open http://127.0.0.1/cgi-bin/hello.cgi?param=value, then you will see that the REQUEST_URI and QUERY_STRING environment variables now contain param=value. Please note that these are in encoded format and that using input parameters opens a whole new can of security worms.
sudo touch /var/www/cgi-bin/other.py sudo chmod +x /var/www/cgi-bin/* sudo vi /var/www/cgi-bin/other.pyContents of the file:
#!/usr/bin/python3 print("Content-type: text/html") print("") print("Python says hello!")Now view the result at: http://127.0.0.1/cgi-bin/other.py
sudo touch /var/www/cgi-bin/monitoring.cgi sudo chmod +x /var/www/cgi-bin/* sudo vi /var/www/cgi-bin/monitoring.cgiHere is the sample script for a rudimentary monitoring page:
#!/bin/sh echo "Content-type: text/html" echo "" echo "<html>" echo " <head>" echo " <meta http-equiv="refresh" content="5;">" echo " </head>" echo "<body>" echo "<h1>cpu usage</h1><pre>$(top -b -n2 | grep "Cpu(s)" | tail -n 1)</pre>" echo "<h1>memory usage</h1><pre>$(free -h)</pre>" echo "<h1>disk usage</h1><pre>$(df -h)</pre>" echo "</body>"Unfortunately top doesn't show the correct cpu usage on the first iteration. I had to apply a workaround that causes the script to wait on top for a second. So the page might be a little slow.
Check the result at: http://127.0.0.1/cgi-bin/monitoring.cgi Next: cgi handlers Main Page