Apache Day1
Apache Day1
H T T P S e r v e r
COURSE MATERIALS
Made with by 2
CONTENTS
• History
• Clients, servers and URLs
• Anatomy of request and response
• HTTP status codes
• Web servers
• Apache HTTP Server components
• Documentation explained
• Directives examples
• .htacess
• Modules
• Virtual hosts
Made with by 3
HISTORY
History
Made with by 4
“ Apache Software Foundation (ASF) is an American non-profit
corporation to support Apache software projects, including the
Apache HTTP Server. The ASF was formed from the Apache Group
in June 1999
Made with by 5
WEB ADDRESS
• Addresses on the Web are expressed with URLs - Uniform
Resource Locators - which specify
• a protocol (e.g. http),
• a servername (e.g. www.apache.org),
• a URL-path (e.g. /docs/current/getting-started.html), and
• possibly a query string (e.g. ?arg=value) used to pass additional
arguments to the server
Status Codes 12
Made with by
HTTP STATUS CODES EXAMPLES
• 200 OK
• 401 Unauthorized
• 403 Forbidden
• 404 Not Found
• 500 Internal Server Error
Status Codes 13
Made with by
WHAT IS A WEB SERVER?
• This is a computer that's sole purpose is to distribute information
that is hosted within its hard drives.
• Depending on the information, it is accessed and distributed
differently.
• A detailed example would be standard Web Pages that are
accessed via the Internet protocol HTTP on port 80 and
distributed back in the same fashion which are stored on a web
server.
Web Servers 14
Made with by
WHAT IS A WEB SERVER?
• Any computer can be turned into a Web server by installing
server software and connecting the machine to the Internet.
• There are many Web server software applications.
Web Servers 15
Made with by
APACHE VS IIS
• Apache is free while IIS is packaged with Windows.
• IIS only runs on Windows while Apache can run on almost any OS
including UNIX, Apple’s OS X, and on most Linux Distributions.
• IIS has a dedicated staff to answer most problems while support
for Apache comes from the community itself.
• IIS is optimized for Windows because they are from the same
company.
• The Windows OS is prone to security risks.
Web Servers 16
Made with by
CONFIGURATION FILES
• The Apache HTTP Server is configured via simple text files.
• The default configuration file is usually called httpd.conf.
• The configuration is frequently broken into multiple smaller files,
for ease of management. These files are loaded via the Include
directive.
Module Module
Core
Module Module
INSTALLATION 20
Made with by
MAIN CONFIGURATION FILE
• Ubuntu:
/etc/apache2/apache2.conf
• CentOS:
/etc/httpd/conf/httpd.conf
INSTALLATION 21
Made with by
PUBLIC WEB FILES / LOG FILES
• Ubuntu:
/var/www/html
/var/log/apache2
• CentOS:
/var/www/html
/var/log/httpd
INSTALLATION 22
Made with by
MAIN CONFIGURATION FILE
INSTALLATION 23
Made with by
DIRECTIVES EXAPMLES
• ServerRoot directive
The path to the server’s configuration
• PidFile directive
The process identification number for the httpd registered at starting the
server
• ServerName directive
This is where you declare the name of your website
• DocumentRoot directive
This is where your web documents (html files, images etc) should be
located.
• Listen directive
The Listen directive instructs Apache httpd to listen to only specific IP
addresses or ports
Documentation explained 25
Made with by
DIRECTIVES DESCRIPTION
This indicates where in the server's configuration files the
directive is legal.
Server config: This means that the directive may be used in
the server configuration files (e.g., httpd.conf), but not within
any <VirtualHost> or <Directory> containers. It is not allowed
in .htaccess files at all.
Virtual host :This context means that the directive may
appear inside <VirtualHost> containers in the server
Context: configuration files.
Directory :A directive marked as being valid in this context
may be used inside <Directory>, <Location>, <Files>, and
<Proxy> containers in the server configuration files, subject to
the restrictions outlined in Configuration Sections.
.htaccess: If a directive is valid in this context, it means that it
can appear inside per-directory .htaccess files. It may not be
processed, though depending upon the overrides currently
active.
Documentation explained 26
Made with by
AllowOverride
Description: Types of directives that are allowed in .htaccess files
Syntax: AllowOverride All|None|directive-type [directive-type] ...
AllowOverride None (2.3.9 and later), AllowOverride All
Default: (2.3.8 and earlier)
Context: directory
Status: Core
Module: core
Directives examples 27
Made with by
KeepAlive
Description: Enables HTTP persistent connections
Syntax: KeepAlive On|Off
Default: KeepAlive On
Context: server config, virtual host
Status: Core
Module: core
Directives examples 28
Made with by
MaxKeepAliveRequests
Description: Number of requests allowed on a persistent connection
Syntax: MaxKeepAliveRequests number
Default: MaxKeepAliveRequests 100
Context: server config, virtual host
Status: Core
Module: core
Directives examples 29
Made with by
ErrorLog
Description: Location where the server will log errors
Syntax: ErrorLog file-path|syslog[:facility]
ErrorLog logs/error_log (Unix) ErrorLog logs/error.log (Windows
Default:
and OS/2)
Context: server config, virtual host
Status: Core
Module: core
The ErrorLog directive sets the name of the file to which the server will
log any errors it encounters. If the file-path is not absolute then it is
assumed to be relative to the ServerRoot
Directives examples 30
Made with by
Directory
Enclose a group of directives that apply only to the named file-
Description:
system directory, sub-directories, and their contents.
Syntax: <Directory directory-path> ... </Directory>
Context: server config, virtual host
Status: Core
Module: core
Directives examples 31
Made with by
DirectoryMatch
Enclose directives that apply to the contents of file-system
Description:
directories matching a regular expression.
Syntax: <DirectoryMatch regex> ... </DirectoryMatch>
Context: server config, virtual host
Status: Core
Module: core
Directives examples 32
Made with by
Files
Description: Contains directives that apply to matched filenames
Syntax: <Files filename> ... </Files>
Context: server config, virtual host, directory, .htaccess
Override: All
Status: Core
Module: core
The <Files> directive limits the scope of the enclosed directives by filename. It
is comparable to the <Directory> and <Location> directives. It should be
matched with a </Files> directive. The directives given within this section will
be applied to any object with a basename (last component of filename)
matching the specified filename
Directives examples 33
Made with by
FilesMatch
Contains directives that apply to regular-expression matched
Description:
filenames
Syntax: <FilesMatch regex> ... </FilesMatch>
Context: server config, virtual host, directory, .htaccess
Override: All
Status: Core
Module: core
The <FilesMatch> directive limits the scope of the enclosed directives by
filename, just as the <Files> directive does. However, it accepts a regular
expression.
Directives examples 34
Made with by
Allow
Description: Controls which hosts can access an area of the server
Allow from all|host|env=[!]env-variable [host|env=[!]env-
Syntax:
variable] ...
Context: directory, .htaccess
Override: Limit
Status: Extension
Module: mod_access_compat
The Allow directive affects which hosts can access an area of the server. Access
can be controlled by hostname, IP address, IP address range, or by other
characteristics of the client request captured in environment variables.
Directives examples 35
Made with by
Deny
Description: Controls which hosts are denied access to the server
Deny from all|host|env=[!]env-variable [host|env=[!]env-
Syntax:
variable] ...
Context: directory, .htaccess
Override: Limit
Status: Extension
Module: mod_access_compat
Directives examples 36
Made with by
Order
Controls the default access state and the order in which Allow
Description:
and Deny are evaluated.
Syntax: Order ordering
Context: directory, .htaccess
Override: Limit
Status: Extension
Module: mod_access_compat
The Order directive, along with the Allow and Deny directives, controls a three-
pass access control system. The first pass processes either all Allow or all Deny
directives, as specified by the Order directive. The second pass parses the rest
of the directives (Deny or Allow). The third pass applies to all requests which
do not match either of the first two.
Directives examples 37
Made with by
CacheEnable
Enable caching of specified URLs using a specified storage
Description:
manager
Syntax: CacheEnable cache_type [url-string]
Context: server config, virtual host, directory, .htaccess
Override: Extension
Status: mod_cache
Enable caching of specified URLs using a specified storage
Module:
manager
The CacheEnable directive instructs mod_cache to cache urls at or
below url-string. The cache storage manager is specified with the
cache_type argument. The CacheEnable directive can alternatively be
placed inside either <Location> or <LocationMatch> sections to indicate
the content is cacheable.
Directives examples 38
Made with by
.htaccess
• .htaccess stands for hypertext access. This is the default name
of the Apache directory-level configuration file.
• One of the most common uses is to require user authentication
in order to serve certain web pages.
• Also you can use it to define rewrite rules and rewrite conditions.
• You need to configure apache using AllowOverride directive
to allow/deny the usage of .htaccess
.htaccess 39
Made with by
.htaccess FOR AUTHENTICATION
#.htaccess content
AuthType Basic
AuthName “Restricted web page”
AuthUserFile “/var/www/.htpasswd”
require valid-user
.htaccess 40
Made with by
.htaccess FOR AUTHENTICATION
• AuthType Basic defines the type of authentication.
• Basic means there is no encryption and the password hash is
sent as clear text. This is one of the major reasons why .htaccess
cannot be considered for protection of confidential user data.
• "Restricted web page" is a window title string. When someone
tries to access an .htaccess-protected page, a username &
password window will pop in the web browser. This window will
bear a title - this is the AuthName. It can be anything you like.
• AuthUserFile /var/www/.htpasswd defines the path to a file
where user credentials are stored. This file does not exist, but we
will create it soon.
• require valid-user indicates only successful authentication
attempts will result in the loading of the page.
.htaccess 41
Made with by