Lemur zaprasza
Red Hat® Linux 6 Unleashed
Chapter 9: Apache Server Previous
Sections in this Chapter: Server Installation CGI and SSI Runtime Server Configuration Settings Starting and Stopping the Server Virtual Hosting Configuration File Listings Previous SectionNext Section CGI and SSI The most common way to provide dynamic content on Web sites is with CGI (Common Gateway Interface) programs. The CGI is a specification of communication between server processes (such as programs that generate dynamic documents) and the server itself. Server Side Includes (SSI) allow output from CGI programs, or other programs, to be inserted into existing HTML pages. CGI By default, you may put any CGI program in the ScriptAlias directory on your server. These programs must be executable by the user as which the server is running. This usually means that you will need to change the mode of the files to 755 so that the user nobody can execute them. chmod 755 program.cgi In order to execute CGI programs outside of the ScriptAlias directory, you will need to enable the ExecCGI option for that directory. This is done either in your httpd.conf file (access.conf prior to version 1.3.4) or in an .htaccess file in the directory.CGI programs can be written in any language. The most popular languages for CGI programming are Perl and C. You may want to pick up a good book on CGI programming, such as CGI Programming With Perl, Second Edition, since this is not intended to be a CGI book.To test whether you have CGI configured correctly, try the following CGI program, written in Perl, which displays the values of the HTTP environment variables. #!/usr/bin/perl print "Content-type: text/html\n\n"; print "<html><head><title>Simple CGI program</title></head><body>\n"; for (keys %ENV) { print "$_ = $ENV{$_}<br>\n"; } print "</body></html?\n"; If you are going to be writing CGI programs in Perl, you may want to look at the CGI modules that come bundled with Perl. SSI Server Side Includes (SSI) are directives that are written directly into an HTML page, which the server parses when the page is served to the Web client. They can be used to include other files, the output from programs, or environment variables.The most common way to enable SSI is to indicate that files with a certain filename extension (typically .shtml) are to be parsed by the server when they are served. This is accomplished with the following lines in your httpd.conf file (srm.conf prior to version 1.3.4): # To use server-parsed HTML files # #AddType text/html .shtml #AddHandler server-parsed .shtml By uncommenting the AddType and AddHandler lines, you could tell the server to parse all .shtml files for SSI directives.The less commonly used, and in my opinion much better, way of enabling SSI is with the XBitHack directive. XbitHack can be set to a value of on or off, and can be set in either your configuration file or in .htaccess files. If the XBitHack directive is on, it indicates that all files with the user execute bit set should be parsed for SSI directives. This has two main advantages. One is that you do not need to rename a file, and change all links to that file, simply because you want to add a little dynamic content to it. The other reason is more cosmetic--users looking at your Web content cannot tell by looking at the filename that you are generating a page dynamically, and so your wizardry is just that tiny bit more impressive.In addition to these directives, the following directive must be specified for directories where you want to permit SSI: Options Includes This may be set in the server configuration file or in an .htaccess file. Basic SSI Directives SSI directives look rather like HTML comment tags. The syntax is the following: <!--#element attribute=value attribute=value ... --> The element can be one of the following: config This
errmsg Sets the error message that is returned to the client
Example: <!--#config errmsg="[It's
sizefmt Sets the format used to display file sizes. You can set
Example: <!--#config sizefmt="bytes"
timefmt Sets the format used to display times. The format of the
%% PERCENT %a Day of the week abbreviated. %A Day of the week %b Month abbreviated. %B Month %c ctime format: Sat
%d Numeric day of the month %e DD %D MM/DD/YY %h Month abbreviated. %H Hour, 24-hour clock, leading zeros %I Hour, 12-hour clock, leading zeros %j Day of the year %k Hour %l Hour, 12-hour clock %m Month number, starting with 1 %M Minute, leading zeros %n Newline %o Ornate day of month--1st, 2nd, 25th, and so on %p AM or PM %r Time format: 09:05:57 PM %R Time format: 21:05 %S Seconds, leading zeros %t Tab %T Time format: 21:05:57 %U Week number; Sunday as first day of week %w Day of the week, numerically; Sunday == 0 %W Week number; Monday as first day of week %x Date format: 11/19/94 %X Time format: 21:05:57 %y Year (2 digits) %Y Year (4 digits) %Z Time zone in ASCII, such as PST echo Displays
DATE_GMT The current date in Greenwich Mean Time. DATE_LOCAL The current date in the local time zone. DOCUMENT_ NAME The filename (excluding directories) of the document requested
DOCUMENT_ URI The (%-decoded) URL path of the document requested by
LAST_ MODIFIED The last modification date of the document requested by
exec Executes
cgi The URL of a CGI program to be executed. The URL needs
cmd A shell command to be executed. The results will be displayed
fsize Displays
file The path (filesystem path) to a file, either relative
virtual The relative URL path to a file. flastmod Displays
include Include
printenv Displays all of existing variables. There are no attributes. Example: <!--#printenv --> set Set the value of a variable. Attributes are var
Example: <!--#set var="animal"
Note - All defined CGI environment variables are also allowed as include variables. Note - In your configuration files (or in .htaccess), you can specify Options IncludesNOEXEC to disallow the exec directive, as this is the least secure of the SSI directives. Be especially cautious when Web users are able to create content (like a guestbook or discussion board) and these options are enabled! These variables can be used elsewhere with some of the following directives. Flow Control Using the variables set with the set directive and the various environment variables and include variables, there is a limited flow control syntax that can be used to generate a certain amount of dynamic content on server-parsed pages.The syntax of the if/else functions is as follows: <!--#if expr="test_condition" --> <!--#elif expr="test_condition" --> <!--#else --> <!--#endif --> expr can be a string, which is considered true if non-empty, or a variety of comparisons between two strings. Available comparison operators are =, !=, <, <=, >, and >=. If the second string has the format /string/, the strings are compared with regular expressions. Multiple comparisons can be strung together with && (AND) and || (OR). Any text appearing between the if/elif/else directives will be displayed on the resulting page. An example of such a flow structure follows: <!--#set var="agent" value="$HTTP_USER_AGENT" --> <!--#if expr="$agent = /Mozilla/" --> Mozilla! <!--#else --> Something else! <!--#endif --> This code will display "Mozilla!" if you are using a browser that passes Mozilla as part of its USER_AGENT string, and "Something else!" otherwise. Red Hat® Linux 6 Unleashed
Chapter 9: Apache Server Previous
Sections in this Chapter: Server Installation CGI and SSI Runtime Server Configuration Settings Starting and Stopping the Server Virtual Hosting Configuration File Listings Previous SectionNext Section © Copyright Macmillan USA. All rights reserved. |