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

Dear Monks, I've been looking for a good cross platform way to restart apache web server from a cgi script (mod_perl 2). Any ideas?
  • Comment on restart apache web server from CGI script

Replies are listed 'Best First'.
Re: restart apache web server from CGI script
by jbrugger (Parson) on Jan 03, 2006 at 11:49 UTC
    I'd create a batchfile / bash-script that performs the action as the proper user in the os-specific language, and then `` or qx, perlfunc:system or perlfunc:exec that script.
    But i'm not keen on having a cgi-script call a apache restart.
    I'd use Webmin, (running on windows as well i see)!, it's running it's own webservice.

    "We all agree on the necessity of compromise. We just can't agree on when it's necessary to compromise." - Larry Wall.
Re: restart apache web server from CGI script
by Anonymous Monk on Jan 04, 2006 at 08:53 UTC
    Hi
    This might be a little long winded but safer then running the script as root.

    Why don't you perfom some kind of request in your CGI which creates a text file somewhere in f.e. /tmp if a certain condition returns true. Then you let root or Administrator run a task every minute to check if this file exists. If so, root / Administrator can restart you Apache Server.

    You also might want to include " if ($^O eq Win32) {...} elsif ($^O eq Linux) {...}

    Marcel

      I was just thinking if one can't create an Apache module to control the service/daemon ? I know this is a funny way of doing it, but it will allow Apache to selfrestart upon request either via web or internaly calling a function... It's just a tought. I've been using Sudo in other scripts... on top of everything this is one user machine - ie the user is root, and the machine is running Intranet, which means - no Internet access is provided to the computer, which solves the security issues.
Re: restart apache web server from CGI script
by pileofrogs (Priest) on Jan 03, 2006 at 23:49 UTC

    This is especailly scary since whatever your script is, it has to run as root/admin to start the web server. Tread softly.

      I second pileofrogs's comment.

      Update: I forgot the "cross platform" requirement. I don't believe any of the following applies to Windows systems.

      On Linux the kernel will not run scripts started with the first line "#! ..." setuid. I refer you to google: linux run script setuid

      You could write a program and compile it to an executable file then make the executable setuid. Of course anyone who can execute that program can restart apache.

      You could try using the "sudo" command. man.sudo(8). The idea is you can say that whatever user apache runs as is allowed to run the command "/path/to/apachectl restart" as another user (like root) without specifying a password.

      Now suppose there is a CGI that updates the apache config files. Further suppose that one time this introduces an error in the configs. Now when the CGI to restart apache is run apache might not restart. I don't know what you are doing. This is just a thought brought on by experience. Think about what could go wrong with a system before building it.
Re: restart apache web server from CGI script
by cowboy (Friar) on Jan 05, 2006 at 21:06 UTC

    If you're talking a dev machine, a cron job/some scheduled event in windows that will stop/start it if a certain file exists works fine for us here.

    I certainly wouldn't trust this in production though... you're depending on the webserver running, to start the webserver. If startup fails, not only is the site down, but the ability to start it back up is down as well.
Re: restart apache web server from CGI script
by kwaping (Priest) on Jan 03, 2006 at 16:11 UTC
    Which platforms are you crossing?
      Win32 (Windows XP) and Linux (Gentoo i386 platform) I was thinking of using an external script to do that but I thought that there is a better way to do it via mod_perl.