Tuesday, September 15, 2009

Apache Web Server

INSTALLING THE APACHE HTTP SERVER
Most modern Linux distributions come with the binary package for the Apache HTTP
server software in Red Hat Package Manager (RPM) format, so installing the software is
usually as simple as using the package management tool on the system. This section walks
you through the process of obtaining and installing the program via RPM and Advanced
Packaging Tool (APT). Mention is also made of installing the software from source code,
if you choose to go that route. The actual configuration of the server covered in later sec-
tions applies to both classes of installation (from source or from a binary package).
On a Fedora system, there are several ways to obtain the Apache RPM. Here are some
of them:
 ? Download the Apache RPM (e.g., httpd-*.rpm) for your operating system from
your distribution’s software repository. For Fedora, you can obtain a copy of
the program from http://download.fedora.redhat.com/pub/fedora/linux/
releases/9/Fedora/i386/os/Packages/.
? You can install from the install media, from the /Packages/ directory on the
media.
 ? You can pull down and install the program directly from a repository using the
Yum program. This is perhaps the quickest method if you have a working con-
nection to the Internet. And this is what we’ll do here.
To use Yum to install the program, type
[root@fedora-serverA ~]# yum -y install httpd
To confirm that the software is installed, type
[root@fedora-serverA vsftpd]# rpm -q httpd
httpd-2.*
And that’s it! You now have Apache installed on the Fedora server.
For a Debian-based Linux distribution like Ubuntu, you can use APT to install
Apache by running
yyang@ubuntu-serverA:~$ sudo apt-get -y install apache2
The web server daemon is automatically started after you install using apt-get on
Ubuntu systems
Installing Apache from Source
Just in case you are not happy with the built-in defaults that the binary Apache
package forces you to live with and you want to build your web server software
from scratch, you can always obtain the latest stable version of the program
directly from the apache.org web site. The procedure for building from source is
discussed here.
We’ll download the latest program source into the /usr/local/src/ directory from
the apache.org web site. You can use the wget program to do this. Type
[root@serverA src]# wget http://www.apache.org/dist/httpd/httpd-2.2.8.tar.gz
Extract the tar archive. And then change to the directory that is created during
the extraction.
[root@serverA src]# tar xvzf httpd-2.2.8.tar.gz
[root@serverA src]# cd httpd-2.2.8
Assuming we want the web server program to be installed under the /usr/local/
httpd/ directory, we’ll run the configure script with the proper prefix option.
[root@serverA httpd-2.2.8]# ./configure --prefix=/usr/local/httpd
Runmake.
[root@serverA httpd-2.2.8]# make
Create the program’s working directory (i.e., /usr/local/httpd/), and then run
make install.
[root@serverA httpd-2.2.8]# make install
Once the install command completes successfully, a directory structure will
be created under /usr/local/httpd/ that will contain the binaries, the configuration
files, the log files, etc. for the web server.
Apache Modules
Part of what makes Apache so powerful and flexible is that its design allows extensions
through modules. Apache comes with many modules by default and automatically
includes them in the default installation.
If you can imagine “it,” you can be almost certain that somebody has probably
already written a module for “it” for the Apache web server. The Apache module appli-
cation programming interface (API) is well documented, so if you are so inclined (and
know how to), you can probably write your own module for Apache to provide a func-
tionality that you want.
To give you some idea of what kinds of things people are doing with modules, visit
http://modules.apache.org. There you will find information on how to extend Apache’s
capabilities using modules. Some common Apache modules are
 ? mod_cgi Allows the execution of CGI scripts on the web server
? mod_perl Used to incorporate a Perl interpreter into the Apache web server
? mod_aspdotnet Provides an ASP.NET host interface to Microsoft’s ASP.NET
engine
? mod_authz_ldap Provides support for authenticating users of the Apache
HTTP server against a Lightweight Directory Access Protocol (LDAP) database
? mod_ssl Provides strong cryptography for the Apache web server via the
Secure Sockets Layer (SSL) and Transport Layer Security (TLS) protocols
? mod_ftpd Allows Apache to accept FTP connections
 ? mod_userdir Allows user content to be served from user-specific directories
