Apache Server 2 Bible Hungry Minds phần 6 docx

80 214 0
Apache Server 2 Bible Hungry Minds phần 6 docx

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

370 Part III ✦ Running Web Applications Enabling CGI Debugging Support in Apache To help CGI developers, Apache has logs for CGI output. For each CGI program error, the log file contains a few lines of log entries. The first two lines contain the time of the request, the request URI, the HTTP status, the CGI program name, and so on. If the CGI program cannot be run, two additional lines contain information about the error. Alternatively, if the error is the result of the script returning incorrect header information, the information is logged in as: all HTTP request headers, all headers outputted by CGI program, and STDOUT and STDIN of the CGI program. If the script failed to output anything, the STDOUT will not be included. To log CGI output in Apache, use the directives described in the following sections in the mod_cgi module, which is part of standard distribution. With these directives you can set up the logging of CGI programs that you are developing or attempting to install on your system. ScriptLog The ScriptLog directive sets the log filename for CGI program errors. If the log filename is relative (that is, it does not start with a leading /), it is taken to be relative to the server root directory set by ServerRoot directive. Syntax: ScriptLog filename Context: Resource config When you use this directive, make sure that the log directory is writeable by the user specified by UserDir directive. Using this directive on a daily basis might not be a good idea as far as efficiency or performance goes. I recommend using it when needed and turning it off when the debugging is completed. ScriptLogLength The ScriptLogLength directive limits the size of the log file specified by the ScriptLog directive. The script log file can log a lot of information per CGI error and, therefore, can grow rapidly. By using this directive, you can limit the log size so that when the file is at the maximum length, no more information will be logged. Syntax: ScriptLogLength size Default: ScriptLogLength 10385760 Context: Resource config Caution g4821-2 ch12.F 2/22/02 10:27 AM Page 370 371 Chapter 12 ✦ Running CGI Scripts ScriptLogBuffer The ScriptLogBuffer directive limits the size of POST or PUT data that is logged. Syntax: ScriptLogBuffer size Default: ScriptLogBuffer size 1024 Context: Resource config Debugging Your Perl-Based CGI Scripts If you use Perl-based CGI scripts, as discussed earlier in this chapter, you have lot more help in troubleshooting your CGI scripts than just what Apache offers as CGI logs. You can debug a Perl-based CGI script from the command line by using the famous CGI.pm module. Or, you can write debug messages to the standard error log (STDERR) file, which Apache automatically redirects to the Apache error log. I will discuss these techniques in the following sections. Debugging from the command line If you use the famous CGI module, as I did in all of the practical CGI scripts discussed in this chapter, you are in luck. The CGI module enables you to troubleshoot your CGI script from the command line, which makes it really convenient to debug a script. Let’s look at an example CGI script called badcalc.pl, which is shown Listing 12-7. Listing 12-7: badcalc.pl #!/usr/bin/perl -w use CGI; my $query = new CGI; my $num1 = $query->param(‘num1’); my $num2 = $query->param(‘num2’); my $sum = $num1 + num2; #print $query->header; print “$num1 + $num2 = $sum”; exit 0; g4821-2 ch12.F 2/22/02 10:27 AM Page 371 372 Part III ✦ Running Web Applications When this script is accessed via a URL such as http://www.domain.com/cgi-bin/ notready.pl , it returns an internal server error message and logs an error message in the server’s error log file. You want to know why this small script does not work. Here is a typical debugging session. 1. Enable command-line debugging for the CGI module by changing the use CGI line to: Use CGI qw(-debug); This enables the command-line debugging for the module. 2. As root, su to the Apache user (that is, the user you set the User directive to) and run the script from the command line. You will see this message: (offline mode: enter name=value pairs on standard input) and the script will wait for your input. 3. In command-line mode, enter key=value pairs in each line to simulate input from the Web. For example, to feed the above script, an example command-line session would look similar to this: (offline mode: enter name=value pairs on standard input) num1=100 num2=200 The preceding sets the num1 input field to 100 and the num2 input field to 200. Each field is set to a value in its own line. 4. When you are done entering all input, press Ctrl+D to terminate the input part of the debugging and watch what the script does. The complete debugging session for the above input is shown here: (offline mode: enter name=value pairs on standard input) num1=100 num2=200 [control+d] 100 + 200 = 100 As you can see, the script added the two numbers and printed the data as expected. So why did this script bomb when run from the Web? Well, do you see any Content-Type header before the output? No. If you look at the script you will notice that the print $query->header; line is commented out. If you remove the comment and rerun the script in command-line mode, you will see the following: (offline mode: enter name=value pairs on standard input) num1=100 num2=200 Content-Type: text/html; charset=ISO-8859-1 100 + 200 = 100 g4821-2 ch12.F 2/22/02 10:27 AM Page 372 373 Chapter 12 ✦ Running CGI Scripts Debugging by using logging and debug printing This type of command-line debugging is very useful for small, less-complex scripts, but if you have a fairly large script, such as the formwizard.pl, command-line debugging is too cumbersome. In such a case, you need to use a combination of logging and debug printing. Here is an example script, called calc.pl, that uses logging and debug printing: !/usr/bin/perl -w use CGI qw(-debug); use constant DEBUG => 1; my $query = new CGI; my $num1 = $query->param(‘num1’); my $num2 = $query->param(‘num2’); print $query->header; if ($num1 == $num2) { # do something useful DEBUG and print STDERR “num1 and num2 are same.\n”; } elsif ($num1 > $num2 ) { # do something useful DEBUG and print STDERR “num1 is greater than num2.\n”; } elsif ($num1 < $num2 ) { # do something useful DEBUG and print STDERR “num1 is less than num2\n”; } print $query->start_html(‘Calculator’); print $query->h1(“Calculator”); print $query->p(“Number 1: $num1”); print $query->p(“Number 2: $num2”); print $query->end_html; exit 0; When this script is called from a URL such as http://www.domain.com/cgi-bin/ calc.pl?num1=100&num2=300 , it prints information in the standard error log for that site. For the above-mentioned URL, the entry in the error log will similar to this: [Tue Mar 20 20:04:26 2001] [error] [client 207.183.233.19] num1 is less than num2 g4821-2 ch12.F 2/22/02 10:27 AM Page 373 374 Part III ✦ Running Web Applications The following statement prints this error message: DEBUG and print STDERR “num1 is less than num2\n”; The interesting thing about this line is that it uses a constant called DEBUG, which is set in the beginning of the script with this line: use constant DEBUG => 1; The logic in the DEBUG and print statement follows: ✦ When DEBUG is set to 1 or to any nonzero number it is the equivalent of the ‘true’ value obtained when DEBUG is used in a logical operation. ✦ The built-in print function always returns a nonzero value when it is successful in printing. ✦ So, when Perl evaluates DEBUG and print, it executes the print statement. ✦ When DEBUG is set to 0, the DEBUG and print statement does not execute. This enables you to insert print statements that can be part of your code but that can be turned off when you are done debugging. Notice that the print statement writes to STDERR, which always writes the data to the error logs for the Web site. To turn off these statements, you simply set the DEBUG constant to 0. Now, some might argue that you should completely remove these statements from your script when you are ready to hand the script to production. The reasoning behind such an argument is that Perl still evaluates these DEBUG statements even though they do not print anything, thereby slowing down the script. The truth is that in a CGI solution, the speed difference might not matter because CGI scripts already have a heavier overhead than does a mod_perl or other persistent solution. But if you are concerned, then remove the DEBUG statements before sending the script to production. Debugging with CGI::Debug Now let’s take a look at another debugging solution. You can get a great deal of help in debugging your CGI script using the CGI::Debug module. Simply add this module right after the use CGI; statement in your script, and you will be able to catch all types of errors. For example: !/usr/bin/perl –w use CGI; use CGI::Debug; my $query = new CGI; g4821-2 ch12.F 2/22/02 10:27 AM Page 374 375 Chapter 12 ✦ Running CGI Scripts my $num1 = $query->param(‘num1’); my $num2 = $query->param(‘num2’); #print $query->header; print $query->start_html(‘Calculator’); print $query->h1(“Calculator”); print $query->p(“Number 1: $num1”); print $query->p(“Number 2: $num2”); print $query->end_html; exit 0; I intentionally commented out the $query->header line, which would normally generate an internal server error message on the Web browser. But because I added the use CGI::Debug; statement in this script, the script will show the following when it is accessed as http://www.domain.com/c/s.dll/cgidebug. pl?num1=1&num2=200 : /cgi-bin/cgidebug.pl Malformed header! Program output below <?xml version=”1.0” encoding=”utf-8”?> <!DOCTYPE html PUBLIC “-//W3C//DTD XHTML Basic 1.0//EN” “http://www.w3.org/TR/xhtml-basic/xhtml-basic10.dtd”> <html xmlns=”http://www.w3.org/1999/xhtml” lang=”en- US”><head><title>Calculator</title> </head><body><h1>Calculator</h1><p>Number 1: 1</p><p>Number 2: 200</p></body></html> This program finished in 0.078 seconds. Parameters num1 = 1[1] num2 = 3[200] Cookies Environment DOCUMENT_ROOT = 15[/home/kabir/www] g4821-2 ch12.F 2/22/02 10:27 AM Page 375 376 Part III ✦ Running Web Applications GATEWAY_INTERFACE = 7[CGI/1.1] HTTP_ACCEPT = 133[image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, applica] HTTP_ACCEPT_ENCODING = 13[gzip, deflate] HTTP_ACCEPT_LANGUAGE = 5[en-us] HTTP_CONNECTION = 10[Keep-Alive] HTTP_HOST = 14[rhat.nitec.com] HTTP_USER_AGENT = 50[Mozilla/4.0 (compatible; MSIE 5.5; Windows NT 5.0)] PATH = 60[/usr/bin:/bin:/usr/sbin:/sbin:/usr/X11R6/bin:/home/kabir/bin] QUERY_STRING = 15[num1=1&num2=200] REMOTE_ADDR = 14[207.183.233.19] REMOTE_PORT = 4[2841] REQUEST_METHOD = 3[GET] REQUEST_URI = 36[/cgi-bin/cgidebug.pl?num1=1&num2=200] SCRIPT_FILENAME = 37[/home/kabir/www/asb2/ch12/cgidebug.pl] SCRIPT_NAME = 20[/cgi-bin/cgidebug.pl] SERVER_ADDR = 14[207.183.233.20] SERVER_ADMIN = 16[you@your.address] SERVER_NAME = 14[rhat.nitec.com] SERVER_PORT = 2[80] SERVER_PROTOCOL = 8[HTTP/1.1] SERVER_SIGNATURE = 66[<ADDRESS>Apache/2.0.14 Server at rhat.nitec.com Port 80</ADD] SERVER_SOFTWARE = 20[Apache/2.0.14 (Unix)] <EOF> As you can see, there is a ton of information that will help you to troubleshoot the problem and to fix the script quickly. For example, one line in the preceding program output states that the header is malformed. ✦✦✦ g4821-2 ch12.F 2/22/02 10:27 AM Page 376 Server Side Includes (SSI) I n Chapter 12, I discuss how dynamic Web content can be created using CGI programs; however, there are tasks that might not call for the development of full-blown CGI programs but that still require some dynamic intervention. For example, say you want to add a standard copyright message to all your HTML pages; how would you implement this? Well, you have two solutions: ✦ Add the content of the copyright message to each HTML page. ✦ Write a CGI program that adds the message to each HTML page. Neither of these options is elegant. The first option requires that anytime that you make a change to the copyright message, you manually update all your files. The second option requires that you have some way to get your CGI program running be- fore each page is sent to the Web browser. This also means that every link on each page has to call this CGI program so that it can append the message to the next page. Situations like these demand a simpler solution. Server Side Include (SSI), the topic of this chapter, is that simpler solution. What Is a Server Side Include? Typically, an SSI page is an HTML page with embedded command(s) for the Apache Web server. Web servers normally do not parse HTML pages before delivery to the Web browser (or to any other Web client). However, before delivery the Web server always parses an SSI-enabled HTML page, and if any special SSI command is found in the page, it is executed. Figure 13-1 shows the simplified delivery cycle of an HTML page and an SSI-enabled HTML (SHTML) page from a Web server. 13 13 CHAPTER ✦✦✦✦ In This Chapter Understanding Server Side Includes Setting up Apache for Server Side Includes Applying Server Side Includes in Web pages ✦✦✦✦ g4821-2 ch13.F 2/22/02 10:27 AM Page 377 378 Part III ✦ Running Web Applications Figure 13-1: A simplified delivery cycle diagram for an HTML page and an SHTML page As you can see, the SSI version of the HTML page is first parsed for SSI commands. These commands are executed, and the new output is delivered to the Web browser (that is, the Web client.) Apache implements SSI as an INCLUDES filter. Before you can configure Apache for SSI, you need to check your current Apache executable ( httpd) to ensure that the mod_include module is included in it. I show you how in the next section. Configuring Apache for SSI Before you use SSI with Apache, you need to make sure SSI support is enabled. To find out if you have mod_include built into your current Apache binary, run the httpd -l | grep include command from the /usr/local/apache/bin directory or from wherever you have installed the Apache binaries. This enables you to see the Web Server Web Server Parse & process SSI commands Retrieve /index.shtml Retrieve /index.html Web Client GET /index.html Web Client GET /index.shtml Output new content Output content of /index.html Simplified HTML Delivery Cycle Simplified SHTML Delivery Cycle g4821-2 ch13.F 2/22/02 10:27 AM Page 378 [...]... using a 24 -hour clock (range 00 to 23 ) %I The hour as a decimal number using a 12- hour clock (range 01 to 12) %j The day of the year as a decimal number (range 001 to 366 ) %m The month as a decimal number (range 01 to 12) %M The minute as a decimal number %p Either a.m or p.m., according to the given time value or locale %S The second as a decimal number g4 821 -2 ch13.F 3 86 2/ 22/ 02 10 :27 AM Page 3 86 Part... current document By using the if-then-else construct, this example sets a different value to the same variable and loads different files ✦ ✦ ✦ 397 g4 821 -2 ch13.F 2/ 22/ 02 10 :27 AM Page 398 g4 821 -2 ch14.F 2/ 22/ 02 10 :29 AM Page 399 14 C H A P T E R Configuring Apache for FastCGI T his chapter discusses FastCGI and how it solves the performance problems inherent in CGI, without introducing the overhead and... request process Figure 14 -2 shows the simplified CGI request-processing model Client (1) Request Web Server (2) Run (3) Output CGI Program Client (5) Response Web Server CGI Program (4) Exit Figure 14 -2: The CGI request-processing model For each CGI request, the following happens (refer to the figure above): g4 821 -2 ch14.F 2/ 22/ 02 10 :29 AM Page 405 Chapter 14 ✦ Configuring Apache for FastCGI 1 Client... g4 821 -2 ch13.F 2/ 22/ 02 10 :27 AM Page 387 Chapter 13 ✦ Server Side Includes (SSI) TABLE_ROW } print ‘’; exit 0; Now notice how this script is being called from the exec_cgi1.shtml file, which is shown in Listing 13-3 Listing 13-3: exec_cgi1.shtml Apache Server 2 - Chapter 13 SSI Example #2... Now that you know how to enable SSI support in Apache, the next section discusses the SSI commands in detail 381 g4 821 -2 ch13.F 3 82 2 /22 / 02 10 :27 AM Page 3 82 Part III ✦ Running Web Applications Using SSI Commands SSI commands are embedded in HTML pages in the form of comments The base command structure looks like this: The value is often... in-memory caching Requests are routed from any child process to a FastCGI application server The FastCGI application process maintains an in-memory cache Note that in some cases a single FastCGI application server would not provide enough performance With multithreading 401 g4 821 -2 ch14.F 4 02 2 /22 / 02 10 :29 AM Page 4 02 Part III ✦ Running Web Applications you run an application process designed to handle... isolation model g4 821 -2 ch14.F 2/ 22/ 02 10 :29 AM Page 403 Chapter 14 ✦ Configuring Apache for FastCGI www.nitec.com Internet Web Server System TCP connection FastCGI System fcgi.nitec.com Figure 14-1: FastCGI on a remote machine When CGI- and API-based applications become performance bottlenecks because of heavy load; the typical solution is to get either a more powerful Web server or more Web servers to run... see what happens when Apache parses it Figure 13 -2 shows what is returned to the browser when this page is parsed by the server Figure 13 -2: Example of the config errmsg command As you can see from the figure, the second command caused a parse error, and the error message is displayed as a result The message appears where the command is found 383 g4 821 -2 ch13.F 384 2/ 22/ 02 10 :27 AM Page 384 Part III... Apache Server 2 - Chapter 13 Simple SSI Example #1 Example of the SSI config errmsg command: Embedded commands: <!-#config errmsg=”SSI error! Please notify the webmaster.” -> <!-#config badcommand=”whatever” -> g4 821 -2 ch13.F 2/ 22/ 02 10 :27 AM Page... CGI for writing Web -server applications in a variety of languages, including Perl, C, C++, Java, and Python The existence of CGI, FastCGI, and the Server API creates a great deal of confusion for developers and server administrators To shed some light on this murky subject, Table 14-1 provides some key feature comparisons among these technologies ✦ ✦ ✦ ✦ g4 821 -2 ch14.F 400 2/ 22/ 02 10 :29 AM Page 400 Part . in the error log will similar to this: [Tue Mar 20 20 :04 : 26 20 01] [error] [client 20 7.183 .23 3.19] num1 is less than num2 g4 821 -2 ch 12. F 2/ 22/ 02 10 :27 AM Page 373 374 Part III ✦ Running Web Applications The. pairs on standard input) num1=100 num2 =20 0 Content-Type: text/html; charset=ISO-8859-1 100 + 20 0 = 100 g4 821 -2 ch 12. F 2/ 22/ 02 10 :27 AM Page 3 72 373 Chapter 12 ✦ Running CGI Scripts Debugging by. CGI::Debug; my $query = new CGI; g4 821 -2 ch 12. F 2/ 22/ 02 10 :27 AM Page 374 375 Chapter 12 ✦ Running CGI Scripts my $num1 = $query->param(‘num1’); my $num2 = $query->param(‘num2’); #print $query->header; print

Ngày đăng: 14/08/2014, 06:22

Tài liệu cùng người dùng

Tài liệu liên quan