Apache server supports CGI program and SSI program setting method

  
                  

Because Apache is quite portable, it supports more than 30 operating systems, including Unix, Windows, and Darwin. So most of the registered domains on the network use Apache web servers. The ApacheSoftware Foundation is currently working on Apache 2.0, which is now in alpha testing. Here, the web teaching network and everyone discuss how to modify the server option to enable the server to provide simple dynamic web content, that is, support CGI programs and Server-Side Include (SSI) programs. //This article transferred from www.45it.com computer software and hardware application network

1, preparation work

First, I assume that you have installed Apache and your Apache can provide static web pages for browsing. The Apache installation will automatically attach a static HTML test page, which means that if you can see the test page, it means your Apache will work. Apache under basic installation can only provide static HTML pages. However, you can enhance its functionality by using modules. Under the original settings, Apache's compilation will include two modules, mod_include and moc_cgi. You can run ./httpd -l in the bin subdirectory to see if your Apache has both modules installed. The output of the execution will be a long list of all 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 for how to include the mod_include, moc_cgi module. In addition, you must have Server-Side Includes (real-time webpage: 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 server's SSI support settings and then go to CGI.

2, Apache settings

First you must first find the Apache configuration file. The original installation directory for Apache is /usr/local/apache under Unix and c:Program FilesApache under Windows. Then in the conf subdirectory you will find the httpd.conf file. This is the Apache configuration file. This configuration file is a plain text file, so you can edit it using a normal text editor such as vi or Notepad. The first thing to note is that the starting text of some lines in this profile is the # symbol, which means that the text in this line is all annotated. It's a good habit to properly comment in your profile, because that helps you remember what settings you have made and why.

3. Execute the SSI program

Open the configuration file and look for the following text:

#
# To use server-parsedHTMLfiles
#
#AddType text/html .shtml
#AddHandler server-parsed .shtml


Remove the # symbol before the AddLine and AddHandler directives. The AddType directive will ask the server to use text or HTML as the content format for the returned file when returning any web page with the .shtml extension. 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 an option line between the line and the corresponding line. The original settings are:

Options Indexes FollowSymLinks MultiViews

Adding Includes at the end of the line will look like this:

Options Indexes FollowSymLinks MultiViews Includes< Br>

This is to ask Apache to execute the server-side includes program in the htdocs subdirectory. In order for these changes to take effect, we must restart the server. Reactivate under Unix and execute kill -HUP `cat /usr/local/apache/logs/httpd.pid`. Under Windows, execute Apache-k restart. Now let's try the result of the setting. 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 was last modified on

This SSI program will read a file called hello.txt. The size of the file and the most recent modification date are output to the web page. Obviously, we must also add this hello.txt file in the htdocs directory. There is only one line of text in my hello.txt file: HOW ARE YOU!. Once you've finished adding these files, open your favorite browser and open the http://localhost/test.shtml page. If your server installation is not through the root user, you may have to turn on http://localhost:8080/test.shtml instead. After that, you will get the following results:

HOW ARE YOU! The file hello.txt is 1k bytes long and it was last modified on Wednesday, 02-Aug-2000 20:18:28 PDT

Another way to activate support for SSI programs is called XbitHack settings (relevant URL: http://www.apache.org/docs/mod/mod_include.html#xbithack). The origin of this method is that when you set the user-executable bit of the text file to an executable state, Apache treats those files as SSI program files.

To activate such a function, you must place the following directive in the .htaccess file for all directories: XbitHack status on (or full) status can be set to on, off, or full. The setting of on forces the server to treat all user-executable files as SSI items. Off causes the server to completely ignore the setting state that the user can perform. If it is set to Full, the server will treat all user executable files as SSI items, and will also check the 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 time the file was last modified. This setting allows the client's browser and proxy to be caching. However, care must be taken when using such features. For example, if your web page has a rotating billboard, you won't want to set the group executable to be on, because doing so will cause the first downloaded ad to be cached, resulting in use. No more ads for other pages can be seen.

4, the implementation of CGI program

In the original Apache installation, the cgi-bin subdirectory is accompanied by two sets of CGI programs, test-cgi and printenv, but these two groups of programs have potential Security hole. But since we are just going to do the setup test, and we won't put this original setup directly on the live server, we will still activate one of the CGI programs and see how Apache was originally Set to execute this set of programs. Finally, we will write a simple CGI program ourselves.

First, make sure that this set of programs is executable. Enter the cgi-bin subdirectory and make sure that the program file is set to be executable by the user (user at server execution) and can be executed using the group (the group is used when the server is executed). For Windows systems, this step should be unnecessary. Next, ask the server for such content:

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

Note: Only when installing the server through a non-root user You need to specify port 8080. This Apache built-in test-cgi program lists the values ​​of variables that CGI programs will access. Activate CGI support is the ScriptAlias ​​command section set in 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 tells Apache if the requested page The path starts with cgi-bin and 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. Below I have prepared a simple CGI program that will output How are you! I named it how.sh.

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


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

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

Although this group of CGI is It is written in a shell script, but it can be written in any language that is appropriate for the system. As for the writing of CGI programs, I will discuss them more deeply if I have the opportunity.

Conclusion:

All SSI directives supported by Apache can be found in Apache documentation. All Apache functions can be debugged via the config file. What I am introducing here is just the fur of setting up the relevant knowledge of the file. The original settings of the settings file have very detailed documentation, and each system version comes with a description of the core module and standard modules. If you spend some time exploring these files, you will find whatever you want. Features.

Copyright © Windows knowledge All Rights Reserved