Linux Setup Notes

name and address
created dec 15, 2009

Enabling server-side includes (SSIs) in Apache

Server-side includes are the recommended way of including one html file in another. This is very convenient if you have a file that changes frequently. Or it would be, except for one problem: the included file cannot have an html header or <body> statements, or the page will not validate. This means your included file cannot be a complete html page, but must be an HTML fragment.

  1. Compile apache, specifying --enable-includes in the configure command (see linuxsetup42.html).
  2. Type the line ./httpd -l | grep mod_include. It should print mod_include.c , which indicates that mod_include is compiled into apache.
  3. If your version of apache was already compiled by someone else, verify that it contains the compiled-in mod_include option by typing httpd -l. If it doesn't, you will have to find a file called mod_include.so somewhere (on my system, it's at /usr/lib64/apache2-prefork/mod_include.so). Apache2 must load this file as a separate module. To do this, add the line
     LoadModule include_module modules/mod_include.so
    to your httpd.conf file.
  4. Add the following lines to your httpd.conf file:
    AddType text/html      .shtml
    AddHandler server-parsed .shtml
    
    AddType text/html      .html
    AddHandler server-parsed .html
    The first two lines let you use includes in files with the extension .shtml. The last two lines let you use includes in files with the extension .html. Many people use only the first two, because most files don't contain includes. This reduces the load on the server somewhat; however, in that case, SSIs will only work in files with an .shtml extension. You will then have to deal with complaints from people who can't find your page because they forgot to type the 's'.
  5. Add Includes to your Options section in httpd.conf like so:
    <Directory "/usr/local/apache2/htdocs">
        Options FollowSymLinks ExecCGI Includes
        ... other stuff ...
    </Directory>
  6. Restart apache
    cd /etc/init.d
    su
    ./apache2 stop ./apache2 start

    Note: simply typing ./apache2 restart doesn't always work.
  7. Create an html or shtml file as appropriate, and add some include lines in the body:
    <!--#include virtual="some-included-file.html" -->
    <!--#include file="some-included-file.html" -->
    <!--#echo var="DATE_LOCAL" -->
    This file last modified <!--#echo var="LAST_MODIFIED" -->
    

    Other variables are DATE_GMT, DOCUMENT_URI, DOCUMENT_PATH_INFO, USER_NAME, DOCUMENT_NAME, and QUERY_STRING_UNESCAPED.

  8. Note that, because of where the Options section was placed in the httpd.conf file, SSIs will only work if the file is in the htdocs file. If you open the file somewhere else (for example, using file:///) it won't work.
  9. If SSIs are working on this server, you should see a date below before the word "today."
    This file last modified today.

Back