leocharre has asked for the wisdom of the Perl Monks concerning the following question:

I have a question about the structure of a site on apache.

I put my cgi executables on cgi-bin Sometimes scripts need things like conf files or templates.. I've been letting users configure where needed files might be. For example, a generic login script expects by default to have a template file in

$ENV{DOCUMENT_ROOT}/.templates/login.html but it can be overridden. etc. The idea is that the scripts will by default expect things to be in predetermined places- kinda like unix that expects logs to be in /var/logs and conf to be in /etc/whatever - This is just proving tough on hosting accounts.
Keeping track of scripts and where their files are expected to be is proving painful. Installation and update is proving painful.

I was considering having the script, conf files, templates, db whatever all in one directory under cgi-bin. Ala windows.. isn't it? - is that bad juju?

  • Comment on cgi apps, is it bad juju to pile program and conf file, template, etc under one directory in cgi-bin?

Replies are listed 'Best First'.
Re: cgi apps, is it bad juju to pile program and conf file, template, etc under one directory in cgi-bin?
by grep (Monsignor) on Oct 06, 2006 at 18:19 UTC
    As sgifford pointed out - This is a bad idea. But it has a very simple solution.

    Keeping track of scripts and where their files are expected to be is proving painful. Installation and update is proving painful.

    Make a standard. It really doesn't matter much what it is, but just having a standard you follow will make your maintenence much simpler.

    Here is what I do. You don't have to do this, but I like and it seems to work well :

    /www /project_name /html /cgi /lib /conf /images /tmp #if needed
    This way Apache (in a straight cgi app) only needs to know about the 'cgi', 'html', and 'images' dir. It won't try to serve files out of 'lib', 'conf', or 'tmp'. This system also translates to __insert_favorite_versioning_system_here__ very well. You can set the 'images' folder for binary processing and ignore the 'tmp' dir.

    If you don't standardize you layout, it's quite easy to make a simple mistake that exposes your 'lib' or 'conf' files. You'll get into the habit of not including these directories in your httpd.conf and all you really need to when progamming is  use lib '../lib/';

    Then make a simple script to create your standard layout. You can even include some starter scripts with some standard modules like Template Toolkit or CGI::Application, ...



    grep
    One dead unjugged rabbit fish later
Re: cgi apps, is it bad juju to pile program and conf file, template, etc under one directory in cgi-bin?
by sgifford (Prior) on Oct 06, 2006 at 17:29 UTC
    The risk of having everything under a directory the Web server contains is that they may be accessible in surprising ways. For example, if your configuration contains database passwords and it's in a directory the server treats as HTML, anybody can get your passwords by simply loading that file. Similarly, if you put utility libraries in a directory where the Web server expects CGI scripts, the libraries can be run by Web clients.

    One way around that would be to install everything in one directory outside the Web server's area, then use symlinks to link in the parts that should be available on the Web.

    Another way is to put all support files in a subdirectory, and put a .htaccess file that will block access (though that will only work under Apache).

    Yet another way is to have a smart installer run one time via the Web, which will figure out where things should go and put them there, then disable itself. I've seen that approach used before, most often in PHP scripts, and it makes it very easy to get things installed quickly.

      Another way is to put all support files in a subdirectory, and put a .htaccess file that will block access (though that will only work under Apache).

      ...And only if .htaccess is turned on to begin with.

      Ah, yes. I should have mentioned that I am (or at least *think* I am!) controlling access to the directories through Apache conf.

      Thanks for making that clear :-)

Re: Organization of CGI application files
by talexb (Chancellor) on Oct 06, 2006 at 20:25 UTC

    Good question. I have the following layout, more or less inherited from the original developer (Hi Mike), but something that makes good sense to me:

    /--| /code--| /bin /lib /web--| /html--| /images /cgi-bin /templates /utilbin /conf /data /logs /run /setup

    Inside the code directory, the bin and lib directories are programs and libraries that the web application will use; there's a web directory that holds the static HTML pages (with a sub for images), the CGIs and the templates for the dynamic pages. The utilbin directory is for programs that are associated with the web application but that will usually be run from the command line -- sort of like an sbin for the application, I suppose.

    Outside of the code directory I have directories for configuration files, data, log files, PID files and one for installation files.

    An application that I'm in the process of developing uses the same directory structure, with the difference that below the templates directory I have two more directories, html and yaml, based on whether I'm outputing HTML web pages or YAML objects for the REST interface.

    I like to have more, rather than less directories. I don't like to have everything in one directory -- yes, it's easy to find things, up to a point, then it starts to get hard to find things again.

    Alex / talexb / Toronto

    "Groklaw is the open-source mentality applied to legal research" ~ Linus Torvalds

Re: cgi apps, is it bad juju to pile program and conf file, template, etc under one directory in cgi-bin?
by bibliophile (Prior) on Oct 06, 2006 at 17:22 UTC
    I don't have any experience with hosting companies, but in my own sandbox, I have it set up like so that cgi-bin, DOCUMENT_ROOT, a templates directory, a log directory, a conf directory, an images directory, etc, are all siblings under /var/www

    It seems to work well enough. :-)

    -- WARNING: You are logged into reality as root.