![]() |
![]() Second Summer 2009 |
Apache is installed in2. Basic Apache maintenance.~/apache.
![]()
There are five folders you need to always remember:
Working with Apache means going into these folders and editing various files.
htdocsis the document root.
This is where you place your.phpand.htmlfiles.
cgi-binis your script folder.
Place your perl and/or python scripts here.
binhas Apache maintenance utilities in it.
One utility we used already isapachectl, used to start and stop the server.
logscontains the Apache logs and the Apache.pidfile (when the server is running).
The three files in it:access_log,error_log,httpd.pid.
confis the folder that contains the Apache configuration files.
One file we used already:httpd.conf(and we will use it again soon).
Inevitably we need to resort to Unix commands for this topic.3. Posting HTML documents in your Apache.To find out if your server is running:
(Note: needless to sayps -ef | grep usernameusernamerefers to, needs to be, that is, your actual username.)To start and stop your server you need to use
apachectl.Start it with:
~/apache/bin/apachectl start.You can use
restart,stop,configteston the command line as well (not juststartas shown above).Use
crontabto set up automatic restart of your Apache server:
crontab -ewill open a window in your default editorcrontab -lwill show you the entries you haveWhen you usecrontab -rwill remove all your entries (careful when you do that)crontab -eyou should set your Apache entry to:We need to talk about:0 * * * * /u/username/apache/bin/apachectl/restart
crontab(read about it here)- setting your default editor to something you like (e.g.,
vi,emacs,picoetc.)- using
tail -fon~/apache/conf/access_logto see server accesses in real timeOf these we might have to address the second in a more involved manner, we will see.
(If so, we will add some notes here.)
We start by going into4. CGI programming in Python (and Perl).htdocsto write some HTML.When we write
htdocswe will always mean~/apache/htdocs(your server's document root).Go in there and let's create this file (call it
one.html):
As we will explain the target is<html> <head> <title>This is my first HTML file.</title> <body> An HTML form is used to collect data from the user: <p> <form action="http://silo.cs.indiana.edu:44063/cgi-bin/tuesday"> Enter your name here: <input type=text name=something> <p> Enter your age here: <input type=text name=age> <p> Press <input type=submit name=whatever value=Proceed> to submit your data. </form> </body> </head> </html>tuesday, a program we will develop below.
Go tocgi-binin your Apache installation folder.Create this file (call it
tuesday):Make it executable (#!/usr/bin/python import cgi print "Content-type: text/html\n\n" input = cgi.FieldStorage() if input.has_key("something"): name = input["something"].value else: name = "" if input.has_key("age"): age = input["age"].value else: age = "" try: output = "Well, " + name + " next year you will be " + str(int(age) + 1) + " years old." except: output = "Something went wrong. Maybe you didn't supply a valid age?" print outputchmod 700 tuesdayin that folder) then access it from the web.First access it through the form you have just developed:
Note: instead ofhttp://silo.cs.indiana.edu:44xxx/one.html44xxxplease type your own port.Enter the values in the form and press the submit button.
Pay attention to the URL and the content of the browser page upon reply.
That concludes the first part of accessing your program. Second, you can access it directly, like this:
We will discuss this second aspect in great detail when we discuss HTTP.http://silo.cs.indiana.edu:44xxx/cgi-bin/tuesday?something=Larry&age=58For now it's enough to notice that the name we spent has no space in it (we wrote
Larry, notLarry Bird).Here's now the Perl version of the Python program you've seen above:
Call this program#!/usr/bin/perl use CGI; print "Content-type: text/html\n\n"; $input = new CGI; $name = $input->param('something'); $age = $input->param('age'); $output = "Well, $name, next year you will be " . ($age + 1) . " years old."; print $output;afternoonand point the form inone.htmlto it, then test that it works like the other one did.