View Full Version : How To Separate cgi-bin Directories For Each Domain

07-18-2008, 03:20 PM
By default, Apache is configured to use a single cgi-bin directory for all domains. Assuming that you have two domains, primary.com and secondary.com on your hosting account, pointing respectively to /var/www/html and /var/www/secondary, the default configuration causes http://primary.com/cgi-bin/ and http://secondary.com/cgi-bin/ to both reference files within the /var/www/cgi-bin/ directory. One might expect (desire) that these URLs respectively reference files within the /var/www/html/cgi-bin/ and /var/www/secondary/cgi-bin/ directories.

You can configure Apache to follow this expected behavior, giving you multiple cgi-bin directories and allowing you to have different CGI scripts under each domain. To do this, edit the /etc/httpd/conf/httpd.conf file. Remove the following lines (beginning around 549):

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

# "/var/www/cgi-bin" should be changed to whatever your ScriptAliased
# CGI directory exists, if you have that configured.
<Directory "/var/www/cgi-bin">
AllowOverride AuthConfig FileInfo Limit
Options None
Order allow,deny
Allow from all
# Allow .html/.htm files to be served from the cgi-bin
AddHandler server-parsed .shtml .html .htm

Replace the removed lines with the following:

# Execute scripts from each domain's own cgi-bin directory
<Location /cgi-bin>
AllowOverride AuthConfig FileInfo Limit
Options +ExecCGI
AddHandler cgi-script .cgi .pl .py .sh

That's it! Simply restart your Apache web server using the restart function within Site Manager or by running the command apachectl graceful for the change to take effect.

Note that by eliminating the use of the ScriptAlias directive, you are now also allowed to mix CGI and non-CGI content within a domain's cgi-bin directory. In other words, the cgi-bin directory for a domain can now serve images, HTML pages, PHP files, etc., and any CGI content must have one of the file extensions listed in the AddHandler line above.

Update: If you're using Miva Merchant, you will also need to add the following code:

# Override for Miva Merchant
<Location /cgi-bin/mivavm>
SetHandler cgi-script

07-19-2008, 07:39 AM
One thing to keep in mind when using this method is that it might break WH default installation of things for the main domain. Default installations well be made to /var/www/cgi and because the ScriptAlias is removed they well not work.

07-19-2008, 12:37 PM
At least in my testing, this does not cause a problem with the default installation because /var/www/html/cgi-bin is a symbolic link to /var/www/cgi-bin. In other words, the primary domain already has a cgi-bin within it (even if it is a symbolic link instead of an actual folder, Apache will follow the link and find the "default" WestHost cgi-bin). This should be safe to use even if you only have one domain on the account. I've tested the change on my account and am still able to use http://primary.com/manager (which invokes a script in the cgi-bin to redirect to the Site Manager login) as well as FormMail. If you try this and it breaks the default cgi-bin, please let me know and I'll revise the code appropriately.

07-20-2008, 08:03 AM
Ahh yes you are right. I forgot that the symlink was there. It is doubtful this would break the defaults.