1. First enable cgi module using the below lines.
sudo a2enmod cgi sudo service apache2 restart
After latest apache with cgi mod enabled.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:ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
Options +ExecCGI AddHandler cgi-script .cgi .pl .py Options FollowSymLinks Require all granted #!/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.sh4. 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.shYou 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: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 htmlcode5.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-essentialOnce installed create a file example-c.c with the following code:#includeint 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.cnow you should be able to access your C compiled CGI script with: http://cgi-example.local/cgi-bin/example-c5.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-essentialOnce installed create a file example-cpp.c with a following code:#includeusing 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.cnow you should be able to access your C compiled CGI script with: http://cgi-example.local/cgi-bin/example-cpp6. 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:
Post a Comment