on the web server via HTTP
If you know the name of a particular module that you want (and if the module is
popular enough), you might find that the module has already been packaged in an
RPM format, and so you can install it using the usual RPM methods. For example, if
you want to include the SSL module (mod_ssl) in your web server setup, on a Fedora
system, you can issue this Yum command to automatically download and install the
module for you:
[root@serverA ~]# yum install mod_ssl
Alternatively, you can go to the Apache modules project web site and search for,
download, compile, and install the module that you want.
STARTING UP AND SHUTTING DOWN APACHE
Starting up and shutting down Apache on most Linux distributions is easy. To start
Apache on a Fedora system or any other Red Hat–like system, use this command:
[root@serverA ~]# service httpd start
To shut down Apache, enter this command:
[root@serverA ~]# service httpd stop
After making a configuration change to the web server that requires you to restart
Apache, type
[root@serverA ~]# service httpd restart
Starting Apache at Boot Time
After installing the web server, if you find that the web service is one that you want the
system to provide at all times, you will need to configure the system to automatically
start the service for you between system reboots. It is easy to forget to do this on a sys-
tem that has been running for a long time without requiring any reboots, because if you
ever had to shut down the system due to an unrelated issue, you might be baffled as to
why the web server that has been running perfectly without incident failed to start up
after starting the box. So it is good practice to take care of this during the early stages of
configuring the service.
Most Linux flavors have the chkconfig utility available, which can be used for
controlling which system services start up at what runlevels.
To view the runlevels the web server is configured to start up in, type
[root@fedora-serverA ~]# chkconfig --list httpd
httpd 0:off 1:off 2:off 3:off 4:off 5:off 6:off
This output shows that the web server is not configured to start up in any runlevel
in its out-of-the-box state. To change this and make Apache start up automatically in
runlevels 2, 3, 4, and 5, type
[root@fedora-serverA ~]# chkconfig httpd on
In Ubuntu, you can use either the sysv-rc-conf or the update-rc.d utility to
manage the runlevels in which Apache starts up
TESTING YOUR INSTALLATION
You can perform a quick test of your Apache installation using its default home page.
To do this, first confirm that the web server is up and running using the following
command:
[root@serverA httpd-2.2.8]# service httpd status
httpd (pid 31090 31089 31084 31083 31081) is running...
On our sample Fedora system, Apache comes with a default page that gets served to
visitors in the absence of a default home page (e.g., index.html or index.htm). The file
that gets displayed to visitors when there is no default home page is /var/www/error/
noindex.html.
To find out if your Apache installation went smoothly, start a web browser and tell
it to visit the web site on your machine. To do this, simply type http://localhost (or the
Internet Protocol Version 6 [IPv6] equivalent, http://[::1]/) in the address bar of your web
browser. You should see a page stating something to the effect that “your Apache HTTP
server is working properly at your site.” If you don’t, retrace your Apache installation
steps and make sure you didn’t encounter any errors in the process.
CONFIGURING APACHE
Apache supports a rich set of configuration options that are sensible and easy to follow.
This makes it a simple task to set up the web server in various configurations.
This section walks through a basic configuration. The default configuration is actu-
ally quite good and (believe it or not) works right out of the box, so if the default is
acceptable to you, simply start creating your Hypertext Markup Language (HTML) doc-
uments! Apache allows several common customizations. After we step through creating
a simple web page, we’ll show how you can make those common customizations in the
Apache configuration files.
Creating a Simple Root-Level Page
If you like, you can start adding files to Apache right away in the /var/www/html direc-
tory for top-level pages (for a source install, the directory would be /usr/local/httpd/
htdocs). Any files placed in that directory must be world-readable.
As mentioned earlier, Apache’s default web page is index.html. Let’s take a closer
look at creating and changing the default home page so that it reads “Welcome to
 serverA.example.org.” Here are the commands:
