1

I want to restart mysql service on click of the button using php.

I have developed application using php till now I tried below things and facing the problem

<?php
if ($_GET['run']) {
  # This code will run if ?run=true is set.
  var_dump(exec("sh rst.sh"));
  exit;
}
?>

<!-- This link will add ?run=true to your URL, myfilename.php?run=true -->
<a href="?run=true">Click Me!</a>

What I am getting is:

string(55) "Restarting mysql (via systemctl): mysql.service failed!" 

rest.sh:

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

while executing above file from the commnad line it asks for the password.

any help will be appreciated.

Thanks

Casual Coder
  • 81
  • 12
  • That's the error message from your CLI script and has nothing to do with PHP. – Dormilich Apr 20 '18 at 12:24
  • Does the apache user have permission to execute `rst.sh` and whatever functions that calls? – chris85 Apr 20 '18 at 12:28
  • @Dormilich If I run the script from the terminal it asks for the password how do I achieve it using php ? – Casual Coder Apr 20 '18 at 12:28
  • @chris85 Yes, It has the permission. – Casual Coder Apr 20 '18 at 12:29
  • What does the shell script do? If you sudo over to the apache user can you execute the script, without doing anything outside of `sh rst.sh`? – chris85 Apr 20 '18 at 12:32
  • @chris85 my shell script has: #!/bin/bash /etc/init.d/mysql restart – Casual Coder Apr 20 '18 at 12:34
  • You are sure your apache user has permissions to execute `/etc/init.d/mysql restart`? What do your error logs show? – chris85 Apr 20 '18 at 12:38
  • @chris85 it is giving me the following error: Failed to restart mysql.service: Interactive authentication required. See system logs and 'systemctl status mysql.service' for details – Casual Coder Apr 20 '18 at 12:43
  • I've never seen `Interactive authentication required` but from an Ubuntu thread that is a request for root permissions. So it sounds like your apache user doesn't have permission, which also is a good idea not to have. – chris85 Apr 20 '18 at 12:46
  • @chris85 how am I gonna do this ? – Casual Coder Apr 20 '18 at 12:49
  • 2
    Why do you need to restart the service from the browser? Maybe just have a CRON do it? Restarting the service multiple times though sounds like you are just masking the real issue. This question would probably be better on ubuntu or serverfault since it is about server security and permissions, not coding. – chris85 Apr 20 '18 at 12:59
  • I know that you can do input with `proc_*()` but never have used it at this low lever (i.e. only used libraries that use it). – Dormilich Apr 20 '18 at 13:24

3 Answers3

2

Granting the user that runs PHP/web server permissions to restart any service is generally a bed idea from security perspective not even taking about permission to manage all system daemons or full sudo.


Once you have been warned, you have to make sure the user that is running the PHP script has the permission to run the restart command. As already stated, the issue has likely little to do with PHP. There are several ways from you to proceed here:

  • You can grant the PHP user the sudo permission to run the desired command and nothing else (how to do that is covered elsewhere) and invoke the command directly from PHP: sudo systemctl restart mysql.

  • Keep the rst.sh file, get it owned by root, writeable by noone else but root and set SUID bit on that file. This way you can invoke the script without sudo but the script will be run as root thanks to the SUID bit.

#1 feels safer and simpler.

Oliver Gondža
  • 3,386
  • 4
  • 29
  • 49
0

You could also try with system():

<?php
if ($_GET['run']) {
  # This code will run if ?run=true is set.
  var_dump(system("sh rst.sh"));
  exit;
}
?>

<!-- This link will add ?run=true to your URL, myfilename.php?run=true -->
<a href="?run=true">Click Me!</a>

Though your problem is coming from the script you are running (rst.sh). Check the commands in script, you probably need to a systemctl call with sudo.

0

In php try using this command

exec("/etc/init.d/mysql restart");

or push this command to rst.sh

be ensure have chmod +x on file rst.sh

Makyen
  • 31,849
  • 12
  • 86
  • 121
Kamil Dąbrowski
  • 984
  • 11
  • 17