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
How to Route Webcam Video to Virtual Video Device on Linux (Via Opencv)
Escaping the Exclamation Point in Grep
Old Logs Are Not Imported into Es by Logstash
How to Merge Two Seperate - Yet Similar - Codebases into One Svn Rep
Raspberry Pi Refusing Connection to Bottle Server
Interacting with Files from Multiple Directories via Bash Script
Bash: Checking If Files Are Duplicates Within a Directory
How to Find Process Using Tcp Port
Terminate an Ongoing Qprocess That Is Running Inside a Qthread
Why Count Differs Between Ls and Ls -L Linux Command
Replace Line with Space and Backslash with a String Containing Spaces
Add Month to a Variable Date in Shell Script
How to Make a Bash String of Command with Redirect and Pipe
Rust Linux Version Glibc Not Found - Compile for Different Glibc/Libc6 Version
Qserialport Cannot Open Tty After Application Has Previously Been Run by 'Root'
How to Split Flv File by Size Using Ffmpeg or Mencoder or Smth Else