[root@serverA ~]# cd /var/www/html/
[root@serverA html]# echo "Welcome to serverA.example.org" >> index.html
[root@serverA html]# chmod 644 index.html
You could also use an editor such as vi, pico,or emacs to edit the index.html file and
make it more interesting.
Apache Configuration Files
The configuration files for Apache are located in the /etc/httpd/conf/ directory on a
Fedora or Red Hat Enterprise Linux (RHEL) system, and for our sample source install,
the path will be /usr/local/httpd/conf/. The main configuration file is usually named
httpd.conf on Red Hat–like distributions like Fedora. On Debian-like systems, the main
configuration file for Apache is named /etc/apache2/apache2.conf.
The best way to learn more about the configuration files is to read the httpd.conf file.
The default configuration file is heavily commented, explaining each entry, its role, and
the parameters you can set.
Common Configuration Options
The default configuration settings work just fine right out of the box, and for basic needs,
may require no further modification. Nevertheless, site administrators may need to cus-
tomize their web server or web site further.
This section discusses some of the common directives or options that are used in
Apache’s configuration file.
ServerRoot
This is used for specifying the base directory for the web server. On Fedora, RHEL,
and Centos distributions, this value, by default, is the /etc/httpd/ directory. The default
value for this directive in Ubuntu, OpenSuSE, and Debian Linux distributions is /etc/
apache2/.
Syntax: ServerRoot directory-path
Listen
This is the port(s) on which the server listens for connection requests. It can also be used
to specify the particular IP addresses over which the web server accepts connections. The
default value for this directive is 80 for nonsecure web communications.
Syntax: Listen [IP-address:] portnumber
For example, to set Apache to listen on its IPv4 and IPv6 interfaces on port 80, you
would set the Listen directive to read
Listen 80
To set Apache to listen on a specific IPv6 interface (e.g., fec0::20c:dead:beef:11cd) on
port 8080, you would set the Listen directive to read
Listen [fec0::20c:dead:beef:11cd]:8080
ServerName
This directive defines the hostname and port that the server uses to identify itself. At
many sites, servers fulfill multiple purposes. An intranet web server that isn’t getting
heavy usage, for example, should probably share its usage allowance with another ser-
vice. In such a situation, a computer name such as “www” (fully qualified domain name,
or FQDN=www.example.org) wouldn’t be a good choice, because it suggests that the
machine has only one purpose.
It’s better to give a server a neutral name and then establish Domain Name System
(DNS) Canonical Name (CNAME) entries or multiple hostname entries in the /etc/hosts
file. In other words, you can give the system several names for accessing the server,
but it needs to know only about its real name. Consider a server whose hostname is
dioxin.eng.example.org that is to be a web server as well. You might be thinking of
giving it the hostname alias www.sales.example.org. However, since dioxin will know
itself only as dioxin, users who visit www.sales.example.org might be confused by see-
ing in their browsers that the server’s real name is dioxin.
Apache provides a way to get around this through the use of the ServerName direc-
tive. This works by allowing you to specify what you want Apache to return as the host-
name of the web server to web clients or visitors.
Syntax: ServerName fully-qualified-domain-name[: port]
ServerAdmin
This is the e-mail address that the server includes in error messages sent to the client.
It’s often a good idea, for a couple of reasons, to use an e-mail alias for a web site’s
administrator. First, there may be more than one administrator. By using an alias, it’s
possible for the alias to expand out to a list of other e-mail addresses. Second, if the  current administrator leaves, you don’t want to have to make the rounds of all those web
pages and change the name of the site administrator.
Syntax: ServerAdmin e-mail_address
DocumentRoot
This defines the primary directory on the web server from which HTML files will be
served to requesting clients. On Fedora distros and other Red Hat–like systems, the
default value for this directive is /var/www/html/. On OpenSuSE and SEL distributions,
the default value for this directive is /srv/www/htdocs.

No comments:

Post a Comment