Apache installation configuration and Virtual Host configuration tutorial

Source: Internet
Author: User
Tags format error code header html page iis include variables password protection

Apache 2.0

Installation

Compared to IIS to establish a more professional Apache site, some large portal sites and large traffic of professional sites are published through the Apache site. And many of the previous procedures have been completed through the ASP language, ASP and IIS compatibility is better, at that time IIS users more. With the popularity of PHP language, more and more users abandon the ASP. Apache is more compatible with PHP than IIS, more efficient at executing code, and more stable to run.

First step: Download the Apache 2.0 installer and run it.

Step Two: agree to the license agreement point "NEXT" button to continue.

Step Three: Review the help information without changing the point "NEXT" button to continue.

Fourth step: In the Server Information Settings window to fill in the site's domain name, server name and administrator e-mail address and other information. If you don't have a domain name, it's okay to keep the default. Click the "NEXT" button to continue the installation.

Fifth Step: Choose the installation type, generally keep the default typical typical mode can be.

Sixth step: Set up the installation directory, default to C:\Program Files\apache Group. We can modify this path by clicking the "Change" button on the right.

Step Seventh: Click the Install button to start the installation.

Step Eighth: Copy the required files to the local hard drive and start the Apache main program.

nineth Step: Complete the Apache installation work, point "Finish" button to return to the desktop.

Tenth step: At the same time we will see the desktop in the lower right corner of the taskbar appears a small leaf icon, this is the Apache launch.

The 11th step: We open IE browser in the address bar input http://127.0.0.1, if you can see the display of the Apache page will indicate that we have successfully completed the installation work.

Virtual Host Configuration

First, open the httpd.conf configuration file with the Notepad program. This file is stored in the "Conf" folder under the Apache Server installation path.

first, the use of Mod_vhost_alias

1. Simple Dynamic Virtual host

(1) Get server name from Host: header

Usecanonicalname off

(2) Here the log format, you can in the future through the first parameter field to separate the different virtual host log

Logformat "%V%h%l%u%t \%r\"%s%b "Vcommon customlog logs/access_log Vcommon

(3) Include the server name in the path that returns the file name of the request

Virtualdocumentroot/www/hosts/%0/docs
Virtualscriptalias/www/hosts/%0/cgi-bin

2. More efficient virtual host based on IP address

(1) from the IP address to the resolution of the name of the server (server name)

Usecanonicalname DNS

(2) Include IP address in the log to facilitate subsequent distribution

Logformat "%A%h%l%u%t \%r\"%s%b "Vcommon customlog logs/access_log Vcommon

(3) Include the IP address in the file path

Virtualdocumentrootip/www/hosts/%0/docs
Virtualscriptaliasip/www/hosts/%0/cgi-bin

ii. use of mod_rewrite

1. Using Mod_rewrite to implement simple dynamic virtual host

(1) Get server name from Host: header

Usecanonicalname off

(2) Divisible log

Logformat "%{host}i%h%l%u%t \"%r\ "%s%b" Vcommon customlog logs/access_log vcommon <DIRECTORY/WWW/HOSTS&G
t;
EXECCGI is needed this because we can "T force
CGI execution in the way that Scriptalias does
Options followsymlink s execcgi
</Directory>

(3) Next is the key part

Rewriteengine on
a ServerName derived from a host:header may is any case at all Rewritemap lowercase

First process the normal document:

Allow variable name/icons/to function-other variable names are similar
rewritecond%{request_uri}!^/icons/allow CGIs rewritecond the%{request_uri
}!^/ Cgi-bin/
begins "Conjuring"
rewriterule ^/(. *) $/www/hosts/${lowercase:%{server_name}}/docs/$1

Now deal with CGIs-we need to force a MIME type

Rewritecond%{request_uri} ^/cgi-bin/
rewriterule ^/(. *) $/www/hosts/${lowercase:%{server_name}}/cgi-bin/$1 [T =application/x-httpd-  CGI]


2. Use a stand-alone virtual host configuration file Vhost.map

(1) The Vhost.map file contains something similar to the following:

WWW.CUSTOMER-1.COM/WWW/CUSTOMERS/1
www.customer-2.com/www/customers/2 ...
Www.customer-N.com/www/customers/N

(2) The http.conf contains:

Rewriteengine on
rewritemap lowercase int:tolower

(3) Defining image files

Rewritemap Vhost Txt:/www/conf/vhost.map

(4) As in the example above, processing variable names

Rewritecond%{request_uri}!^/icons/
rewritecond%{request_uri}!^/cgi-bin/rewritecond ${lowercase:%{
server_name}} ^ (. +) $

(5) file based remapping is done here

Rewritecond ${vhost:%1} ^ (/.*) $
rewriterule ^/(. *) $%1/docs/$1 rewritecond%{request_uri
} ^/cgi-bin/
Rewritecond ${lowercase:%{server_name} ^ (. +) $
rewritecond ${vhost:%1} ^ (/.*) $ rewriterule ^/
(. *) $%1/ Cgi-bin/$1

. htaccess file Features

The features of the. htaccess file: Distributed configuration files provide a way to change the configuration of a directory by placing a file containing one or more directives in a specific document directory for this directory and all its subdirectories. As a user, the commands you can use are limited. Administrators can set up by using Apache's allowoverride directives. Directives in subdirectories overwrite instructions in higher-level directories or primary server configuration files.


I. Locating the wrong document

(1) Common client request error return code:

401 Authorization Required 403 Forbidden 404 Not Found 405 Method not 
allowed 
408 Request Timed out 
4 One Content Length Required 
412 precondition Failed 
413 request Entity Too long 
414 request URI Too Long 
41 5 Unsupported Media Type 

(2) Common server error return code:

Internal Server Error 

(3) Users can use the. htaccess to specify their own prepared Error alert page. In general, people can set up a directory specifically, such as errors to place these pages. And then again. htaccess, add the following instructions:

ErrorDocument 404/errors/notfound.html 
errordocument 500/errors/internalerror.html 
A single line of instructions.


The first instruction above means that the page is displayed as a notfound.html page in the/errors directory for 404, which is when the required document is not found. It is not difficult to see that the syntax format is:

ErrorDocument error code/directory name/filename. extension 

If you need to be prompted with very little information, you don't have to create a page to use the HTML number directly in the instruction, such as the following example:

ErrorDocument 401 "<body bgcolor= #ffffff >

You do not have permission to access the page!

</body> "

Second, the password protection of document access to use. htaccess the Access user and corresponding password for a document in a directory, the first thing to do is to generate a. htpasswd text document, for example:

Zheng:y4e7ep8e7eyv 

The password is encrypted, and the user can find some tools to encrypt the password into a. htaccess supported encoding. This document is best not to be placed in the WWW directory, it is recommended to be placed outside the WWW root document, which is more secure.

With the authorized user documentation, you can add the following directive to the. htaccess:

AuthUserFile. HTPASSWD Server directory 
authgroupfile/dev/null (directory that requires authorized access) 
authname enterpassword authtype 
Basic (Authorization type) 

Require user wsabstract (users who are allowed to access, you can use require valid-user if you want all users in the table to allow it)

Note: brackets part for the study time to add their own notes

third, deny access from an IP

If I don't want a government department to access the content of my site, I can reject it by joining the department's IP in the. htaccess. For example:

Order Allow,deny 
deny from 210.21.112.43
deny 219.146.95 
allow from 

Note: The second line rejects an IP, and the third line rejects an IP segment, which means that 219.146.95.0~219.146.95.255 wants to reject everyone? With the Deny from all. Not only with IP, you can also use domain name to set.

iv. Protection of. htaccess documents

When you use. htaccess to set the password protection for a directory, it contains the path to the password file. From a security point of view, it is necessary to protect the htaccess, and not let others see the content. Although this can be done in other ways, such as the permissions of the document. However, htaccess itself can do so by simply adding the following instructions:

<files .htaccess> order 
allow,deny 
deny 
</Files> 

v. URL-turn

We may be planning the site, migrating the document, or changing the directory. At this point, access from the search engine or other web links may be wrong. In this case, the old URL can be automatically shifted to the new address by using the following instructions:

Redirect/old directory/old document name new document address 
or whole directory of the direction: 
Redirect old directory new directory 
change default home file 

Under normal circumstances, the default home page name has default, index and so on. However, there are times when there are no default files in the directory, but a specific file name, such as pmwiki.php in PmWiki. In this case, it is troublesome for the user to remember the filename to access. You can easily set a new default filename in. htaccess:

DirectoryIndex the new default filename 

You can also list multiple, sequentially indicating the priority level between them,

For example:

DirectoryIndex filename.html index.cgi index.pl default.htm

Rewrite rewrite feature

Apache module Mod_rewrite, an engine for real-time rewrite URL requests based on certain rules this module provides an overriding engine based on the regular Expression Analyzer to rewrite URL requests in real time. It supports a flexible and powerful URL manipulation mechanism for each complete rule that can have an unlimited number of child rules and additional conditional rules. This URL action can depend on a variety of tests, such as server variables, environment variables, HTTP headers, time tokens, and even a lookup database of various formats that match the components of a URL. This module can manipulate all parts of the URL, including the Path Information section, at both the server-level (httpd.conf) and directory-level (. htaccess) configurations, and generate the final request string. The result of this rewrite operation can be an internal child processing, or an external request, or even an internal proxy processing. However, all of these features and flexibility pose a problem, which is complexity, so don't expect to be able to read the entire module within a day. For more discussion, details, and examples, see the detailed URL rewrite document.

(1) references to special characters:

In the Apache 1.3.20, special characters in teststring and substitution can be escaped with a leading slash (\) (that is, ignore their special meaning
as ordinary characters). For example, substitution can use "\___fckpd___27quot" to contain a dollar sign to avoid mod_rewrite view it as a reverse reference.

(2) environment variables

This module tracks two additional (nonstandard) Cgi/ssi environment variables, Script_url and Script_uri. They contain a logical network view of the current resource, while standard Cgi/ssi variables script_name and script_filename contain physical system views. Note: These variables hold the Uri/url when they were originally requested, that is, the uri/url before any rewrite operation. The importance is that they are the original basis for overriding the URL to the physical path name of the rewrite operation.

Example:

script_name=/sw/lib/w3s/tree/global/u/rse/.www/index.html
script_filename=/u/rse/.www/index.html
script_url=/u/rse/
script_uri=http://en1.engelschall.com/u/rse/

(3) Rewrite's instructions

Rewrite's instructions are many, like rewritebase instructions.

The rewritebase directive explicitly sets the base URL for a directory-level override. In the following, you will see that rewriterule can be used in directory-level configuration files (. htaccess) and in a local scope, that is, the rule actually handles only part of the local path prefix being stripped. After the process is finished, the path is automatically attached back. The default value is "Rewritebase physical-directory-path".

When a new URL is replaced, the module must inject the URL back into the server process. To do this, it must know its corresponding URL prefix or URL reference. Typically, the prefix is the corresponding file path. However, most web site URLs do not directly correspond to their physical file path, and therefore generally do not make such an assumption! So in this case, you must use the Rewritebase directive to specify the correct URL prefix.

If your site server URL does not correspond directly to the physical file path, and you need to use the Rewritebase directive, you must specify the Rewriterule in each of the corresponding. htaccess files.


For example, the contents of a directory-level configuration file are as follows:

/abc/def/.htaccess--/abc/def directory profile
Note:/abc/def is the physical path to/XYZ (for example, there is a "alias/xyz/abc/def" directive).
Rewriteengine
on Let the server know that we are using/xyz instead of physical paths/abc/def
rewritebase   /xyz
rewrite rules
rewriterule   ^oldstuff\.html$  newstuff.html

Note: in the above example, the request for/xyz/oldstuff.html is correctly rewritten as a request to the physical file/abc/def/newstuff.html.

The detailed steps for internal processing are listed below:

Request:
/xyz/oldstuff.html
Internal process:
/xyz/oldstuff.html     ->/abc/def/oldstuff.html  (per-server Alias)
/abc/def/oldstuff.html->/abc/def/newstuff.html  (per-dir    rewriterule)
/abc/def/ newstuff.html->/xyz/newstuff.html      (per-dir    rewritebase)
/xyz/newstuff.html     ->/abc/def /newstuff.html  (per-server Alias)
Result:
/abc/def/newstuff.html

While this process may seem cumbersome, it has to inject this (rewrite) request back into the Apache core because the arrival of the directory-level rewrite is too late, so this is exactly how it is handled within Apache. But: it's not as big as it seems, because the injection is done entirely within the Apache server, and the process is also used by many other operations within Apache. So you can fully trust that its design and implementation are correct.



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.