2006/08/21

Set up public_html and CGI

By default, http://exaple.com will be translated to the file path '/var/www/index.html' or default apache page if index.html does not exist. Also, all files in ‘/var/www/cgi-bin’ will be reconginzed as being eligible for execution rather than process as normal document, no matter the file name (in the other word, the file can be ‘anyname.anyexten’).

Of course, the ordinary user can also have 'http' and 'cgi' supported. Just follow the below steps:

Prerequisites:
1. You must have ‘root’ privilege
2. Apache has been installed and started, in my machine I use following commands to check

/usr/sbin/httpd status (check the Apache status)
/usr/sbin/httpd –h (help file of httpd command)


Step one: create public_html and cgi-bin at a user’s home directory, change mods to 755

Step two: edit server configuration file, in my machine it is ‘/etc/httpd/conf/httpd.conf’

vi /etc/httpd/conf/httpd.conf

a. Set the file path with UserDir

UserDir public_html

b. Enable a cgi directory for a user

<Directory /home/*/public_html/cgi-bin/>
Options ExecCGI
SetHandler cgi-script
</Directory>

You can see http://httpd.apache.org/docs/2.0/howto/public_html.html for detail.

IMPORTANT:
You need to restart Apache to invoke the change. I use below command:

/usr/sbin/httpd - k restart

NOTE:
1. Every file in cgi-bin will be executed as a script, every file in public_html will be executed as normal documents
2. Don’t need to change other perameter in httpd.conf, which you may think need to be changed, such as ‘AddHandler cgi-script .cgi’, ect. I will explain how to use them in next.

OK! Now it is the time to write a simple html file, for example index.html, in public_html, and write a simple cig/perl file in cgi-bin you created before. Set permission of those files to 755. You should be able to run them from browser.

You may want to run script everywhere instead of the scripts in cgi-bin. You can do this by following steps:

Step one: edit server configuration file.

vi /etc/httpd/conf/httpd.conf

Add a line, or just move the ‘#”,

Addhandler cgi-script .cgi .pl

.cgi and .pl means the file with those extention will be executed as script

Step two: search for the line that says "<directory>" in the file. It should look something like this:

<Directory>
Options FollowSymLinks
AllowOverride None
<Directory/>


Add "+ExecCGI" to the options list. The line now looks like this:

Options FollowSymLinks +ExecCGI

Note:
1. You need to restart Apache to invoke the change.
2. Doing this does not affect the file in cgi-bin. In the other word, every file in cgi-bin is still treated as scripts no matter what file extension.
3. When you find the problem, you can check the error logs by

cat /var/log/httpd/error_log

1 comment:

  1. Just found a new way to tell Apache to reread its configuration file:

    apachectl graceful


    instead of doing a full sever restart.

    ReplyDelete