Projects‎ > ‎

Apache Web Server Setup

posted Dec 19, 2009, 9:42 PM by Philip Saxton   [ updated Dec 19, 2009, 9:45 PM ]
This document attempts to detail the installation and setup of the apache http server. The goals of the web server are to serve http requests for a small number of virtual hosts. The underlying operating system for this document is Debian GNU/Linux 5.0 (Lenny). Installation of supporting programs is not documented as it is beyond the scope of this document.

Why apache

apache is not the only http server available. In some situations it is not even the best option. apache has been chosen for this document because it is by far the most popular http server and has a nearly endless variety of plug ins and development options available.

Installation

There are four server models or 'MPM's available under Debian for apache. prefork has been selected for this document because it is required for mod_php.

Other MPMs available are:
  event - which is also thread based but passes threads off after they are received.
  itk - which is based on prefork and allows vhosts to use their own user and group IDs.
  worker - a threaded server model

As usual with Debian, installation is quite straight forward:

# aptitude install apache2-mpm-prefork;

Configuration

In Debian, apache configuration files are kept under /etc/apache2. The distribution default settings are kept in apache2.conf. Any of the settings in apache2.conf should be changed by setting the same variable in httpd.conf as this is read in after apache2.conf. Changes made to apache2.conf may be overwritten if the apache server installation is upgraded or modified. ports.conf exists to specify which ports apache should listen.

apache mods are controlled by the mods-available/ and mods-enabled/ directories. When an apache module is installed it will place load file and possibly a config file in the mods-available/ directory. The conf and load files should be sym-linked in the mods-enabled/ directory and the conf file edited to suit the needs of the server.

Virtual hosts are defined in the sites-available/ directory and are activated by sym-linking to the file in the sites-enabled/ directory. This way one can just remove the sym-link in the sites-enabled/ folder to deactivate a site without losing the configuration.

Any remaining server wide configuration bits can be placed in files in the conf.d directory. Below is a summary of the default files upon installation. Some of the files in mods-available/ and mods-enabled/ have been snipped for brevity. Note how the files in the *-enabled/ directories are links to their *-available/ counterparts.

/etc/apache2:
  apache2.conf    httpd.conf         ports.conf
  conf.d/         mods-available/    sites-available/
  envvars         mods-enabled/      sites-enabled/
/etc/apache2/conf.d:
  charset
  security
/etc/apache2/mods-available:
  alias.conf      asis.load          authz_host.load
  alias.load      auth_basic.load
/etc/apache2/mods-enabled:
  alias.conf -> ../mods-available/alias.conf
  alias.load -> ../mods-available/alias.load
  auth_basic.load -> ../mods-available/auth_basic.load
/etc/apache2/sites-available:
  default         default-ssl
/etc/apache2/sites-enabled:
  000-default -> ../sites-available/default

Changes made to ANY of the configuration files will require apache to be restarted before the changes will take effect.

 Setting Up Virtual Hosts 

The default apache configuration coming from the Debian package is set up to use virtual hosts. This is not a bad idea even if the server will only be hosting a single site as there is no performance penalty and more sites can be added later if needed with minimal changes necessary.

For the first virtual host the 000-default link in the sites-enabled/ directory will be removed. Once the real default site has been created in sites-available/ the link will be recreated to it.

To create a new site:

# cd /etc/apache2;
# cp sites-available/default sites-available/site.tld;
# editor sites-available/site.tld;
  <VirtualHost *:80>
        ServerAdmin webmaster@site.tld                 ## Correct email addr
        DocumentRoot /srv/www/site.tld                 ## Correct site name
        <Directory />                                  ## Remove
                Options FollowSymLinks                 ##  |
                AllowOverride None                     ##  |
        </Directory>                                   ## _V_
        <Directory /srv/www/site.tld>                  ## Correct site path
                Options Indexes FollowSymLinks MultiViews
                AllowOverride None
                Order allow,deny
                allow from all
        </Directory>
        ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/        ## Remove if no cgi
        <Directory "/usr/lib/cgi-bin">                 ##  |
                AllowOverride None                     ##  |
                Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
                Order allow,deny                       ##  |
                Allow from all                         ##  |
        </Directory>                                   ## _V_
        ErrorLog /var/log/apache2/error.site_tld.log   ## Add site name
        # Possible values include: debug, info, notice, warn, error, crit,
        # alert, emerg.
        LogLevel warn
                                                       ## Add site name
        CustomLog /var/log/apache2/access.site_tld.log combined
       Alias /doc/ "/usr/share/doc/"                   ## Remove
      <Directory "/usr/share/doc/">                    ##  |
          Options Indexes MultiViews FollowSymLinks    ##  |
          AllowOverride None                           ##  |
          Order deny,allow                             ##  |
          Deny from all                                ##  |
          Allow from 127.0.0.0/255.0.0.0 ::1/128       ##  |
      </Directory>                                     ## _V_
  </VirtualHost>
# mkdir -p /srv/www/site.tld;
# chown www-data:www-data /srv/www/site.tld;
                                                 ## XXX is a counter,
                                                 ## 000 becomes default site
# ln -s sites-available/site.tld sites-enabled/XXX-site.tld;
# apache2ctl graceful;

Installing Modules

Modules for apache can be installed through aptitude as well. This document will install php5 as an example. Other modules may be installed in a similar fashion.

# aptitude install php5;

The php5 package automatically creates the links required to enable php on the apache server. The following example illustrates how this may be done for packages which do not automatically set themselves as enabled. Either way, after installing the mod, the apache server will need to be restarted to make use of the new features.

# cd /etc/apache2/mods-enabled;
# ln -s ../mods-available/php5.conf .;
# ln -s ../mods-available/php5.load .;
# apache2ctl graceful;                         ## Restart the apache server
Comments