The three most famous web servers are IIS in Windows, while Appache HTTP server (httpd) in Linux/Unix. In addition, there is a Tomcat Server dedicated to running java code, of course, it is followed by java cross-platform.
In fact, Appache HTTP Server is also cross-platform. For example, I used the version in Windows for the past two days.
Basic Concepts
Apache HTTP Server, Apache Server, or httpd is a Web Server that supports Unix and Linux and provides plug-in mechanisms. The so-called Web Server refers to the software used to host web sites. The host content includes: database, static content and runnable code.
Assume that you have created a website. Whether it is a few simple static webpages, or a complex dynamic website containing databases or cgi, you must have a web server for hosting, implement data transmission between browser-server.
Note: Here is a good introduction to dynamic web pages and static Web pages.
Download and install
- Apache official website provides the Win32 version of msi
- After the download and installation are complete, you can see the approximate directory structure:
Bin-httpd and other main executable files
Cgi-bin-cgi script, Program
Conf-configuration file, httpd. conf, mime. types... the following settings are determined by the configuration in the configuration file.
Htdocs-main directory of static html and javascript files
Include-sdk used to compile the module
Lib-sdk used to compile the module
Logs-execute log. If an error occurs, refer
Manual-Documentation
Modules of modules-httpd are suffixed with so. In windows, they are actually dll
- Enter http: // localhost/in the browser. The page displays: It Works! In this case, the index.html file under the htdocsdirectory is used.
- Enter http: // localhost/cgi-bin/printenv in the browser. pl, you can see that all the related environment variable values are printed on the page, which is actually executed cgi-bin/printenv. pl files
Operation practices
To do this, you need to display and add data in an sqlite database on the web page, so you do not need to manually open the database and select or insert it.
These functions can be implemented using SSI and CGI. Here are two good introductions about SSI and CGI:
- CGI: Dynamic Content
- Server Side codecdes (SSI)
Basically, you need to configure httpd. conf as follows to enable these functions:
# SSI
Options +Includes
AddType text/html .shtml
AddOutputFilter INCLUDES .shtml
# CGI
ScriptAlias /cgi-bin/ "D:/System/Apache Software Foundation/Apache2.2/cgi-bin/"
Then, create a main.shtml file to design the overall webpage framework. Where data needs to be read from the database, use ssi:
<table border="1">
<tbody>
<tr>
<th valign="top" width="174">Date</th>
<th valign="top" width="114">Goodjob</th>
<th valign="top" width="241">Explain</th>
</tr>
<!--#exec cgi="/cgi-bin/webpm/yexiaodata.pl"-->
</tbody></table>
In the cgi script, call sqlite3 to open the database, query the data, format the data, and print it out. This script will be called every time the page is loaded or refreshed.
Note that the first line of the cgi script must print content type:
print "Content-type: text/plain;
or
print "Content-type: text/html;
For cgi, You need to specify the parser for the script in the first line, for example:
#!"C:/Program Files/perl64/bin/perl.exe"
Of course, you can also use the C language to directly write a feasibility file as cgi.
Perl and c both have some mature cgi libraries:
- Perl: http://search.cpan.org /~ Lds/CGI. pm-3.43/CGI. pm
- C: http://www.boutell.com/cgic/
Then, insert the data, use html form, design the required data input, and set the form action to a cgi script:
<form action="/cgi-bin/webpm/updateyexiao.pl" method=GET>
<fieldset>
<legend>New: </legend>
Date:<br><input name="date" size="30" value="2012-01"> <br>
Goodjob:<br><select name="goodjob"><option selected>1</option><option>0</option></select><br>
Explain:<br><textarea name="explain">NULL</textarea><br>
<input value="Submit" type="submit">
</fieldset>
</form>
In this way, when the submit button is pressed, the data in form is passed to the cgi script through the url, and the cgi script can be obtained through the QUERY_STRING environment variable:
REQUEST_METHOD: GET
QUERY_STRING: date=2012-01&goodjob=1&explain=NULL
Therefore, we can parse user input data in the cgi script and make up an SQL statement to insert it into the database.
This is generally the case:
# Get the name and value for each form input:
my @pairs = split(/&/, $ENV{"QUERY_STRING"});
my %Form;
# Then for each name/value pair....
foreach my $pair (@pairs) {
# Separate the name and value:
my ($name, $value) = split(/=/, $pair);
# Convert + signs to spaces:
$value =~ tr/+/ /;
# Convert hex pairs (%HH) to ASCII characters:
$value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
# Store values in a hash called %FORM:
$Form{$name} = $value;
}