Wednesday, September 3, 2008

Fedora 8 Quickie Apache Server Setup (Part II)

Note: Make sure to check the conventions I use before going through the tutorial.

Hello! For second part of our web server setup, we'll setup CGI script and Virtual Hosting. As with previous post you can use http://localhost for testing if you haven't setup local DNS from here

Running CGI Scripts

CGI scripts are a way for you to run external program and dynamic content on your Apache web server. For this post, we'll only run very basic BASH shell script but note that there are a number of scripting language you can use. Perl (forever classic way to run cgi), Php (popular but on it's way out), Python (slowly inching it's way to being the de-facto scripting language of choice among the geeky crowd), Ruby (distant cousin of Python) and host of other non-open-source languages.

By default, all CGI scripts are place in /var/www/cgi-bin directory. This is sufficient for our testing. However, if you need to change them, then look for following parameters in /etc/httpd/conf/httpd.conf file and change them to your preferred settings:

ScriptAlias /cgi-bin/ "/var/www/cgi-bin/"

<Directory "/var/www/cgi-bin">
AllowOverride None
Options None
Order allow,deny
Allow from all
</Directory>

Step 1: Log in as root, and create a basic cgi BASH script.

# cd /var/www/cgi-bin/
# nano test.sh

#!/bin/bash
echo Content-Type: text/html
echo
echo "<pre>"
echo "Hello, this is test cgi script and my username is `whoami`"
echo "My id is `id`"
echo "Today is `date`"
echo "</pre>"

Step 2: Set the execute permission for the script

# chmod 755 test.sh

Step 3: Log in as local user (ie. gene) and test the cgi script

$ firefox http://localhost/cgi-bin/test.sh
or
$ firefox http://www.exampledns.com/cgi-bin/test.sh

Setting Virtual Host

Virtual Hosting refers to the feature of Apache to host multiple domain from a single web server. So you have something like a www.company1.com and ww.company2.com hosted on a single server. There are two way to implement virtual hosting, IP-based and Name-based. For our setup, we'll use Name-based as it's generally more straight-forward.

Note: Before taking it for a test drive, make sure to setup a domain for www.company1.com and www.company2.com. We'll use these for our virtual domain. You can follow a quick DNS setup here.

Step 1: Do a quick dig if the test domains are properly resolving.

$ dig www.company1.com
$ dig www.company2.com

Step 2: Log in as root and create the appropriate subdirectory and test page for "www.company1.com" and "www.company2.com".

# cd /var/www
# mkdir virtual
# mkdir virtual/company1
# mkdir virtual/company2
# echo "Welcome to Company1" > virtual/company1/index.html
# echo "Welcome to Company2" > virtual/company2/index.html

Step 2: As root, edit /etc/httpd/conf/httpd.conf file and add the following settings at bottom of of the file.

NameVirtualHost *:80

<VirtualHost *:80>
ServerName www.company1.com
DocumentRoot /virtual/company1
ErrorLog logs/company1_error_log
CustomLog logs/company1-access_log common
</VirtualHost>

<VirtualHost *:80>
ServerName www.company2.com
DocumentRoot /virtual/company2
ErrorLog logs/company2_error_log
CustomLog logs/company2-access_log common
</VirtualHost>

Step 3: Restart the Apache web server and point your web browser to the virtual domain.

# service httpd restart
# firefox http://www.company1.com
# firefox http://www.company2.com

You now should have a rudimentary virtual hosting setup, enough to get you started.

No comments: