Professional Documents
Culture Documents
Apache Configuration File
Apache Configuration File
November18th 2011
mod_mime
Related Directives
Apache can be configured by placing the directives in plain text configuration files. httpd.conf is usually called as main configuration file. It can be overridden with the f command line flag but it location is set at compile-time. Configuration files may be added using the Include directive, and wildcards can be used to include many configuration files.
Page 1 of 18
NilayThakkar
November18th 2011
Any directive may be placed in any of these configuration files. Changes to the main configuration files are applied when it is restarted. Server file containing mime document types; the filename is set by the TypesConfig directive, and is mime.types by default.
Page 2 of 18
NilayThakkar
November18th 2011
The values of shell environment variables can be used in configuration file lines using the syntax ${ENVVAR}. If "ENVVAR" is the name of a valid environment variable, the value of that variable is substituted into that spot in the configuration file line, and processing continues as if that text were found directly in the configuration file. (If the ENVVAR variable is not found, the characters "${ENVVAR}" are left unchanged for use by later stages in the config file processing.) The maximum length of a line in the configuration file, after environment-variable substitution, joining any continued lines and removing leading and trailing white space, is 8192 characters. You can check your configuration files for syntax errors without starting the server by using apachectl configtest or the -t command line option.
Page 3 of 18
NilayThakkar
November18th 2011
Modules
Related Modules Related Directives
<IfModule> LoadModule
mod_so
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
Page 4 of 18
NilayThakkar
November18th 2011
Scope of Directives
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.
Page 5 of 18
NilayThakkar
November18th 2011
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
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
Page 6 of 18
NilayThakkar
November18th 2011
directives may be placed in .htaccess files by configuring the AllowOverride directive in the main configuration files.
November18th 2011
AllowOverride directive. This directive specifies, in categories, what directives will be honored if they are found in a .htaccess file. If a directive is permitted in a .htaccess file, the documentation for that directive will contain an Override section, specifying what value must be in AllowOverride in order for that directive to be permitted. For example, if you look at the documentation for the AddDefaultCharset directive, you will find that it is permitted in .htaccess files. (See the Context line in the directive summary.) The Override line reads FileInfo. Thus, you must have at least AllowOverride FileInfo in order for this directive to be honored in .htaccess files. Example: Context: server config, virtual host, directory, .htaccess Override: FileInfo
Page 8 of 18
NilayThakkar
November18th 2011
If you are unsure whether a particular directive is permitted in a .htaccess file, look at the documentation for that directive, and check the Context line for ".htaccess".
Scope of directives
All the directives placed in the main configuration files applies to the entire network. By placing the directives in <Directory> , <DirectoryMatch> , <Files> , <FilesMatch> , <Location> and <LocationMatch> sections we can scope our directives and can change theconfiguration for only that part of the server. To particular file location these sections limit the applications of the directives to which they enclose. The term virtual hosting means that the apache server has the capability to serve many different websites simultaneously . Directives can also be scoped by placing them inside
Page 9 of 18
NilayThakkar
November18th 2011
<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.
Page 10 of 18
NilayThakkar
November18th 2011
November18th 2011
In the directory /www/htdocs/example1 we have a .htaccess file containing the following: Options +ExecCGI (Note: you must have "AllowOverride Options" in effect to permit the use of the "Options" directive in .htaccess files.) In the directory /www/htdocs/example1/example2 we have a .htaccess file containing: Options Includes Because of this second .htaccess file, in the directory /www/htdocs/example1/example2, CGI execution is not permitted, as only Options Includes is in effect, which completely overrides any earlier setting that may have been in place.
Page 12 of 18
NilayThakkar
November18th 2011
Authentication Example
If you jumped directly to this part of the document to find out how to do authentication, it is important to note one thing. There is a common misconception that you are required to use .htaccess files in order to implement password authentication. This is not the case. Putting authentication directives in a <Directory> section, in your main server configuration file, is the preferred way to implement this, and .htaccess files should be used only if you don't have access to the main server configuration file. See above for a discussion of when you should and should not use .htaccess files. Having said that, if you still think you need to use a .htaccess file, you may find that a configuration such as what follows may work for you.
Page 13 of 18
NilayThakkar
November18th 2011
You must have "AllowOverrideAuthConfig" in effect for these directives to be honored. .htaccess file contents: AuthType Basic AuthName "Password Required" AuthUserFile /www/passwords/password.file AuthGroupFile /www/passwords/group.file Require Group admins Note that AllowOverrideAuthConfig must be in effect for these directives to have any effect.
Page 14 of 18
NilayThakkar
November18th 2011
Options +Includes AddType text/html shtml AddHandler server-parsed shtml Note that AllowOverride Options and AllowOverrideFileInfo must both be in effect for these directives to have any effect.
November18th 2011
Note that AllowOverride Options and AllowOverrideFileInfo must both be in effect for these directives to have any effect.
Troubleshooting
When you put configuration directives in a .htaccess file, and you don't get the desired effect, there are a number of things that may be going wrong. Most commonly, the problem is that AllowOverride is not set such that your configuration directives are being honored. Make sure that you don't have aAllowOverrideNone in effect for the file scope in question. A good test for this is to put garbage in your .htaccess file and reload. If a server error is not generated, then you almost certainly have AllowOverride None in effect. If, on the other hand, you are getting server errors when trying to access documents, check your Apache error log. It will likely tell you that the directive used in your.htaccess file is not permitted. Alternately, it may tell you that you had a syntax error, which you will then need to fix.
Page 16 of 18
NilayThakkar
November18th 2011
Bibliography
1. http://httpd.apache.org/docs/2.2/configuring.html 2. Apache Tutorial version 2.0
Page 17 of 18
NilayThakkar