How to restart Apache using a button (web button)?

I play with my virtual machine, and the code I'm developing requires that I restart apache every time in order to commit the changes. I thought it would be nice to have a bookmarklet, or a link or button that I could launch to do this. Any thoughts on how to do this with PHP on CentOS 5 dev VM?

+7
source share
5 answers

As Mark B said, you need root privileges to restart Apache. The best way to handle this, IMHO, is to let the user know that Apache is running under access to restart Apache through the sudo .

You want to edit the /etc/sudoers file and add lines similar to the following:

 Cmnd_Alias RESTART_APACHE = /sbin/service apache2 restart www-data ALL=NOPASSWD: RESTART_APACHE 

You may need nobody instead of www-data , it depends on the user Apache is running on. On Debian, Apache usually runs under the www-data user, while on Red Hat, Apache often runs under the nobody user. In addition, /sbin/service apache2 restart may require /sbin/service apache restart or maybe /sbin/service httpd restart . It all depends on your system configuration.

After that, in PHP you can use the code:

 exec('/sbin/service apache2 restart'); 

(Obviously, this is a change if the Apache restart command is different on your server.)

Please note: this may be considered a security risk! If you do this, you completely trust the sudo binary, the service binary and your system to abide by the rules and prevent the Apache / PHP process from getting the root shell. I highly recommend asking http://serverfault.com for an understanding of what you are doing here.

+17
source
 <?php echo shell_exec('service httpd restart &'); ?> 

Perhaps you may have permission problems with a script that is trying to do this. It seems that you are in a fully functional development environment, so you do not have to give elevated privileges.

+4
source

The file contains the process identifier for apache. Just send him a HUP. To do this, you can write a simple script (sh) - and put it in the cgi-bin directory.

0
source

These conditions do not work !!! Anyone who found it works correctly ???

0
source

If you are using linux, you can write a shell script to take care of you.

eg.

 if [ $? -ne 0 ] # if apache not running then # restart apache $RESTART 

More on this blog: http://www.droidmsg.com/blog/?p=146

-4
source

Source: https://habr.com/ru/post/895900/


All Articles