Linux Ask!

Linux Ask! is a Q & A web site specific for Linux related questions. Questions are collected, answered and audited by experienced Linux users.

Feb 122010
 

Turn on expire headers in Apache

Answer:

By turning on the expire headers, browser will cache the files serving from Apache more aggressively, so as to reduce traffic loading to the Apache web server.

Steps:

Add the following lines to the Apache configuration (httpd.conf)

<IfModule mod_expires.c>
    ExpiresActive On

    # Cache all files for 2 weeks after access (A).
    ExpiresDefault A1209600
    
    # Do not cache dynamically generated pages.
    ExpiresByType text/html A1
</IfModule>

Restart Apache to take effect

# apachectl -k graceful

Assume the module mod_expires is enabled

Feb 042010
 

Turn on content compression in Apache

Answer:

By turning on content compression in Apache, content's transfer size can be reduced for users and hence a faster loading time. However, not all content types can be compressed, e.g. images, videos, we usually only compress text content types, e.g. html, xml, css & js.

Steps:

Add the following lines to the Apache configuration (httpd.conf)

<IfModule mod_deflate.c>

    AddOutputFilterByType DEFLATE text/html text/xml application/xml application/javascript application/json text/css text/plain
    header set Vary Accept-Encoding

    # Don't compress images - Important for IE caching bug
    SetEnvIfNoCase Request_URI \.(?:gif|jpe?g|png)$ no-gzip dont-vary
    header unset Vary env=dont-vary

</IfModule>

Restart Apache to take effect

# apachectl -k graceful

Assume the module mod_deflate is enabled

Jan 152010
 

How to enable Server Side Includes (SSI) ?

Answer:

Pick either one of the following method and add the following lines to Apache configuration (httpd.conf).

1. Enable SSI on files with extension .shtml

AddType text/html .shtml
AddHandler server-parsed .shtml

2. Enable SSI on files if they have the user-execute bit set

XBitHack on

Reference: http://httpd.apache.org/docs/1.3/mod/mod_include.html

Jan 152010
 

Sending Apache log to syslog?

Answer:

In the Apache config (httpd.conf), add/modify the followings as needed

1. For Access log

CustomLog |/usr/local/apache/bin/apache_syslog combined

Where apache_syslog is a Perl script


#!/usr/bin/perl
use Sys::Syslog qw( :DEFAULT setlogsock );

setlogsock('unix');
openlog('apache', 'cons', 'pid', 'local2');

while ($log = ) {
            syslog('notice', $log);
}
closelog

2. For Error log

Apache already has direct support error log to syslog, so just add the target syslog facility.

ErrorLog syslog:local1

This tells Apache to send the error log output to the syslog facility local1