I want to restart Apache when I change configuration file on a web application which is I need to create a php file for restarting Apache but I dont want to give a permission to user apache in /etc/sudoers Can someone explain intensively to me ?

Don't see this is duplicate because I am really a newbie for this apache and script I am sorry if anynoe see this as an annoying topic.

This is what I tried without giving any permission

restart.sh

#!bin/bash
/etc/init.d/httpd restart

restart.php

<?php
exec('./restart.sh');
?>

edit /etc/sudoers by add

sudo /var/www/html/home/restart.sh

when I run ./restart.sh it works perfectly but when I run

php restart.php

It can't restart giving me this error

   (13)Permission denied: make_sock: could not bind to address [::]:80
(13)Permission denied: make_sock: could not bind to address 0.0.0.0:80
no listening sockets available, shutting down
Unable to open logs

Obviously this is not an easy task. When you invoke the restart on Apache, it will automatically kill the PHP script that was used to invoke it. Reason: Apache --> executed PHP --> executed PHP script.

Processes can only die, if all their children die as well. So this means in your case the script will kill itself.

This makes it necessary to do this in an asynchronous way. Either have an external script like your shell script and perform a "schedule" of this script.

Example:
calculate the current time of the day (let's assume it would be 4:30 PM) then schedule an Apache restart at 4:31 PM using for example the "at" command (http://linux.die.net/man/1/at).

Caution:
if this fails, your Apache server might not restart properly and you will lock yourself out. E.g. when the "Stop" command works properly but there is a syntax error in your httpd.conf file.

Other options:
Create the httpd.conf file and instead of re-starting Apache right away, make sure that the syntax is correct by using "httpd -t" or "httpd -S". If this check tells you that the config has no errors you could do a "reread" of the config only. Depending on the operating system this might work without an actual restart. You will still need appropriate permissions (meaning a sudo entry). But this is true for all the solutions.

Your PHP restart script may need to specify a valid IP address, such as localhost (127.0.0.1:80). 0.0.0.0:80 is not a valid IP address. FWIW, the :80 part is the port that PHP will be listening on. Also, port 80 is the default for http (web services), and that may be already used by Apache. What version of PHP are you using?

You can also stop httpd using /sbin/service httpd stop. The restart option is a shorthand way of stopping and then starting the Apache HTTP Server.

*You can restart the server as root by typing:

apachectl restart
or:
/sbin/service httpd restart
www.uaesol.com

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.