473,386 Members | 1,796 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ


Configuring Apache Part 2

By Jillian Carroll
Administrator, TheScripts.com

Content Negotiation

I know what you are thinking, what is content negotiation? Well, first of all, it's an often overlooked feature of Apache. It is more accurately known as content selection, and is the selection of documents that best match a client's browser capabilities, from one of several available documents.

Why do you need it?

Well, you don't... but, if you are aiming your web site at a multi-lingual region, then having it would greatly be to your benefit. Not only will your documents be correctly served to the visitors, but it will also improve their stay at your site.

By default, content negotiation is compiled in with your server, as it is powered by mod_negotiation. If for some reason you didn't compile this feature in with Apache, you must go back to your Configuration.tmpl file and put it back in.

Ok, what do you need now? Well, you have to choose what languages you are going to be using, and have content available for both languages. There are actually two ways with which we can proceed, using a variants file, which can be found discussed here (http://www.apacheweek.com/features/negotiation), or, by using file extensions.

For this example, we will be using three different languages, English, French and German.

Now open up the access.conf file and lets get started. Firstly, we will need to specify a directory, which we will call international, and have it so that it is set for content negotiation. The international directory will be locate just off of the root document directory, which, in our case, is /usr/local/apache/htdocs/.

<Directory /usr/local/apache/htdocs/international>
Options MultiViews
</Directory>

Options MultiViews sets the directory so that the server does filename pattern match and chooses from among the results.

Now, open up httpd.conf so we can add our languages to look for, and how the server will identify them. We will be using the directive AddLanguage

AddLanguage en .en
AddLanguage fr .fr
AddLanguage de .de

Here we have added English, French, and German, all identified with the extension found to their right.

By default, quite a few languages are already set. You can comment them out and add as needed.

The LanguagePriority directive allows you to give precedence to some languages in case of a tie during content negotiation. The languages are listed in decreasing order or preference.

LanguagePriority en fr de

So how do you name your files now? The best method is to do something like test.html.en for an English document, test.html.fr for a French document, and test.html.de for a German document. When you would like to link to this document, you just use test.html.

For more information on Content Negotiation, see http://www.apache.org/docs/content-negotiation.html.

A Single Config File

Geez, why would this help?

Well, it would put all your options into one single file, which makes life easy when you want to edit anything. In this case, we will use httpd.conf to store all of our directives. For some commentary on the three configuration files, see http://www.virtualhostsetup.com.

Anyways, back to the single config file issue.

Right now you are using three configuration files, httpd.conf, access.conf, and srm.conf. You don't need to. Simply empty the important stuff from the latter two files into httpd.conf, and then add these two lines with them;

AccessConfig /dev/null
ResourceConfig /dev/null

This will inform Apache that httpd.conf is the only configuration file. With this in place, you can just simply remove the srm.conf and access.conf files, and proceed with a smile on your face.

Rotating Logs

So what are these log files you keep hearing about?

Every time a user requests a document from your site, the server 'logs' a record of this request into a log file. Log files contain vital statistics about users, such as their host, the date/time, and the request line, which contains browser information. If your site is even remotely busy, or it's just been a while since you've done anything to the logs, they will probably be rather large. Typically, they can reach many MB's in a short period of time. Since the best application for log files is for log analysis with other software, large log files can slow down this process to a crawl.

So, what can you do about it? Log rotation. Log rotation is a procedure that takes a log file, puts its contents into a new file, and clears its own contents afterwards. Since its a rather boring procedure, there have been a few scripts to help you do it, including one from the Apache group itself.

Once script for example is a small utility written to allow log files to be quickly split and processed. This utility will create a new log file for each day, and with the date as the extension. Once the new log file is created, the old one can be compressed or moved to a new location. The utility is called logbox, and is available from ftp://ftp.lemuria.org/pub/Code/logbox.tar.gz. You must compile it on your server before you can use it.

Another method of splitting files is made by the Apache group. Earlier you compiled it, which was also when you compiled apachectl and htpasswd. If you used the default settings, you also copied it to /usr/local/apache/bin/ afterwards.

This program is called rotatelogs (how ingenious!), and it is used with the TransferLog directive in Apache.

In httpd.conf, add something like this:

LogFormat "%h %l %u %t \"%r\" %s %b \"%{Referer}i\" \"%{User-agent}i\""
ErrorLog /usr/local/apache/logs/error_log
TransferLog "|/usr/local/apache/bin/rotatelogs /usr/local/apache/logs/access_log 86400" 

Let me explain this. The directive LogFormat really isn't too important for you to care actually.... it just specifies the template format for how the access and error logs will look. For more information on the options LogFormat provides, see http://www.apache.org/docs-1.2/mod/mod_log_config.html#formats

ErrorLog sets the name of the file to which the server will log any errors it encounters. This is important to look over from time to time to see what might be going wrong with your site/server.

Now, to the part you've been waiting for. The TransferLog directive. Here, we are mixing it up with the rotatelogs program at the same time. Technically, there is one argument to TransferLog here, but it can be broken down. The first part is the location to our rotatelogs program (in this case, it is at /usr/local/apache/bin/rotatelogs). The second part is the location of our access_log file (The file in which we log our server requests, located at /usr/local/apache/logs/access_log). The last part sets the amount of time before rotating the log files. It is in seconds, and since 86400 seconds is equal to 1 day (24 hours), we rotate the file every day. You can set this to whatever time period you would like. The generated name of the old log file (after each rotation) will be /usr/local/apache/logs/access_log.nnnn in this case, where nnnn is the system time the log started at. After each rotation time, a new, empty log is started.

Get some Extras

Since Apache is open source, there are a ton of extra's built for it. These extra's can greatly enhance the performance of your server, and even give you more possibilities with your web site. Two of the most notable add-on's include mod_perl, and mod_php

mod_perl is an excellent binding of the Apache Web server and Perl (http://www.perl.com/), the popular CGI scripting language. The module takes a copy of the Perl interpreter, and embeds it within Apache itself. This not only speeds up existing CGI's, but it also allows you to write more modules in Perl itself. The Perl scripts are compiled once this way, unlike the usual method. Usually, the scripts are compiled on run-time by the interpreter, which makes them run a little slower from the start. If they are already compiled though, they start instantaneously, making this module an excellent addition to a web site with high levels of traffic.

mod_php is another great addition to Apache. The powerful server-side scripting language, PHP (http://www.php.net/), which is also open-source/free, can easily be linked with the server upon compilation. PHP scripts can be run like normal CGI's, but this method allows them to be run at a much greater speed.

Both of these add-ons must be compiled in with Apache from the start though, as they are modules. If you would like to use them, just download them, edit your Configuration.tmpl file, run configure, and compile as usual.

There are instructions for these modules though, as they are a little more complicated. You can the documentation at http://perl.apache.org/ and http://www.php.net/ respectively.

« Configure Apache Optimizing Apache »

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.