Transcript Web Server

Web Server (Apache httpd )
1
Apache Web Server
•
•
•
•
A PAtCHy server: developed by the Apache group
History-http://httpd.apache.org/ABOUT_APACHE.html
First official public release (0.6.2) in April 1995
Add adaptive pre-fork child processes (very
important!).
• Port to multiple platforms. Add documentation.
• Apache 1.0 was released on 12/1/95.
2
Compiling httpd-2.2.0
• Download httpd-2.2.0.tar.bz2 from http://www.apache.org/dist or closer
mirror sites
• $tar xjf httpd-2.2.0.tar.bz2
• $ ./configure --prefix=PREFIX
• $ make
• $ make install
• $ PREFIX/bin/apachectl start
• Here PREFIX is the prefix of the directory containing the distribution,
typically it is /usr/local/apache.
Since as a normal user, we donot have permission to install there, I
specify PREFIX as
/users/server/students/cs526/public_html/apache2.2m/http
d-2.2.0
• For configuring the apache with specific features, we can
specify the corresponding features as option to the configure
command. You can find the list of features by “./configure –
help”
• Here is the command we used to compile the htttpd with
proxy and cache modules we need.
3
Apache Exercises
• Each site.<exercise> directory contains
– conf: configuration files, httpd.conf, mime.types
– htdocs: contains web pages
– logs: access_log, error_log, httpd.pid
– cmd: alias of “<path>httpd -d
serverrootDirectory -X”
Here <path> specify the directory contains the
httpd program
-d specifies the server root directory, -X single
process execution
4
Httpd Configuration File
• Apache uses a set of directives to tell httpd
how the web site should be configured.
http://www.apache.org/docs/mod/directives.html
Each Apache configuration directive is described using a common format
that looks like this:
Syntax: directive-name some args
Default: directive-name default-value
Context: context-list
Override: override
Status: status
Module: module-name
Compatibility: compatibility notes
5
Block Directives
• Directives that limit the application of other
directives.
• Specify by a group like a tag section in html.
• <VirtualHost host[:port]>
...
</VirtualHost>
• <VirtualHost…><Directory dir>, <Files file>,
<Location URL> in ascending order of authority.
<Location> can overwrite others.
• dir, file, URL can specify using wildcards and full
regular expressions preceded by “~”
6
List of Directives
• User, Group: specify user and group that httpd runs on.
• ServerName: hostname of server
• ResourceConfig, AccessConfig: for reading additional
related directives. Can be disabled by /dev/null as value
• Listen: specify the port httpd run on (Port directive is
deprecated)
• ServerAdmin:email addr. for browser to do automatic
replies.
• DocumentRoot:
• TransferLog, ErrorLog, PidFile: where access,error logs,
httpd.pid should be located.
7
Performance Related Directives
• KeepAlive [on|off](on): keep connection alive for n requests
before terminate provided they come in before timeout. n is
defined in
MaxKeepAliveRequests <n>(100) directive
• KeepAliveTimeout <n>(15): wait for the next request for n
seconds before terminate the connections.
• Timeout <n>(300): max. time in sec for a block data.
• HostNameLookups [on|off|double](off): do reverse DNS lookup
for logging the domain name of the request.
• MaxClients <n>(256): the limit of # of simultaneous requests
(hence the # of child processes).
• MaxRequestsPerChild <n>(0): Spare(child) server dies after <n>
requests, avoid mem leak. 0 mean infinite requests.
8
Web Hosting
• There are a few way we can host a web site:
– Named-based Virtual Hosting
– IP-based Virtual Hosting
– Virtual Machine Virtual Hosting
• Name-based Virtual Hosting
– A set of hostnames shared the same IP address (similar to alias)
– utilize the HOST: meta header in http request (browser fill in the hostname) to
distinguish different web site.
– Each hostname will have its own site configuration, document root.
– Require either the set of hostnames are registered DNS names or the client
machines need to configure their ip addresses mapping in hostfiles such as
/etc/hosts (Unix) or C:\WINDOWS\system32\drivers\etc\hosts
(Windows)
• IP-based virtual Hosting:
– Require a unique IP address for each virtual hosting site
– Use IP alieas to configure the same Network Interface Card (NIC) to listen to
different IP address, e.g., ifconfig eth0:1 128.198.160.33
– Some Unix system sets limit on how many IP aliases can be supported.
• Use <VirtualHost hostname[:port]> block directives
• Specify ServerAdmin, DocumentRoot, ServerName, ErrorLog, TransferLog
for individual VH
9
Virtual Hosts
• The apache server can handle multiple “web sites” at a time
– a web service provider company may have multiple different
sites to offer (see figure 6-2)
– a single company may wish to partition their web site into
multiple domains (e.g., sales, accounting, management,
research, etc) (see figure 6-1)
– a single organization may wish to give each user their own web
site
• In any of these cases, the idea is to have the web server
create several virtual hosts
– although in the last case, we just divide the file space up
• We use the <VirtualHost> container for this, which allows
each “host” to have their own server-specific configuration
– we can divide our hosts up based on IP addresses or IP aliases
<VirtualHost ipaddress>
– the container looks like this:
ServerName ipalias
DocumentRoot path
</VirtualHost>
10
IP-Based
• Assume that we want each of our servers to have a
unique IP address
– in fact, we will have to set up the DNS entries so that all
of the different IP addresses resolve to the same IP
address, that of our web server
• see figure 6-3
• Now we place in our httpd.conf file <VirtualHost>
containers for each server
– one container per server
– this allows us to add more servers over time (or remove
servers) just by modifying the httpd.conf file
• each virtual host will apply the overall server configuration but
you can also specify server-specific directives for each virtual
host as is needed
• many of the directives we saw from chapter 5 that dealt with 1
server can be applied to any, each or some combination of
servers
11
• Each <VirtualHost> container
will have its own unique IP
address
• To the right, you see three
different servers
• Notice how each server maps
to a different location under
DocumentRoot
• We might create symbolic
links so that each company
can edit their own directory
from a location that they are
more familiar with
• We can also set up our
various servers to respond to
different ports of the same IP
address (see the right below)
More
<VirtualHost 172.19.31.1>
ServerName www.company1.com
DocumentRoot /home/company1
</VirtualHost>
<VirtualHost 172.19.31.2>
ServerName www.company2.com
DocumentRoot /home/company2
</VirtualHost>
<VirtualHost 172.19.31.3>
ServerName www.company3.com
DocumentRoot /home/company3
</VirtualHost>
<VirtualHost 172.19.31.1:80>
ServerName www.company1.com
DocumentRoot /home/company1
</VirtualHost>
<VirtualHost 172.19.31.1:8080>
ServerName www.company2.com
DocumentRoot /home/company2
</VirtualHost>
12
Name-Based
• If we want all of our servers
to share the same IP
address, there is only one
major difference required to
the apache server
– we must add the directive
NameVirtualHost ipaddress to
our httpd.conf file where
ipaddress is the shared IP
address of all servers
• this same address will be placed
in each server’s <VirtualHost>
container
• We will also have to ensure
that all DNS servers resolve
any of the servers’ aliases to
this same address
NameVirtualHost 172.19.31.1
<VirtualHost 172.19.31.1>
ServerName www.company1.com
DocumentRoot /home/company1
</VirtualHost>
<VirtualHost 172.19.31.1>
ServerName www.company2.com
DocumentRoot /company2
</VirtualHost>
<VirtualHost 172.19.31.1>
ServerName www.company3.com
DocumentRoot /home/company3
</VirtualHost>
13
• We can use the name-based
approach to allow multiple alias
names to map to the same server
by supplying numerous
ServerAlias directives within a
VirtualHost container
– for instance, imagine that
www.company1.com wishes to
also be recognized by
company1.com,
www.company1.org,
www.company1.net and
sales.company1.com
• We can also permit the * or ?
wildcard to save on the number
of entries we might want to put
into our container
– however, this can easily result in
problems with respect to have
misspelled server names map to
the wrong thing
More
<VirtualHost 172.19.31.1>
ServerName www.company1.com
ServerAlias company1.com
ServerAlias www.company1.org
ServerAlias www.company1.net
ServerAlias sales.company1.com
DocumentRoot /home/company1
</VirtualHost>
Or
<VirtualHost 172.19.31.1>
ServerName www.company1.com
ServerAlias company1.com
ServerAlias *.company1.com
ServerAlias www.company1.*
DocumentRoot /home/company1
</VirtualHost>
14
Some Additional Comments
• If we just add the previous entries into our httpd.conf file,
it does us no good unless we also ensure that DNS entries
have the same mappings
– for instance, if you are at site X and issue an http request for
www.company1.org, if the DNS for site X does not map
www.company1.org to 172.19.31.1, then the http request
never makes it to our server for processing!
– so we have to make sure that the DNS tables across the
Internet reflect the proper mappings
• we have already set up a BIND server, so we will explore this in the
next labs
• You can use both IP-based and Name-based virtual hosts
if desired by having some VirtualHosts share the same IP
address and others have different addresses
– this will require that you use NameVirtualHost for any shared
address
15
Continued
• If you are using name-based virtual hosts, then any
http request that reaches your server will already
have had the IP address resolved
– because of this, we do not have to put IP addresses in our
VirtualHost containers, but can replace them with *
• the * will match any IP address
• only do this with the name-based approach, if you use the IPbased approach, then each virtual host maps to a unique
address
• If your computer has multiple IP addresses (e.g., an
internal address and an external address)
– you can place all IP addresses in the VirtualHost container
(and the NameVirtualHost directive)
• NameVirtualHost 172.19.31.12
• NameVirtualHost 10.11.31.12
• <VirtualHost 172.19.31.12 10.11.31.12> … </VirtualHost>
16
Include Files
• Recall that not all directives need to be placed in
the httpd.conf file
– in fact, for virtual hosts, you would not want this
because each server’s web administrator(s) will need
to edit the configuration file for that server
• if all directives were shared in one file, then you would
have to give many different people access to that one file
– therefore, we allow the administrators to place these
server-wide directives in a different file, which is
then included with the httpd.conf directives via
include directives
17
Overriding httpd.conf Directives
• Recall that the outermost context is the server’s settings
– we do not want our individual administrators to have access to
httpd.conf but we want to allow them to have their own specific
directives in their include files
– so, in our httpd.conf file, we need to add the AllowOverride
directive to any DocumentRoot directory
• for instance, in the <VirtualHost> container for company1, you will add
<Directory /home/company1> and place the AllowOverride All directive in
that container
– the question is, do we want to use “All” in our AllowOverride or be
more specific?
• AllowOverride can have None,
All, or any subset of this list:
– AuthConfig, FileInfo, Indexes,
Limit, Options
• see the example to the right
<Virtualhost 172.19.31.1>
ServerName www.company1.com
DocumentRoot /home/company1
<Directory /home/company1>
AllowOverride Indexes Options
</Directory>
18
</VirtualHost>
Example
NameVirtualHost 172.19.31.12
<Directory /var/www/hosts>
Order allow,deny
Deny from all
Establish protection for
</Directory>
directories above any
<VirtualHost 172.19.31.12>
virtual host’s directory
ServerName www.company1.com
DocumentRoot /var/www/hosts/company1
<Directory /var/www/hosts/company1> Override any directives and
protection from the previous
AllowOverride all
directory
Allow from all
</Directory>
</VirtualHost>
<VirtualHost 172.19.31.12>
ServerName www.company2.com
DocumentRoot /var/www/hosts/company2
<Directory /var/www/hosts/company2>
AllowOverride all
Allow from all
</Directory>
</VirtualHost>
19
Web Site Structure
20
Virtual Host Performance and Status
• The only real drawback to using virtual hosts is that
the more hosts you have, the worse the server’s
performance will be
– running 10 servers instead of 1 will probably result in 10
times the number of requests to service and thus the
server will be 10 times more busy
– running 10 servers instead of 1 will probably require 10
times the amount of disk usage
• an ISP that wishes to provide web server services will have to
take this into account when purchasing server hardware
• To determine the status of the various servers, use
httpd –S which will list
– IP address and port (if different from the default server)
– name of the server
– location of DocumentRoot for the given server
• we will examine status information in more detail in chapter 721
Remember
• Basic Configurations
• Virtual Hosting & its Types
22