Thursday, December 3, 2009

How to configure Apache? (Part 3)

Module mod_so

This module provides for loading of executable code and modules into the server at start-up or restart time.

Status: Base (Windows); Experimental (Unix)

Source File: mod_so.c

Module Identifier: so_module

Compatibility: Available in Apache 1.3 and later.

Summary

This is an experimental module. On selected operating systems it can be used to load modules into Apache at runtime via the Dynamic Shared Object (DSO) mechanism, rather than requiring a recompilation.

On Unix, the loaded code typically comes from shared object files (usually with .so extension), whilst on Windows this module loads DLL files. This module is only available in Apache 1.3 and up.

In previous releases, the functionality of this module was provided for Unix by mod_dld, and for Windows by mod_dll. On Windows, mod_dll was used in beta release 1.3b1 through 1.3b5. mod_so combines these two modules into a single module for all operating systems.

Directives

    * LoadFile

    * LoadModule

Creating DLL Modules for Windows

The Apache module API is unchanged between the Unix and Windows versions. Many modules will run on Windows with no or little change from Unix, although others rely on aspects of the Unix architecture which are not present in Windows, and will not work.

When a module does work, it can be added to the server in one of two ways. As with Unix, it can be compiled into the server. Because Apache for Windows does not have the Configure program of Apache for Unix, the module's source file must be added to the ApacheCore project file, and its symbols must be added to the os\win32\modules.c file.

The second way is to compile the module as a DLL, a shared library that can be loaded into the server at runtime, using the LoadModule directive. These module DLLs can be distributed and run on any Apache for Windows installation, without recompilation of the server.

To create a module DLL, a small change is necessary to the module's source file: The module record must be exported from the DLL (which will be created later; see below). To do this, add the MODULE_VAR_EXPORT (defined in the Apache header files) to your module's module record definition. For example, if your module has:

    module foo_module;

Replace the above with:

    module MODULE_VAR_EXPORT foo_module;

Note that this will only be activated on Windows, so the module can continue to be used, unchanged, with Unix if needed. Also, if you are familiar with .DEF files, you can export the module record with that method instead.

Now, create a DLL containing your module. You will need to link this against the ApacheCore.lib export library that is created when the ApacheCore.dll shared library is compiled. You may also have to change the compiler settings to ensure that the Apache header files are correctly located.

This should create a DLL version of your module. Now simply place it in the modules directory of your server root, and use the LoadModule directive to load it.

LoadFile directive

Syntax: LoadFile filename [filename] ...

Context: server config

Status: Base

Module: mod_so

The LoadFile directive links in the named object files or libraries when the server is started or restarted; this is used to load additional code which may be required for some module to work. Filename is either an absolute path or relative to ServerRoot.

For example:

LoadFile libexec/libxmlparse.so

LoadModule directive

Syntax: LoadModule module filename

Context: server config

Status: Base

Module: mod_so

The LoadModule directive links in the object file or library filename and adds the module structure named module to the list of active modules. Module is the name of the external variable of type module in the file, and is listed as the Module Identifier in the module documentation. Example (Unix, and for Windows as of Apache 1.3.15):

    LoadModule status_module modules/mod_status.so
Example (Windows prior to Apache 1.3.15, and some 3rd party modules):

    LoadModule foo_module modules/ApacheModuleFoo.dll

Note that all modules bundled with the Apache Win32 binary distribution were renamed as of Apache version 1.3.15.

Win32 Apache modules are often distributed with the old style names, or even a name such as libfoo.dll. Whatever the name of the module, the LoadModule directive requires the exact filename, no assumption is made about the filename extension.

How to configure Apache? (Part 2)

How Directory, Location and Files sections work

The sections <Directory>, <Location>  and <Files> can contain directives which only apply  

1.to specified directories,  

2.URLs or files respectively.  

    Also htaccess files can be used inside a directory to apply directives to that directory.

    This document explains how these different sections differ and how they relate to each other when Apache decides which directives apply for a particular directory or request URL.

Directives allowed in the sections

    Everything that is syntactically allowed in <Directory> is also allowed in <Location> (except a sub-<Files> section). Semantically, however some things, most notably AllowOverride and the two options FollowSymLinks and SymLinksIfOwnerMatch, make no sense in <Location>, <LocationMatch> or <DirectoryMatch>. The same for <Files> -- syntactically everything is fine, but semantically some things are different.



How the sections are merged?

The order of merging is:

   1. <Directory> (except regular expressions) and .htaccess done simultaneously (with .htaccess, if allowed, overriding <Directory>)

   2. <DirectoryMatch>, and <Directory> with regular expressions

   3. <Files> and <FilesMatch> done simultaneously

   4. <Location> and <LocationMatch> done simultaneously

    Apart from <Directory>, each group is processed in the order that they appear in the configuration files. <Directory> (group 1 above) is processed in the order shortest directory component to longest. If multiple <Directory> sections apply to the same directory they are processed in the configuration file order. The configuration files are read in the order httpd.conf, srm.conf and access.conf. Configurations included via the Include directive will be treated as if they were inside the including file at the location of the Include directive.

    Sections inside <VirtualHost> sections are applied after the corresponding sections outside the virtual host definition. This allows virtual hosts to override the main server configuration. (Note: this only works correctly from 1.2.2 and 1.3a2 onwards. Before those releases sections inside virtual hosts were applied before the main server).

Later sections override earlier ones.

Notes about using sections

The general guidelines are:

    * If you are attempting to match objects at the filesystem level then you must use <Directory> and/or <Files>.

    * If you are attempting to match objects at the URL level then you must use <Location>

ut a notable exception is:

    * proxy control is done via <Directory>. This is a legacy mistake because the proxy existed prior to <Location>. A future version of the config language should probably switch this to <Location>.

