Wednesday, April 13, 2016

Configure Apache to enable cgi-bin user directories to run cgi, perl and python in UBUNTU

1. First enable cgi module using the below lines.
sudo a2enmod cgi
sudo service apache2 restart
After latest apache with cgi mod enabled. 
ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/

  Options +ExecCGI
  AddHandler cgi-script .cgi .pl .py
  Options FollowSymLinks
  Require all granted
3. Create CGI script CGI works with multiple languages, but for now we start with bash shell. We will show examples for some other languages later. Here is a simples version of the CGI bash example:
#!/bin/bash
echo "Content-type: text/html"
echo ''
echo 'CGI Bash Example'
Copy the above code and paste it to a new file in /usr/lib/cgi-bin/ called example-bash.sh. Once done make the file executable using thechmod command:
$ sudo chmod 755 /usr/lib/cgi-bin/example-bash.sh
4. View CGI script
All what remains is to navigate with your browser to host-name or IP address of your web server. In our case the URL will be: http://cgi-example.local/cgi-bin/example-bash.sh
CGI bash example
You can edit this example to display a disk usage of server's root partition "/". You are only limited by your imagination:
#!/bin/bash
echo "Content-type: text/html"
echo ''
echo 'CGI Bash example
'
echo `df -h / | grep -v Filesystem`
The above code will check for free disk space for a root partition and produce the following page:
CGI bash example 2

5. More CGI examples

As promised, here are more CGI examples for a few more programming languages to get you started.

5.1. Perl

Create and make executable the following /usr/lib/cgi-bin/example-perl.pl with a content:
#!/usr/bin/perl

print "Content-type: text/html\n\n";
print <

CGI Perl Example

CGI Perl Example

CGI Perl Example htmlcode

5.2. Python

Create and make executable the following /usr/lib/cgi-bin/example-python.py with a content:


CGI Python Example

CGI Python Example

CGI Python Example """

5.3. C

For C and C++ to work we will need to have a compiler installed. First, install compiler with:
$ sudo apt-get install build-essential
Once installed create a file example-c.c with the following code:
#include 
int main(void)
{
printf("Content-Type: text/plain \n\n");
printf("CGI C Example \n");
}
save the content of example-c.c file and compile it with the following command:
$ sudo gcc -o /usr/lib/cgi-bin/example-c example-c.c
now you should be able to access your C compiled CGI script with: http://cgi-example.local/cgi-bin/example-c

5.4. C++

For C and C++ to work we will need to have a compiler installed. First, install compiler with:
$ sudo apt-get install build-essential
Once installed create a file example-cpp.c with a following code:
#include 
using namespace std;

int main()
{
        cout << "content-type: text/html" << endl << endl;
        cout << "

CGI C++ example

" << endl; return 0; }
save the content of example-cpp.c file and compile it with the following command:
$ sudo g++ -o /usr/lib/cgi-bin/example-cpp example-cpp.c
now you should be able to access your C compiled CGI script with: http://cgi-example.local/cgi-bin/example-cpp

6. Conclusion

As mentioned earlier the CGI is quite old and was largely superseded by different programing languages such as PHP, etc. However, as you can see, it is still relatively simple tool to use to automate your Linux administration tasks such as a remote custom monitoring for your Linux servers using a web browser.

https://www.youtube.com/watch?v=ELFdP7eEZ5w

No comments: