How to Start/Restart/Stop Apache Server on Linux as Non-Root User

Is there a way to start/restart/stop apache server on linux as non-root user?

The problems pointed out above by benlumley, i.e. log files and reserved ports, can easily be overcome by configuring the log directory and port in your httpd.conf.

Restarting apache without being superuser

Linux will not honor the suid bit on a shell script. Read this for more information.

A common solution for this is the sudo command. With an entry like this in /etc/sudoers:

yourname ALL = NOPASSWD:/path/to/graceful-restart

You could run:

sudo /path/to/graceful-restart

And this would run with root privileges without prompting you for a password. See the sudoers man page for more information on the syntax of the sudoers file.

How to Start/Stop Tomcat for normal user except root in Linux CentOS

You can write a rule in visudo. Something like;

Cmnd_Alias     CUSTOM_CMD = /opt/tomcat/bin/startup.sh, /opt/tomcat/bin/shutdown.sh

myuser ALL = (root) NOPASSWD:CUSTOM_CMD

How do I programatically restart a system service(not apache) from apache in linux?

Like Skip said, but don't run the CGI as root. Instead, have the CGI call sudo. You can give your web server permission to run /etc/init.d/tomcat restart only in the sudoers file.

I've actually done this at work; the relevant part of the CGI looks like this:

#!/usr/bin/perl
use CGI;
use IPC::Run3;
my $CGI = new CGI;

my $output;
if (defined $CGI->param('go') && 'restart' eq $CGI->param('go')) {
run3 [ qw(sudo /etc/init.d/tomcat5.5 restart) ], \undef, \$output, \$output;
}

print <<EOF
Content-type: text/html

Blah, blah, blah, HTML form, displays $output at some point.
EOF

Here is an example line from /etc/sudoers (use visudo to edit, of course):

ALL     ALL=(root) NOPASSWD: /etc/init.d/tomcat5.5 restart

That allows everyone to restart tomcat. You could limit it to Apache only if you'd like.

Is it possible to start / stop apache service from PHP?

Yes, with exec()

exec("apachectl restart");

You might want to allow programs to close themselves before just shutting down the server, so I'd recommend:

exec("apachectl graceful");

Make sure PHP doesn't run in safemode (<= PHP 5.3), as these functions won't be available then.

Please note, this is how I restart apache on my server, you might have to adjust the command.

Also think about the permissions. Not all users (and probably not the one running php scripts) have permission to stop the server.



Related Topics



Leave a reply



Submit