Note about .htaccess parsing:

    * Modifying .htaccess parsing during Location doesn't do anything because .htaccess parsing has already occurred.

<Location> and symbolic links:

    * It is not possible to use "Options FollowSymLinks" or "Options SymLinksIfOwnerMatch" inside a <Location>, <LocationMatch> or <DirectoryMatch> section (the options are simply ignored). Using the options in question is only possible inside a <Directory> section (or a .htaccess file).

<Files> and Options:

    * Apache won't check for it, but using an Options directive inside a <Files> section has no effect.

Another note:

    * There is actually a <Location>/<LocationMatch> sequence performed just before the name translation phase (where Aliases and DocumentRoots are used to map URLs to filenames). The results of this sequence are completely thrown away after the translation has completed.

Wednesday, December 2, 2009

How to configure Apache? (Part 1)

All about Apache Config File

    Apache is configured by placing directives in plain text configuration files. The main configuration file is usually called

httpd.conf

      
    The location of this file is set at compile-time, but may be overridden with the -f command line flag. Some sites also have srm.conf and access.conf files for historical reasons. In addition, other configuration files may be added using the Include directive. Any directive may be placed in any of these configuration files.
Changes to the main configuration files are only recognized by Apache when it is started or restarted.

    New with Apache 1.3.13 is a feature where if any configuration file is actually a directory, Apache will enter that directory and parse any files (and subdirectories) found there as configuration files. One possible use for this would be to add VirtualHosts by creating small configuration files for each host, and placing them in such a configuration directory. Thus, you can add or remove VirtualHosts without editing any files at all, simply adding or deleting them. This makes automating such processes much easier. The server also reads a file containing mime document types; the filename is set by the TypesConfig directive, and is mime.types by default.

Syntax of the Configuration Files

     Apache configuration files contain one directive per line. The back-slash "\" may be used as the last character on a line to indicate that the directive continues onto the next line. There must be no other characters or white space between the back-slash and the end of the line.

    Directives in the configuration files are case-insensitive, but arguments to directives are often case sensitive. Lines which begin with the hash character "#" are considered comments, and are ignored. Comments may not be included on a line after a configuration directive. Blank lines and white space occurring before a directive are ignored, so you may indent directives for clarity.

    You can check your configuration files for syntax errors without starting the server by using
apachectl configtest or the -t command line option.

root@host1:[/usr/apache2/2.2/bin]#./apachectl configtest

Syntax OK

root@host1:[/usr/apache2/2.2/bin]#

Modules

Related Modules : mod_so

Related Directives :
AddModule

ClearModuleList

<IfModule>

LoadModule



    Apache is a modular server. This implies that only the most basic functionality is included in the core server. Extended features are available through modules which can be loaded into Apache. By default, a base set of modules is included in the server at compile-time. If the server is compiled to use dynamically loaded modules, then modules can be compiled separately and added at any time using the LoadModule directive. Otherwise, Apache must be recompiled to add or remove modules. Configuration directives may be included conditional on a presence of a particular module by enclosing them in an <IfModule> block.

To see which modules are currently compiled into the server, you can use the -l command line option.

root@host1:[/usr/apache2/2.2/bin]#./apachectl -l

Compiled in modules:

  core.c

  prefork.c

  http_core.c

  mod_so.c

root@host1:[/usr/apache2/2.2/bin]#

<IfModule>  directive

Syntax: <IfModule [!]module-name> ... </IfModule>

Default: None

Context: all

Status: Core

Compatibility: IfModule is only available in 1.2 and later.

    The <IfModule test>...</IfModule> section is used to mark directives that are conditional. The directives within an IfModule section are only processed if the test is true. If test is false, everything between the start and end markers is ignored.

    The test in the <IfModule> section directive can be one of two forms:

    * module name

    * !module name

In the former case, the directives between the start and end markers are only processed if the module named module name is included in Apache -- either compiled in or dynamically loaded using LoadModule. The second format reverses the test, and only processes the directives if module name is not included. The module name argument is the file name of the module, at the time it was compiled. For example, mod_rewrite.c. <IfModule> sections are nest-able, which can be used to implement simple multiple-module tests.

Scope of Directives

Related Directives:

<Directory>

<DirectoryMatch>

<Files>

<FilesMatch>

<Location>

<LocationMatch>

<VirtualHost>

    Directives placed in the main configuration files apply to the entire server. If you wish to change the configuration for only a part of the server, you can scope your directives by placing them in <Directory>, <DirectoryMatch>, <Files>, <FilesMatch>, <Location>, and <LocationMatch> sections.  These sections limit the application of the directives which they enclose to particular filesystem locations or URLs. They can also be nested, allowing for very fine grained configuration. Apache has the capability to serve many different websites simultaneously. This is called Virtual Hosting. Directives can also be scoped by placing them inside <VirtualHost> sections, so that they will only apply to requests for a particular website.

    Although most directives can be placed in any of these sections, some directives do not make sense in some contexts. For example, directives controlling process creation can only be placed in the main server context. To find which directives can be placed in which sections, check the Context of the directive. For further information, we provide details on How Directory, Location and Files sections work.
.htaccess Files

Related Directives :

AccessFileName

AllowOverride

    Apache allows for decentralized management of configuration via special files placed inside the web tree. The special files are usually called .htaccess, but any name can be specified in the AccessFileName directive. Directives placed in .htaccess files apply to the directory where you place the file, and all sub-directories. The .htaccess files follow the same syntax as the main configuration files. Since .htaccess files are read on every request, changes made in these files take immediate effect.

    To find which directives can be placed in .htaccess files, check the Context of the directive. The server administrator further controls what directives may be placed in .htaccess files by configuring the AllowOverride directive in the main configuration files.