An analysis of the setting method of SSI and CGI in Apache

Source: Internet
Author: User
Tags add date include modify requires
apache|cgi because Apache is highly portable and supports more than 30 operating systems, including UNIX, Windows, and Darwin, most of the currently registered domains in the network are using Apache Web servers. Currently Apachesoftware Foundation is working to develop the Apache2.0 that has now entered the Alpha test phase. Here, I discuss with you how to modify the server option so that the server can provide simple dynamic web content, that is, support CGI programs and Server-side Include (SSI) programs.

1. Preparation work

First of all, I assume that you have Apache installed and that your Apache can provide static Web pages for browsing. The Apache installation automatically attaches a static HTML test page, which means that if you can see the test page, your Apache will work. The basic installation of Apache can only provide static HTML Web pages. However, you can improve its functionality by using a module (modules). In the original settings, Apache compilation will contain the Mod_include and moc_cgi two modules. You can do it in the bin subdirectory./httpd-l to see if your Apache has these two modules. The output that executes will be a long string of all the modules that Apache now installs. If the two modules Mod_include and moc_cgi are not in the list, you must recompile the server. When recompiling, determine the instructions that follow how to include the Mod_include, moc_cgi modules. In addition, you must also have a real-time web page server-side Includes (Related information URL:http://www.oreilly.com/catalog/apache/excerpt/ch10.html). With Server-side includes (SSI) support, you can create real-time dynamic Web pages. Next, I start with the SSI support settings for the server, and then I go into CGI writing.

2, the Apache setting

First you have to find the Apache configuration file. Apache's original installation directory is/usr/local/apache under UNIX and C:\Program Files\apache under Windows. Next in the Conf subdirectory you will find the httpd.conf file. This is the Apache configuration file. This setup file is a text-only file, so you can edit it using a generic text editor, such as VI or Notepad. The first thing to note is that the start text of some lines in this profile is the # symbol, which means that the line's text is all annotated. It's a good practice to make comments appropriately in your profile because it helps you remember what you've done and why.

3, the implementation of SSI program #addhandler server-parsed. shtml

Deletes the # symbol before the AddType and AddHandler these two lines of instruction. The ADDTYPE directive requires the server to use text or HTML as the content format for the returned file when it returns any Web page with a. shtml name. AddHandler is used to instruct the server to send the contents of the file to Mod_include processing. After that, Mod_include will determine how to respond to such a file. Next, look for the following text:

There will be one line of options line between this line of text and the corresponding. The original setting is:

Options Indexes followsymlinks MultiViews

Add includes at the end of the line, and the result looks like this:

Options Indexes followsymlinks multiviews Includes

This requires Apache to execute the Server-side includes program in the Htdocs subdirectory. In order for these modifications to take effect, we must reboot the server. Reactivate under Unix, execute "kill-hup ' cat/usr/local/apache/logs/httpd.pid '". Under Windows, perform "apache-k restart". Now let's try the result of the set. Add a file test.shtml to the/usr/local/apache/htdocs directory. This file must contain the following program code:

The file hello.txt is long and it's last modified on



This SSI program reads a file called Hello.txt and prints the size of the file and the last modified date to the Web page. Obviously, we must also add this Hello.txt file to the Htdocs directory. In my hello.txt file there is only one line of text: how ARE you!. When you're done adding these files, open your usual browser and turn on http://localhost/test.shtmlWeb page. If your server is not installed through root, you may have to switch to http://localhost:8080/test.shtml。 The following results will be obtained:

How ARE you! The file Hello.txt is 1k bytes long and it's last modified on Wednesday, 02-aug-2000 20:18:28 PDT

Another way to activate an SSI-enabled program is known as Xbithack settings (related data URL:http://www.apache.org/docs/mod/mod_include.html). The origin of this method is that when you set the user executable bit (user-executable bit) of a text file to an executing state, Apache treats those files as SSI files.

To activate such a feature, you must place the following instruction (Directive) in the. htaccess file of all directories: Xbithack status on, off, or full. On setting forces the server to treat all user-executable files as SSI items. Off causes the server to completely ignore the set state that the user can perform. If set to full, the server will see all user executable files as SSI items, and will also be able to perform (group-executable bit). If the group executable is set to executable, the value of the last modified time returned to the header is set to the date when the file was most recently modified. Such a setting allows the client's browser and proxy server (proxy) to be cached (caching). However, you must be careful when using such a feature. For example, if you have a Web page that offers a wheeled ad, you won't want to set the group executable bit to open, because doing so will allow the first download of the ad to be quickly fetched, causing the user to no longer see other pages of advertising.

4. Execute CGI Program

In the Apache original installation, two sets of CGI programs, test-cgi, and printenv were attached to the Cgi-bin subdirectory, except that the two sets of programs had potential security vulnerabilities. But since we're just setting up tests and we're not going to put this original setup directly on the main running server (live server), we're still activating one of the CGI programs to see how Apache was set up to execute the set of programs. Finally, we will write a simple CGI program ourselves.

First, make sure that the set of programs is executable. Enter the Cgi-bin subdirectory and make sure that the program files are set to be executable by the user (the server executes the user) and that the group (using the group when the server executes) is executable. For Windows systems, this step should not be necessary. Next, ask the server for something like this:

http://localhost:8080/cgi-bin/test-cgi

Note: You need to specify port 8080 only if the server is installed through a non-root user. This Apache-built test-cgi program lists the variable values that the CGI program accesses. Activation of CGI support is set in the Scriptalias instruction section within the httpd.conf settings file. The original set value for this instruction section is:

scriptalias/cgi-bin/"/usr/local/apache/cgi-bin/"

This line of instructions is to tell Apache if the required page path is Cgi-bin, these files can be found in the/usr/local/apache/cgi-bin/directory. This line of instructions also tells Apache to execute the file in this directory. In the following I prepared a will output "How are you!" Simple CGI program. I'll name it how.sh.

#!/bin/sh
echo "Content-type:text/html"
Echo
echo "How are you!"

Modify the permissions on this file to make it an executable file and make the following requirements to your server:

http://localhost:8080/cgi-bin/how.sh

Although this group of CGI is written in shell script, it can be written in any language that applies to the system. As for the writing about CGI programs, I will discuss it further with you if I have the opportunity.

Conclusion:

All of the SSI commands that Apache supports can be found in apachedocumentation, and all Apache features can be debugged via the config file. What I am introducing here is only the fur of the knowledge of setting up documents. The original settings of the set file have a very detailed description of the file, and each system version with the core modules and standard modules of the documentation, if you spend some time in these files to explore, you will find any of the functions you want.



  

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.