How to enable Server Side Includes

For Apache 1.3.6-7 on Red Hat Linux 6.0

By Phil Jones (pjls16812 at blueyonder.co.uk)


Index


Intro

The Apache server supplied with Red Hat Linux 6.0 has server side includes disabled by default. Here is a procedure to get it going, test it and hopefully understand server side includes. To follow this procedure you must be logged in as root.


Setup

1) Edit /etc/httpd/conf/srm.conf. This is the Apache server configuration file.
2) Navigate to line 173. Edit this section so it reads:
# To use CGI scripts:
AddHandler cgi-script .cgi

# To use server-parsed HTML files
AddType text/html .shtml
AddHandler server-parsed .shtml
3) Save changes to srm.conf and quit the text editor. If you get an error, make sure you are logged in as root.

4) Restart the Apache server. To do this, enter on the command line:

cd /etc/rc.d/init.d
./httpd restart

Testing

Follow these instructions to set up an example Perl script and call it from a HTML page. I have tried to make this newbie friendly by leaving nothing out.

1) Navigate to the cgi-bin directory served by Apache:

cd /home/httpd/cgi-bin
2) Create a new file called helloworld.cgi. Enter the following lines, then save helloworld.cgi in the cgi-bin directory and quit the text editor.
#!/usr/bin/perl
# SSI test file
print "Content-type: text/html\n\n";
print "Hello world";
exit;
3) Check the file was created okay, enter:
ls -l
You should see:
-rwxrwxr--   1 root     root          108 May 23 19:21 helloworld.cgi
4) Make the helloworld.cgi file executable, like so:
chmod +x helloworld.cgi
5) Test it works, enter:
perl helloworld.cgi
You should see:
Content-type: text/html
Hello world
This means Perl processed helloworld.cgi successfully and put the results on the screen. If you get a syntax error, most likely you mis-spelt something or forgot a semicolon.

6) Next navigate to the HTML directory, enter:

cd /home/httpd/html
7) Make a new file called helloworld.shtml, don't forget the s, and enter the following lines:
<html>
<body>
Test next line
<!--#exec cgi="/cgi-bin/helloworld.cgi"-->
</body>
</html>
8) Save helloworld.shtml in the html directory, then quit the text editor.


The moment of truth!

Open a web browser, point it at helloworld.shtml, and you should see " Test next line Hello world ". That means success. :-)

If you get an error, navigate to /var/log/httpd and view the file 'error_log' to find out what's the matter. If the error log says "Invalid CGI ref" it means the reference to the CGI script in the HTML file is incorrect. Check against the HTML file shown in point 7 above.

If the hello world text doesn't appear in the browser, maybe you haven't restarted Apache.

If the Perl program code appears in the browser, it means Apache hasn't been configured to process files ending in .cgi as CGI scripts. Solution: make sure the following line is uncommented in your server configuration files:

AddHandler cgi-script .cgi 

The browser doesn't know...

Assuming it worked, now view the source of helloworld.shtml in your web browser. You'll see:
<html>
<body>
Test next line
Hello world
</body>
</html>
The browser really does have no idea there's script on the original page. That's because the server does all the work and gives the finished HTML to the browser.


Links