|
Related YoLinux Tutorials:
°Bash Shell CGI
°Java and Apache Tomcat
°CGI in C++
°Linux Networking
°Linux Sys Admin
°Internet Security
°Security Tools
°Web server configuration
°Web Tricks
°Internet Gateway
°YoLinux Tutorials Index
Free Information Technology Magazines and Document Downloads
|
Hello World CGI example: /var/www/cgi-bin/hello.cgi
-
#!/usr/bin/perl
print "Content-type: text/html\n\n";
print "<html><body>\n";
print "<h1>Hello World</h1>\n";
print "</body></html>\n";
|
Note:
- The file must be executable: chmod +x /var/www/cgi-bin/hello.cgi
- Start the web server: service httpd start
- Access through the url: http://localhost/cgi-bin/hello.cgi
- The above paths reflect Red Hat/Fedora/CentOS. For Ubuntu and other Linux distributions see the YoLinux.com Web site configuration tutorial for the Apache web server configuration.
Form CGI example:
-
|
HTML source
|
Perl CGI
|
<html>
<body>
<form action="http://localhost/cgi-bin/form.cgi"
method="POST">
<p>
Text:
<input type="text" size="20" name="TextLine">
<input type="submit" name="Button" value="Submit">
</p>
</form>
</body>
</html>
|
#!/usr/bin/perl
print "Content-type: text/html\n\n";
print "<html><body>\n";
print "<h1>Form Info:</h1>\n";
my($buffer);
my(@pairs);
my($pair);
read(STDIN,$buffer,$ENV{'CONTENT_LENGTH'});
@pairs = split(/&/, $buffer);
foreach $pair (@pairs)
{
print "$pair<br>\n"
}
print "</body></html>\n";
|
Note:
- Extra handling is required for URL "+", ASCII conversion of spaces "%20" and other web encoding.
- CGI POST method uses stdin.
- CGI file name: /var/www/cgi-bin/form.cgi
|
HTML form
|
Results
|
|
|
Form Info:
TextLine=Hello
Button=Submit
|
The results are for this example where the text entered in the text field is "Hello".
The beauty of running PHP on Linux is that Linux distributions provide an integrated Apache web server, PHP and supporting packages which are configured to work together.
The term LAMP is often used to describe the following quartet of integrated software:
- Linux
- Apache (httpd server)
- MySQL (database)
- PHP
PHP Information and Links:
Learn Python:
Ruby On Rails: Rails uses the Model-View-Controller (MVC) architecture pattern and contains Javascript libraries, AJAX libraries, and RESTful web services.
Rails relies on a web server to run it. Rails can be run with Apache (and Lighttpd) using CGI, FastCGI, mod_rails or with a dedicated platform server.
Other Ruby Frameworks:
Groovy is a scripting language with Java like syntax with features inspired by Python, Ruby and Smalltalk. Groovy seemlessly integrates with all existing Java objects and libraries as it is compiled into Java bytecode and runs in a JVM.
The simplest form of CGI scripting is with bash shell scripts.
While the scripting language has full access to system commands, its lacks in parsing constructs and reies on tools such as sed or awk.
"Tool Command Language" (Tcl) pronounced as "tickle" is a scripting language most often coupled with Tk for rapid prototyping of GUI applications.
First introduced as a graphics library for Tcl, Tool Kit (Tk) has been integrated with Perl, Python and Ruby. This is also true for other GUI frameworks like GTK (see the YoLinux.com Python PyGTK tutorial) and Qt. Tk is used for GUI scripting and not web server scripting but is included here because it is makes scripting languages like Tcl, Perl, Python and Ruby so versatile. Here is a comparison of the use of Tk in Perl, Python, Ruby and Tcl to create the following graphical "Quit" button:
-
Perl:
-
#!/usr/bin/perl
use Tk;
# Main Window
my $mw = new MainWindow;
my $but = $mw -> Button(-text => "Quit",
-command =>\&push_button);
$but -> pack();
MainLoop;
#This is executed when the button is pressed
sub push_button {
exit;
}
|
The file must be executable: chmod +x btn.pl
Run: btn.pl
Links:
Python:
-
Note: Requires the RPM package tkinter
from Tkinter import *
root = Tk()
example = Button(root, text="Quit", command=root.destroy)
example.grid()
root.mainloop()
|
Run: python btn.py
Links:
Ruby:
-
require 'tk'
root = TkRoot.new()
button = TkButton.new(root) {
text "Quit"
command proc { exit }
}
button.pack()
Tk.mainloop()
|
Run: ruby btn.py
Links:
Tcl/Tk:
-
button .b -text "Quit" -command {exit}
pack .b
|
Run: wish btn.tcl
Links:
Tk Links:
| Publishing, Site Management, Scripting Tools: |
- Zope.org - Zope.com - Conent management and colaberation
- ArsDigita - TCL scripts, Oracle database and the AOL web server - (commercial but open source) - Content management, collaborative commerce, personalization, marketing and analysis.
- OpenACS.org: Open ArsDigita Community System (ACS) for Web publishing - Open version (doesn't use Oracle) of ArsDigita using TCL scripts, the AOL web server and Postgress.
- SteelBlue - Cold Fusion inspired web development environment.
- ChiliSoft - Commercial product to support Microsoft Active Server Pages (ASP) on Linux.
- Macromedia/Allaire: Coldfusion - Commercial product. Build, publish, content management.
- AdminPro
- PERL application to navigate directories, upload images, edit files,
create new directories, delete and manage your site from a browser.
Books: |
-
 |
"Programming Perl"
by Larry Wall, Tom Christiansen, Jon Orwant
ISBN # 0596000278, O'Reilly & Associates
This book teaches you PERL and is also a good reference.
|
|
 |
"Advanced PERL Programming"
by Sriram Srinivasan
ISBN # 1565922204, O'Reilly & Associates
|
|
 |
"CGI Programming with PERL"
by Gunther Birznieks, Scott Guelich, Shishir Gundavaram
ISBN # 1565924193, O'Reilly & Associates
|
|
 |
"Programming with PERL DBI"
by Alligator Descartes, Tim Bunce
ISBN # 1565926994, O'Reilly & Associates
|
|
 |
"Learning PERL Objects, Refences and Modules"
by Randal L. Schwartz, Tom Phoenix
ISBN # 0596004788, O'Reilly & Associates
|
|
 |
"CGI/Perl Cookbook"
by Craig Patchet, Matthew Wright
ISBN # 0471168963, Wiley, John & Son
This book is full of code snippets which you can put together for your own
needs.
|
|
 |
"Building Database Applications on the Web Using Php3"
by Craig Hilton,Bjorn Borud,Jeff Willis
ISBN # 0201657716, Addison Wesley Longman
|
|
 |
"Php3 and MySQL for Dynamic Web Sites"
by Larry Ullman
ISBN # 0321186486, Peachpit Press
|
|
 |
"Programming Python: Object-Oriented Scripting"
by Mark Lutz,Foreword by Guido Van Rossum
ISBN # 0596000855, O'Reilly & Associates
|
|
 |
"Programming Ruby: The Pragmatic Programmers Guide, Second Edition"
by Dave Thomas, Chad Fowler, Andy Hunt
ISBN # 0974514055, Pragmatic Bookshelf; 2nd edition (October 1, 2004)
|
|
|
|