Apache Web Server Guide
Apache Web Server Guide
Complete Guide
Dedoimedo
Foreword
I hope this will be the best guide on the Apache Web server you
will have ever read.
The only thing that you will miss is the fact that links to other
Parts, covering other material, are not available in this stand-
alone release. However, every procedure required to setup the Web
1
www.dedoimedo.com all rights reserved
Lastly, let’s get one thing straight: you will not become Apache gu-
rus by reading this document. For that matter, I’m not an Apache
guru, either. There are so many aspects to the usability and secu-
rity of the Apache Web server, it is practically impossible to put
them all in a single book.
2
About
Have fun!
3
Copyright
4
Disclaimer
5
Contents
1 Introduction 11
2 Basic Setup 13
2.1 Verify installation . . . . . . . . . . . . . . . . . . . . . . . . . 13
2.2 Package files . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
2.3 Main configuration file(s) . . . . . . . . . . . . . . . . . . . . . 15
2.4 Backup . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
2.5 Edit the httpd.conf configuration file . . . . . . . . . . . . . . 16
2.5.1 ServerRoot . . . . . . . . . . . . . . . . . . . . . . . . 17
2.5.2 PidFile . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
2.5.3 ServerName . . . . . . . . . . . . . . . . . . . . . . . . 18
2.5.4 /etc/hosts file . . . . . . . . . . . . . . . . . . . . . . . 19
2.5.5 DocumentRoot . . . . . . . . . . . . . . . . . . . . . . 23
2.5.6 ErrorLog . . . . . . . . . . . . . . . . . . . . . . . . . . 23
2.5.7 Listen . . . . . . . . . . . . . . . . . . . . . . . . . . . 25
2.6 Create your HTML documents . . . . . . . . . . . . . . . . . . 26
2.7 Start the Web Server . . . . . . . . . . . . . . . . . . . . . . . 28
2.8 Access the web site . . . . . . . . . . . . . . . . . . . . . . . . 30
2.8.1 Local access . . . . . . . . . . . . . . . . . . . . . . . . 30
2.8.2 External access . . . . . . . . . . . . . . . . . . . . . . 32
2.9 Summary of basic setup . . . . . . . . . . . . . . . . . . . . . 36
6
www.dedoimedo.com all rights reserved
3 Advanced setup 37
3.1 Directory tags . . . . . . . . . . . . . . . . . . . . . . . . . . . 37
3.1.1 Order (allow, deny) . . . . . . . . . . . . . . . . . . . . 40
3.1.2 Indexes . . . . . . . . . . . . . . . . . . . . . . . . . . 43
3.1.3 DirectoryMatch . . . . . . . . . . . . . . . . . . . . . . 48
3.2 Files tags . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48
3.3 Location tags . . . . . . . . . . . . . . . . . . . . . . . . . . . 49
3.4 Directory, Files and Location . . . . . . . . . . . . . . . . . . 51
3.5 Redirect . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52
3.6 Virtual Hosts . . . . . . . . . . . . . . . . . . . . . . . . . . . 54
3.6.1 Single IP, two websites . . . . . . . . . . . . . . . . . . 57
3.6.2 Two IPs, two websites . . . . . . . . . . . . . . . . . . 63
3.6.3 Other scenarios . . . . . . . . . . . . . . . . . . . . . . 69
3.6.3.1 Different content for intranet and Internet . . 69
3.6.3.2 Different websites on different ports . . . . . . 72
3.7 Modules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 74
3.7.1 Module types . . . . . . . . . . . . . . . . . . . . . . . 74
3.8 View installed modules . . . . . . . . . . . . . . . . . . . . . . 74
3.8.1 LoadModule . . . . . . . . . . . . . . . . . . . . . . . . 76
3.8.2 mod_access . . . . . . . . . . . . . . . . . . . . . . . . 78
3.8.3 mod_dir . . . . . . . . . . . . . . . . . . . . . . . . . . 78
3.8.4 mod_perl . . . . . . . . . . . . . . . . . . . . . . . . . 79
3.8.5 mod_python . . . . . . . . . . . . . . . . . . . . . . . 79
3.8.6 mod_ssl . . . . . . . . . . . . . . . . . . . . . . . . . . 79
4 .htaccess 80
4.1 Create .htaccess file . . . . . . . . . . . . . . . . . . . . . . . . 83
4.2 Create .htpasswd file . . . . . . . . . . . . . . . . . . . . . . . 83
4.3 Copy .htaccess to restricted directory . . . . . . . . . . . . . . 85
4.4 Configure httpd.conf to allow authentication via .htaccess . . . 85
4.5 Restart server . . . . . . . . . . . . . . . . . . . . . . . . . . . 86
7
www.dedoimedo.com all rights reserved
8
www.dedoimedo.com all rights reserved
7 Security 140
7.1 Updates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 141
7.2 Hide your server version . . . . . . . . . . . . . . . . . . . . . 141
7.3 Logs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 143
7.4 Permissions . . . . . . . . . . . . . . . . . . . . . . . . . . . . 143
7.5 Access to root (/) . . . . . . . . . . . . . . . . . . . . . . . . . 145
7.6 AllowOverride . . . . . . . . . . . . . . . . . . . . . . . . . . . 145
9
www.dedoimedo.com all rights reserved
9 Exercises 152
9.1 Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 153
9.1.1 Secure Web server & VirtualHost . . . . . . . . . . . . 153
9.1.2 Directory, Files and Locations . . . . . . . . . . . . . . 154
9.1.3 Server functionality, 1 . . . . . . . . . . . . . . . . . . 154
9.1.4 Server functionality, 2 . . . . . . . . . . . . . . . . . . 155
9.1.5 .htaccess . . . . . . . . . . . . . . . . . . . . . . . . . . 156
9.2 Answers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 157
9.2.1 Secure Web server & VirtualHost . . . . . . . . . . . . 157
9.2.2 Directory, Files and Locations . . . . . . . . . . . . . . 157
9.2.3 Server functionality, 1 . . . . . . . . . . . . . . . . . . 157
9.2.4 Server functionality, 2 . . . . . . . . . . . . . . . . . . 158
9.2.5 .htaccess . . . . . . . . . . . . . . . . . . . . . . . . . . 159
10
Chapter 1
Introduction
The Apache Web server has been the most popular and widely
used Web server for the last decade. It is used by approximately
50% of all websites. Apache is cross-platform, lightweight, robust,
and used in small companies as well as large corporations. Apache
is also free and open-source.
11
www.dedoimedo.com all rights reserved
Being able to configure and secure the Apache Web server is one
of the most important tasks for a (Linux) system administrator.
Almost every company has some sort of a website that advertises it,
including intranet pages that are used by the company’s workers.
The Web interface is used for many tasks beside pure browsing,
including tasks as simple as meal orders and shift rosters, but also
important tasks like administration of databases. In most cases, a
local web server is setup to accommodate these needs.
If you are working for a company that hosts public websites, the
task becomes even more complicated. Web sites are used to serve
content to billions of users daily. Whoever controls this content
- controls the World Wide Web, from news and blogs to financial
transactions. Web servers are hubs of information and power. Mis-
configured or compromised servers can expose a large number of
people to undesired content and potentially incur huge damages to
involved parties.
Running a Web site is much more than opening a port and serving
a few HTML pages. There are tremendous network usability and
security considerations that must continuously be met, evaluated
and improved in order to maintain a safe and effective Web server.
In this Part of the Book, we will learn how to properly setup and
run the Apache Web server, including the secure (HTTPS) server.
12
Chapter 2
Basic Setup
In this chapter, we will setup a Web server that will serve pages
on our internal network. In this chapter, we will perform the most
basic setup with the minimum number of steps required to get
the server running. Later, we will slowly expand, introducing new
features and options.
rpm -q httpd
13
www.dedoimedo.com all rights reserved
Now, let us overview the location and purpose of the files used by
the Apache server. Please note that the list is partial and includes
only the most important entries. We will slowly expand this list
as we go through the Part.
14
www.dedoimedo.com all rights reserved
/etc/httpd/conf/httpd.conf
15
www.dedoimedo.com all rights reserved
2.4 Backup
This is one of the most important things to remember. Always
retain the copy of the original file so you can easily revert to the
default. At the very least, do NOT delete default lines; instead,
just comment them out so you’ll be able to see what the original
settings read and refer to them.
cp /etc/httpd/conf/httpd.conf→
/etc/httpd/conf/httpd.conf-default
vi /etc/httpd/conf/httpd.conf
This is what the file looks like - at least the beginning of it:
16
www.dedoimedo.com all rights reserved
Let’s go over the most important entries you should remember for
now.
2.5.1 ServerRoot
ServerRoot is the path to the server’s configuration, error and log
files. It is possible to change this path, provided all the necessary
files are copied to the new location accordingly. We will later
review this concept as a part of the security measure known as the
Chroot Jail, but more about that later (). The default location is
/etc/httpd.
17
www.dedoimedo.com all rights reserved
2.5.2 PidFile
PidFile is the process identification number for the httpd. This
process number is important, because Apache spawns numerous
child processes when running to accommodate the web traffic. It
allows you to monitor and manipulate your server processes. See
image above.
2.5.3 ServerName
This is the one setting you will have to change to get your server
running. This is where you declare the name of your website.
18
www.dedoimedo.com all rights reserved
19
www.dedoimedo.com all rights reserved
Now, we shall edit the file and add an entry, pointing www.ninja.com
to a local IP address.
vi /etc/hosts
20
www.dedoimedo.com all rights reserved
After saving the hosts file, we can no longer see the Internet site.
Furthermore, we don’t get any fancy results from our own Web
server, because it is not running yet.
21
www.dedoimedo.com all rights reserved
22
www.dedoimedo.com all rights reserved
2.5.5 DocumentRoot
DocumentRoot tells you where your web documents (html files, im-
ages etc) should be located. It is possible to reference files in other
directories using aliases and symbolic links. The default directory
is /var/www/html.
2.5.6 ErrorLog
ErrorLog tells you where the log containing all server errors is
located. This file is critical for debugging and solving server mis-
configuration problems and for proper traffic shaping. By default,
all messages with the value of warning (warn) and higher will be
logged. This is described in the LogLevel directive just below.
23
www.dedoimedo.com all rights reserved
24
www.dedoimedo.com all rights reserved
2.5.7 Listen
The Listen command tells the Web server what ports to use for
incoming connections. By default, port 80 is used, although any
one or several can be used. The accepted conventions calls for
using port 80 for non-secure web communications (without any
encryption of traffic). Secure web communications are normally
handled on port 443.
That’s it. These are all the settings you need to know for now and
tamper with in order to successfully launch the Web server. Save
the configuration file (Esc then :x in vi text editor).
25
www.dedoimedo.com all rights reserved
26
www.dedoimedo.com all rights reserved
Now that we know what we have, it’s time to power up the server.
27
www.dedoimedo.com all rights reserved
If everything worked out fine, the web server should start without
any errors and you should see the following image:
Still, it does not hurt to check the status of the service or verify
its process ID:
28
www.dedoimedo.com all rights reserved
Once started, the Web server dynamically kills and creates pro-
cesses based on the traffic load, with the number of server processes
fluctuating between MinSpareServers and MaxSpareServers. So
far, everything figures out just nicely. Now, let’s make another
check.
29
www.dedoimedo.com all rights reserved
30
www.dedoimedo.com all rights reserved
31
www.dedoimedo.com all rights reserved
32
www.dedoimedo.com all rights reserved
33
www.dedoimedo.com all rights reserved
34
www.dedoimedo.com all rights reserved
35
www.dedoimedo.com all rights reserved
• Open it in the vi text editor and review the options listed therein.
• Configure the /etc/hosts file so that you can access the website by
name.
36
Chapter 3
Advanced setup
37
www.dedoimedo.com all rights reserved
Directory tags take the following form (again this is very analogous
to HTML <div> tags):
• Next, follows a series of options defining what users accessing web pages
located in this directory can do.
38
www.dedoimedo.com all rights reserved
<Directory />
Options FollowSymLinks
AllowOverride none
</Directory>
<Directory />
This declares the block for the root (/) directory and all sub-
directories.
Options FollowSymLinks
The Options directive declares which server features are valid for
the specified directory; FollowSymLinks is one of the possible op-
tions - it allows webpages to use symbolic links to point to files
located anywhere on the root (/) directory. Please note this is not
the best configuration from the security point of view; however, it
does demonstrate the functionality of the Directory tags. We will
discuss the server security measures later in the Part.
AllowOverride none
39
www.dedoimedo.com all rights reserved
</Directory>
Here, we’ll restrict access to the directory (or rather, the server)
by denying access from all - and only permitting access from a
single IP address, that of another machine on the LAN (in this
case, 192.168.1.129).
40
www.dedoimedo.com all rights reserved
• I have changed the order of allow, deny directives. Again, this is im-
portant, because the order defines the precedence of the rules. Thus,
first, we’ll deny everyone (this can be called default deny policy, so to
speak) and then permit only specific hosts (or IPs). If the Order were
reversed (allow, deny rather than deny, allow), no one would be able
to access the server. This is critically important to remember when
implementing allow, deny policies.
41
www.dedoimedo.com all rights reserved
The changes will only take effect after the Web server is restarted
or the configuration file reloaded. This can be achieved by running
either:
Or:
After httpd reads the new configuration file, the changes will take
effect. Now, let’s try to access the server from the Windows ma-
chine.
42
www.dedoimedo.com all rights reserved
As you can see, we are denied access. But accessing from the
CentOS client with the IP of 192.168.1.129 works fine.
3.1.2 Indexes
The Indexes directive tells the server whether to display the di-
rectory listing when asked. The behavior of this directive depends
on another directive - the DirectoryIndex. The DirectoryIndex di-
rective tells the server the name of the default page that it should
serve when a user requests the listing of a directory.
43
www.dedoimedo.com all rights reserved
specified under the DirectoryIndex are looked for and the first one
found is presented to the user. If no file is found, the listing of the
directory is then generated by the server.
This is something you may want to avoid, especially if there are files
you do not wish your users to see. However, if the Options Indexes
directives are used, then directory listings will be generated.
First, we will remove Indexes from the Options line for our Docu-
mentRoot. Then, we will create two directories, called index_allow
and index_deny, where only the first will have the Options In-
dexes specified. Both of these directories will contain some random
files.
44
www.dedoimedo.com all rights reserved
This is the new configuration file. Save it, then restart httpd. Now,
if we request the directory listing for each one from our clients, we’ll
get the following results:
45
www.dedoimedo.com all rights reserved
index_allow
46
www.dedoimedo.com all rights reserved
index_deny
47
www.dedoimedo.com all rights reserved
3.1.3 DirectoryMatch
The directives enclosed in the Directory tags will be indiscrimi-
nately applied to all sub-directories. If you require a more fine-
tuned approach for several similar sub-directories, you will have
to use the DirectoryMatch tags. The main difference is that the
DirectoryMatch tags allow the use of regular expressions, allowing
you to match several sub-directories inside a single rule.
Again, for those familiar with HTML / CSS and the use of classes
and ids, the idea is very much similar.
48
www.dedoimedo.com all rights reserved
49
www.dedoimedo.com all rights reserved
In other words, the Directory and Files tags should be used to con-
trol content that resides on the system (like various files and im-
ages, within their sub-directories), while the Location tags should
be used to control content that is located outside the system, like
databases, for instance.
50
www.dedoimedo.com all rights reserved
Again, for complex regular expressions, you should use the Loca-
tionMatch directive.
51
www.dedoimedo.com all rights reserved
3.5 Redirect
The Redirect setting allows you to map an old webpage to a new
URL. This could be the case if you changed domain, for example,
or moved around a lot of files, renaming and deleting them. To
demonstrate the directive, we’ll map our server to point to my
own site.
52
www.dedoimedo.com all rights reserved
53
www.dedoimedo.com all rights reserved
Virtual Hosts can use almost any option normally used in the
httpd.conf file. To make you better understand this, you can treat
Virtual Hosts as individual customized httpd.conf files nested in-
side the main httpd.conf file.
54
www.dedoimedo.com all rights reserved
<VirtualHost *:80>
DocumentRoot /var/www/html/ninja-father
ServerName www.ninja-father.com
# other directives
</VirtualHost>
<VirtualHost *:80>
This declares the name or the IP address of the site (server) that
should be served using the directives inside the VirtualHost block
on port 80. If no port number is used, the default one specified
under the Listen option is used. The default port is 80 (standard
convention). Asterisk (*) can be replaced with any name (for exam-
ple, www.ninja.com) or IP address (for example, 192.168.1.128),
depending on your needs and requirements. Let see several simple
examples:
55
www.dedoimedo.com all rights reserved
DocumentRoot /var/www/html/ninja-father
This declares the directory where you should place all files that you
wish served when the VirtualHost is invoked (matching names or
IPs and the port).
ServerName www.ninja-father.com
This is the name of the server. In other words, this is the address
people will type in the web browser address name in order to get
to your site. In order to successfully resolve this name to the IP
address of the Web Server, we will need to use /etc/hosts file like
before or setup a DNS Server (later).
# other directives
OK, now that we know what we’re dealing with, let’s create and
test several scenarios.
56
www.dedoimedo.com all rights reserved
57
www.dedoimedo.com all rights reserved
Now, for the sake of convenience, we will also use the /etc/hosts
file to allow name resolution to work. It is also imperative in our
case, because using the IP address would always point to the first
VirtualHost listed in the httpd.conf file.
58
www.dedoimedo.com all rights reserved
59
www.dedoimedo.com all rights reserved
Don’t mind the commented lines, they are used for other configu-
rations: the first, our standard website; the second, for yet another
VirtualHost scenario, which we will discuss soon.
Now, we shall save the files (both httpd.conf and /etc/hosts) and
restart httpd. Then, using Firefox, we will try to access each one.
60
www.dedoimedo.com all rights reserved
www.ninja-father.com
61
www.dedoimedo.com all rights reserved
www.ninja-son.com
It works like magic. Best of all, the user has no idea that these two
sites reside on the same machine.
62
www.dedoimedo.com all rights reserved
63
www.dedoimedo.com all rights reserved
64
www.dedoimedo.com all rights reserved
65
www.dedoimedo.com all rights reserved
After restarting the server, we’ll be able to get to our two sites
easily. Again, the change is completely transparent to the user.
66
www.dedoimedo.com all rights reserved
www.ninja-father.com
67
www.dedoimedo.com all rights reserved
www.ninja-son.com
Excellent.
68
www.dedoimedo.com all rights reserved
• Allow users on the local network access to all content, but deny some
to users on the Internet.
• Allow users on the local network to list directory index, but deny this
feature to the Internet users.
69
www.dedoimedo.com all rights reserved
NameVirtualHost 172.16.1.1:80
<VirtualHost 172.16.1.1:80>
DocumentRoot /www/intranet
ServerName www.our-company.com
<Directory /www/intranet>
Option Indexes FollowSymLinks
</Directory>
</VirtualHost>
NameVirtualHost 211.211.211.211:80
<VirtualHost 211.211.211.211:80>
DocumentRoot /www/web
ServerName www.our-company.com
<Directory /www/web>
Options +ExecCGI FollowSymLinks
</Directory>
</VirtualHost>
70
www.dedoimedo.com all rights reserved
NameVirtualHost
+ExecCGI
71
www.dedoimedo.com all rights reserved
AddHandler cgi-script.cgi
The Apache Web server has many other options and features. You
are welcome to try them all, using this Part of the Book as the
foundation for expanding your knowledge. For more information,
please refer to:
We’ve already discussed this before. Let’s say you have a single
IP address with multiple websites served. Using the hosts file or
72
www.dedoimedo.com all rights reserved
Here’s an example:
Listen 192.168.1.128:80
Listen 192.168.1.128:9021
NameVirtualHost 192.168.1.128:80
<VirtualHost 192.168.1.128:80>
DocumentRoot /www/white-socks
ServerName www.white-socks.com
</VirtualHost>
NameVirtualHost 192.168.1.128:9021
<VirtualHost 192.168.1.128:9021>
DocumentRoot /www/black-socks
ServerName www.black-socks.com
</VirtualHost>
73
www.dedoimedo.com all rights reserved
3.7 Modules
Modules are extensions that enhance the basic functionality of the
Web server. The modules reflect the growth of the Web and the
inclusion of dynamic content into the web pages. The static HTML
can provide only so much functionality. In fact, many of the options
we have seen and used above are provided by different modules.
For example, the Order directive is provided by the mod_access
module.
• Built-in modules, which are compiled into Apache and will load with
the server any time it is started. Their functionality cannot be removed
without recompiling the package. These modules are also known as
static.
httpd -l
74
www.dedoimedo.com all rights reserved
75
www.dedoimedo.com all rights reserved
This command will list all modules, both static and shared:
httpd -M
3.8.1 LoadModule
Shared modules are called by the Web server using the LoadModule
directive in the httpd.conf file. If you do not wish to use a certain
module, simply comment its line. However, you must remember
this will remove the functionality that the module provides.
76
www.dedoimedo.com all rights reserved
77
www.dedoimedo.com all rights reserved
3.8.2 mod_access
This module provides access control based on client host name, IP
address, or other characteristics of the client request.
3.8.3 mod_dir
This modules provides interface for redirects and serving directory
indexes. We have reviewed quite a bit of its functionality in the
previous sections.
78
www.dedoimedo.com all rights reserved
3.8.4 mod_perl
This module allows dynamic content produced by Perl scripts to
be served to incoming requests without using the Perl interpreter
every time, reducing overhead and system load. This is done by
embedding a Perl interpreter into the Apache server. The module
can also emulate a CGI environment, allowing the reuse of Com-
mon Gateway Interface (CGI) scripts without any changes to the
setup.
3.8.5 mod_python
mod_python allows integration of the Python programming lan-
guage into the Apache server. It is intended to replace CGI as
a method of executing Python scripts on a web server. It offers
much faster execution and allows data to be maintained over mul-
tiple sessions.
3.8.6 mod_ssl
This module provides an interface to the OpenSSL library, allowing
the use of Secure Socket Layer (SSL) and Transport Layer Security
(TSL) secure communication protocols. This allows you to run a
Web server that will run encrypted sessions with clients, allowing
a safe exchange of potentially sensitive data. We will discuss this
module again when we setup a secure Web server (7.12).
79
Chapter 4
.htaccess
Before we setup .htaccess, there are some things you should re-
member:
• .htaccess is not a replacement for a carefully laid out security plan. You
should use the httpd.conf file to place restrictions on your server. Only
then should you use .htaccess, to further restrict the already allowed
users.
• Using this file grants individual users an ability to make security modi-
fications to your site, creating possible risks if not properly configured.
80
www.dedoimedo.com all rights reserved
AuthType Basic
AuthName “Restricted web page”
AuthUserFile “/etc/httpd/conf/.htpasswd
require valid-user
AuthType Basic
AuthUserFile /etc/httpd/conf/.htpasswd
81
www.dedoimedo.com all rights reserved
This line 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
82
www.dedoimedo.com all rights reserved
Make sure only root can modify the .htpasswd file! It should have
permissions set to 0644.
83
www.dedoimedo.com all rights reserved
Users and passwords are added to the file by running the htpasswd
command.
After you have finished adding the usernames (there can be one
or more), you can see the contents of the .htpasswd file. The
passwords are encrypted.
84
www.dedoimedo.com all rights reserved
85
www.dedoimedo.com all rights reserved
86
www.dedoimedo.com all rights reserved
87
www.dedoimedo.com all rights reserved
88
www.dedoimedo.com all rights reserved
89
www.dedoimedo.com all rights reserved
90
www.dedoimedo.com all rights reserved
even if it does find the file, the server will have to check up every
directory up the tree to create a complete set of restrictions.
91
www.dedoimedo.com all rights reserved
You can also setup other types of files - or just specific files - from
being accessible - or accessible only to certain hosts.
92
Chapter 5
The secure Web server eliminates this threat by offering two key
advantages:
• It allows users to verify the identity of the server.
93
www.dedoimedo.com all rights reserved
• The server will choose the strongest available encryption method that
both sides can support.
• The server will then send back to the client its certificate and the pub-
lic encryption key. The certificate is a sort of an ID, telling the client
important information about the server. To make this information
credible, the certificate must be signed by a reputable Certificate Au-
thority (CA), like EquiFax, Thawte or others. The public key will be
used by the client to generate its own encryption hash should it choose
to accept the server’s certificate.
94
www.dedoimedo.com all rights reserved
match is found still, the client will be issued a warning by the browser,
informing it that the certificate could not be verified. The user now
must decide whether he/she can take the risk and accept the certificate.
In addition to being self-signed (i.e. no CA signature), the typical
issues arising with certificate prompts include a mismatch between the
site you are trying to access and the one registered in the certificate,
dubious credentials or an expired certificate.
5.2 Requirements
We have already mentioned that the client must support some sort
of encryption to able to establish secure connections to a server.
On the server end, the server must also support the secure com-
munication protocols. The Apache Web server uses the mod_ssl
module, which provides an interface to the OpenSSL library, al-
lowing the use of SSL and TLS.
95
www.dedoimedo.com all rights reserved
rpm -q openssl
5.3 Limitations
On one hand, the secure Web server offers verification of the server’s
identity and safe transactions. On the other hand, it is slower than
the regular server. Therefore, you should take into consideration
the performance loss stemming from the use of encryption. You
should not use the secure Web server for regular daily content that
does not include any exchange of personal information.
96
www.dedoimedo.com all rights reserved
5.4 Setup
5.4.1 Main configuration file(s)
The main configuration file for the secure Apache Web server is:
/etc/httpd/conf.d/ssl.conf
97
www.dedoimedo.com all rights reserved
5.4.2 Backup
We will first backup the file before making any changes.
cp /etc/httpd/conf.d/ssl.conf →
/etc/httpd/conf.d/ssl.conf-backup
But first, let us review the most important directives that we need
to get our server started. The procedure is identical to what we
have done earlier.
5.4.3.1 LoadModule
98
www.dedoimedo.com all rights reserved
5.4.3.2 Listen
5.4.3.3 VirtualHost
99
www.dedoimedo.com all rights reserved
<VirtualHost *:443>
This tells our server to listen on all interfaces for incoming connec-
tions on port 443. You may consider narrowing down the range to
specific IP addresses. Nevertheless, it is important to remember
that you can only use IP addresses! The secure Web server does
not permit named-based connections in its VirtualHost block. This
is because the SSL handshake occurs before the HTTP request can
identify the named-based virtual host.
100
www.dedoimedo.com all rights reserved
DocumentRoot "/var/www/html"
This directive specifies the directory where all your web pages
should be stored. It is recommended that you use a different root
for non-secure and secure pages. However, in our example, we will
use the default selection. Just remember that this is NOT the
optimal setting.
ServerName www.ourserver.com:443
This entry defines the server name. If you do not use the hosts
file or DNS server for name resolution, you will have to specify an
IP address. We have solved this limitation earlier, so we can use
the server name here. In a production setup, where your server is
used by clients on the Internet, you will have to use DNS for name
resolution. For study and testing and in small, private networks,
the hosts file is an adequate solution.
This covers the first part of our setup. Now we must create the
certificate.
101
www.dedoimedo.com all rights reserved
this will not work. If you intend to run any semi-serious business,
you will have to use a reputable, world-acknowledged CA to sign
your certificates.
Please note that the comparison between our setup and the real
scenario can be slightly confusing. If you get lost, there’s a ta-
ble summary (5.4.7) at the end of this section, emphasizing the
important differences between the two setups.
102
www.dedoimedo.com all rights reserved
This OpenSSL command line tool will generate an RSA key, using
the Triple-DES cypher. The -out flag signifies the output name.
The number at the end of the command tells us how long the key
will be; generally, the longer the better. A 4096-bit encryption is
quite sufficient.
After the key is created, you will be asked to use a password. This
means you won’t be able to use this key without providing the
103
www.dedoimedo.com all rights reserved
104
www.dedoimedo.com all rights reserved
-days 365
This tells us how long the certificate will be valid. Security aspects
of this parameter are examined in greater depth in the Security
chapter (7.12).
We will use the key we have created earlier to sign the certificate
for the CA.
105
www.dedoimedo.com all rights reserved
Please note that you should be careful when entering the Common
Name. You should use meaningful entries that will allow you to
easily distinguish your records, especially if you have several CAs.
Most people will never have to bother with this setting, but should
a need arise, here’s a pair of simple rules that you should adhere
to when creating CAs:
• For each CA, use the name of the site it will certify; in our case, ours-
erver.com (or www.ourserver.com).
• Append the letters CA to the end of the Common Name, so you will
know this is the CA entry.
106
www.dedoimedo.com all rights reserved
We now have a certificate. It’s time to create the server key. The
principle is similar to what we’ve done before. The one thing
you should remember is that the server key should be named
server.key, in order to conform with Apache conventions.
Since you must provide the password any time the server is restarted
or reloaded, this means the secure server will not be able to start
107
www.dedoimedo.com all rights reserved
108
www.dedoimedo.com all rights reserved
Since our CA and our website are one and the same, the form
will differ little from what we have done when creating the CA.
This can be confusing. Therefore, you should remember that the
Common Name for your CA should include the letters CA (or
similar), to distinguish it from the server record.
Lastly, you can provide an additional password for the server key,
to make misuse more difficult.
109
www.dedoimedo.com all rights reserved
110
www.dedoimedo.com all rights reserved
-CA myca.crt
-CAkey myca.key
-set_serial 01
111
www.dedoimedo.com all rights reserved
Let’s examine the certificates we have just created. This can help
you see if there are any problems with your files.
112
www.dedoimedo.com all rights reserved
113
www.dedoimedo.com all rights reserved
114
www.dedoimedo.com all rights reserved
Everything looks good. Now, we can finish editing the ssl.conf file.
115
www.dedoimedo.com all rights reserved
use the same directory. Your choice may vary. The important
thing to remember is to make the files unavailable to anyone but
root.
116
www.dedoimedo.com all rights reserved
cp server.key /etc/pki/tls/private/server.key
cp server.crt /etc/pki/tls/certs/server.crt
cp myca.crt /etc/pki/tls/certs/myca.crt
117
www.dedoimedo.com all rights reserved
118
www.dedoimedo.com all rights reserved
119
www.dedoimedo.com all rights reserved
Indeed, everything looks fine. On the Web, though, very few peo-
ple would be convinced by this certificate. But in our setup, it
serves well. After accepting the certificate (either permanently or
temporarily for this session only), you will hit yet another warn-
ing.
120
www.dedoimedo.com all rights reserved
After that, we should reach our site safely. Our setup works.
121
www.dedoimedo.com all rights reserved
5.4.7 Mini-summary
Setting up the secure Web server might seem a little confusing.
Therefore, here’s a mini summary that should clarify the setup
process.
5.4.7.1 Names
122
www.dedoimedo.com all rights reserved
5.4.7.2 Commands
Below, you can find a summarized list of commands you will need
to run to create your certificate. Please note that the names I
have used are generic and might not suit your needs. However, it
is important that you use the name server.key for the server key
file, to conform with Apache standards.
Command Description
openssl genrsa -des3 -out myca.key 4096 Create CA
key
openssl req -new -x509 -days 365 -key Create CA
myca.key -out myca.crt certificate
openssl genrsa -des3 -out server.key 4096 Create server
key
openssl req -new -key server.key -out Create CSR
server.csr
openssl x509 -req -days 365 -in server.csr Sign CSR
-CA myca.crt -CAkey myca.key -set_serial
01 -out server.crt
123
www.dedoimedo.com all rights reserved
This will help you better understand the differences between our
exercise and a real, production setup.
124
www.dedoimedo.com all rights reserved
5.4.7.4 Verification
You will have to run these commands to check your certificates and
keys:
125
www.dedoimedo.com all rights reserved
5.5 Extras
Now that we have our secure Web server running, let us review a
number of other options.
We will “filter” the old key (server.key) into a new one (server.key.nopass),
which will not include a password. Then, we will swap between the
old and the new one. You are advised to keep a backup copy of
the original key, just in case.
126
www.dedoimedo.com all rights reserved
127
www.dedoimedo.com all rights reserved
Assuming the CA wants all records in the purely digital form, you
will have to create a CSR. We have already done that. Optionally,
they might ask you to convert the file into a Privacy Enhanced
Mail (PEM) format. Please note that this format is not widely
used and this will probably not be necessary.
128
www.dedoimedo.com all rights reserved
Please note that you should consult your CA for detailed instruc-
tions regarding the conversions, if at all required.
You will now have to submit the CSR file (or PEM) to the CA.
Once the CA processes your application, you will receive the cer-
tificate back. The file will most likely be sent in the PEM format,
so you will have to convert it back to CSR format. It will also
most likely bear a different name from what you are used to, so
you should rename it to server.crt, to conform with Apache con-
ventions.
Now, you should verify the certificate, against the relevant CA’s
file. You should receive this file from your CA. Alternatively, if
your distro includes a list of CAs, you might try that one. On
CentOS 5, a bundle containing a list of known CAs is located under
/etc/pki/tls/certs.
Next, you should verify that the certificate corresponds to the pri-
vate key. Please make sure the names match.
129
www.dedoimedo.com all rights reserved
Once you have completed the above steps, you will have to edit the
ssl.conf file, restart the server and test your setup. We’re back on
familiar grounds.
130
www.dedoimedo.com all rights reserved
131
Chapter 6
Other configurations
However, you can restrict the traffic even more. For example, you
can allow incoming connections only to a certain interface:
132
www.dedoimedo.com all rights reserved
You can further sharpen the rules by specifying source ports and
packet states. We’ll see this in the next section.
Let’s assume that your Web server is a local machine, with a local
IP address. It serves both internal and external clients. The inter-
nal setup is rather simple. We need to make sure external clients
can connect, too.
133
www.dedoimedo.com all rights reserved
• We will have to “forward” our web ports, so that clients behind the
gateway will be able to accept incoming communications.
Here are the original rules, which we have setup for the DHCP
server acting as a gateway:
134
www.dedoimedo.com all rights reserved
If you want to tighten the rules some more, you can also specify
the source ports:
We have placed the rules on the top of the chain, so they would be
processed before the existing rules, which only allow new outbound
connections. Basically, these rules are sufficient if your gateway is
servicing a number of local networks, all of which can fully resolve
one another’s IP addresses. They are not good enough for the
Internet, though.
135
www.dedoimedo.com all rights reserved
Of course, you can use non-default ports on the Web server, like
8080 or anything alike, which makes the idea of port forwarding
even more meaningful.
If you have more than one publicly visible IP address, you won’t
be able to use IP masquerading. This is because masquerading
forces all traffic to the default network interface on the firewalled
gateway, resulting in a single usable external IP address.
However, it is quite likely that you will want to run your servers on
separate hosts, with different both internal and external addresses,
both to shape your traffic in a more orderly fashion and reduce the
workload on specific hosts. To this end, you will have to use SNAT
rather than IP forwarding.
136
www.dedoimedo.com all rights reserved
The basic principle remains the same, except that you use sepa-
rate external IP addresses for individual hosts, groups of hosts or
the entire local network, as you see fit. In our example, we will
demonstrate SNAT by creating a private rule for the Web server
and a general rule for all other clients.
Let’s assume the Web server will use a public IP address of 1.1.1.1,
while all other clients will use 1.1.1.2.
137
www.dedoimedo.com all rights reserved
These two rules are required to allow DNAT and SNAT for the
client running the Web server. Please note that these two rules
do not specify what kind of servers are running on the particular
client. This grants you extra flexibility, if you need to run more
than one server on a particular machine.
As said, the forwarding rules from before remain valid, both the
specific rules for the Web server, which permit new inbound con-
nections, and the general rules, which permit only new outbound
connections.
138
www.dedoimedo.com all rights reserved
General rules:
139
Chapter 7
Security
140
www.dedoimedo.com all rights reserved
users have a hard time with sites loaded with malicious payload.
As the server owner and administrator, it is your responsibility to
make sure that your visitors are not at risk.
Let us review some of the most crucial settings that you should
pay attention to make sure both your server and your clients are
secure.
7.1 Updates
Keep your server up to date at all times. Make sure you patch
new vulnerabilities instantly. You are advised to subscribe to the
Apache HTTP Server Mailing Lists for information about new
bugs, updates, features, and more.
To remove server information, you will need to use these two di-
rectives:
ServerSignature Off
ServerTokens Prod
141
www.dedoimedo.com all rights reserved
(403 Forbidden, 404 Not found), directory listings and others. The
second directive, ServerTokens, will change the server’s HTTP
Response Header. By default, with the directive set to OS, the
header will disclose both the version and the operating system.
Set to Prod, the header will merely report Apache.
142
www.dedoimedo.com all rights reserved
7.3 Logs
You should check your logs at least daily. This may be tedious and
boring, but it is vital that you discover any potential breaches as
quickly as possible. Keep an eye on things and look for suspicious
directories and files.
7.4 Permissions
Badly implemented permissions can ruin your entire security. It is
critical that you make sure the executables, configuration files, logs,
access files and private keys are located outside the public HTML
directories and writable only by root. The web pages should be
143
www.dedoimedo.com all rights reserved
Location Permissions
/usr/sbin/httpd F: 511
/etc/httpd D: 751
/etc/httpd/conf D: 751, F: 644 / 600
/etc/httpd/conf.d D: 751, F: 644 / 600
/etc/httpd/logs symbolic link, 755 / 711
/etc/httpd/modules symbolic link, 755 / 711
/etc/httpd/run symbolic link, 755 / 711
/usr/lib/httpd/modules D: 751, F: 644 / 600
/var/log/httpd D: 751, F: 644 / 600
/var/run D: 751, F: 644 / 600
/var/www/html D: 755, F: 755
If you are really paranoid, then you should ONLY allow root access
to the binaries and configuration files. It really depends on your
setup and needs. Last but not the least, let’s not forget that system
files MUST be owned by root.
144
www.dedoimedo.com all rights reserved
<Directory />
Order Deny, Allow
Deny from all
</Directory>
7.6 AllowOverride
This directive specifies if options used in the .htaccess files can
conflict (and thus override) the settings configured for the particu-
lar directory. In general, you should set this directive to none and
only permit specific tasks to a small number of trusted users. If
you lease your server to numerous clients who must have some sort
of protection for their content, then you can allow them to use the
.htaccess file for authentication, as we have shown before (4).
AllowOverride none
145
www.dedoimedo.com all rights reserved
<Files ~ “^\.ht”>
Order allow, deny
Deny from all
</Files>
Options -ExecCGI
146
www.dedoimedo.com all rights reserved
Options -Includes
For more about SSI, please refer to Server Side Includes on Wikipedia.
147
www.dedoimedo.com all rights reserved
148
www.dedoimedo.com all rights reserved
7.12.2 Permissions
The server.crt and the server.key file must only be readable by
root. You should even disallow the root user from making any
changes to the files. Set the permissions for these two files to 0400.
The permissions for the ssl.conf should be in line with your policy,
which should be either 640 or 600.
149
www.dedoimedo.com all rights reserved
Needless to say, nothing can replace years of hard work and expe-
rience.
150
Chapter 8
Additional resources
151
Chapter 9
Exercises
When trying to solve the questions, please do not look into the
answers right away. Try to think and figure out the problem on
your own. The questions might also help you identify the stronger
and weaker points in your freshly earned Apache skills.
152
www.dedoimedo.com all rights reserved
9.1 Questions
9.1.1 Secure Web server & VirtualHost
You want to configure a secure Web server that you will use for
online transactions with your customers. You have completed all
the steps successfully, but your server does not work, for some
reason. Looking at the VirtualHost block in the ssl.conf file, you
are unable to find the source of the problem. Can you tell what’s
wrong?
Please note that there is an error only within the displayed di-
rectives in the screenshot. The VirtualHost container is properly
closed and all directives have the correct syntax.
153
www.dedoimedo.com all rights reserved
<Location /dir/>
Order allow, deny
Allow from all
Deny from plush-mush.info
AllowOverride AuthConfig
</Location>
4. You need to specify the location of the .htaccess file in the opening
Location tag.
154
www.dedoimedo.com all rights reserved
155
www.dedoimedo.com all rights reserved
9.1.5 .htaccess
What security precautions must you take to make sure authenti-
cation via .htaccess is not compromised easily?
156
www.dedoimedo.com all rights reserved
9.2 Answers
9.2.1 Secure Web server & VirtualHost
It is impossible to use named-based virtual hosts with the secure
Web server. This is because the SSL handshake occurs before the
HTTP request can identify the named-based virtual host. Using
names will result in errors. You may only use IP addresses in the
VirtualHost directives inside the ssl.conf configuration file.
157
www.dedoimedo.com all rights reserved
You should never add a slash at the end of the directory path -
and ever delete comments.
158
www.dedoimedo.com all rights reserved
9.2.5 .htaccess
The right answers are 1, 3 and 4. The .htaccess file must be
placed in public directories, thus placing them in non-web direc-
tories would invalidate their purpose. This makes answer no. 2
wrong. Furthermore, the executable bit in the permissions is com-
pletely unnecessary for the functionality of the file.
The connection to the Web server (and the relevant port) is estab-
lished before any authentication takes place. Moreover, the choice
of the port number has no bearing whatsoever on the functionality
of the .htaccess files. Thus, answer no. 5 is wrong.
159