LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Server (https://www.linuxquestions.org/questions/linux-server-73/)
-   -   PHP script for rebooting the Linux server (https://www.linuxquestions.org/questions/linux-server-73/php-script-for-rebooting-the-linux-server-943800/)

jeethapj 05-07-2012 10:54 PM

PHP script for rebooting the Linux server
 
Hai Everyone,

I am trying to reboot the server using php script.I am building a GUI using php and when I click on reboot I want my server get reboot.I am new into linux.Can anyone help me by giving scripts

Thanks & regards

cbtshare 05-08-2012 12:31 AM

why not try a shell script?

jeethapj 05-08-2012 01:54 AM

Actually I am new into linux..I have no idea about this shell script..There is a GUI which is build using php,I want to add a module to restart the server when I click on a button.

j-ray 05-08-2012 02:01 AM

you can execute any system command in php with "exec" or "passthru" but to restart an http server you usually need root privileges. Do you think it is wise to allow http users to restart the server?

<?
exec("/usr/sbin/apache2ctl restart"); //if you want to restart the http server
exec("reboot"); //if you want to reboot the machine
?>

If the apache user tries to run this command it will result in an "permission denied" error because he does not have root privileges. But you can replace the apache user in httpd.conf. But you can't set "root" as apache user, afaik, for security reasons.

jeethapj 05-08-2012 04:04 AM

Dear j-ray,

Thank for your reply.It is Local area network.We have an exchange here.Only within this network we can access it..After making some changes inorder to get the changes in effect we need to restart the server..I have tried 'service network restart' directly ,it is working.But I need to implement it in my GUI.After making changes when I click on reboot button what function this service network restart is doing that should come into effect.

<?
exec("/usr/sbin/apache2ctl restart"); //if you want to restart the http server
exec("reboot"); //if you want to reboot the machine
?>

is not working.Nothing is happening when I executed this code.no warning or nothing is coming.

I have tried the below code
<?
System('service network restart');
?>

But it is showing
Shutting down interface eth0: [FAILED] Shutting down loopback interface: [FAILED] Bringing up loopback interface: [FAILED] Bringing up interface eth0: [FAILED]

Please help

j-ray 05-08-2012 04:11 AM

You are executing these commands as apache user. (It is running on apache, isn't it?) So you don't have the permissions to restart the system or even the http server. (Still I don't know what you want to do: restart the computer or restart the http server or something completely different)
Maybe you can
1.replace the apache user with a username on your system who is in the sudoers list and
2. arrange that the command can be executed with "sudo $cmd" without asking for a password

scandalist 05-08-2012 05:49 AM

Give the www-data group permission to execute /sbin/shutdown.

http://how-to.wikia.com/wiki/How_to_...puter_in_Linux

em31amit 05-08-2012 05:58 AM

i did it once before for test purpose and for this i gave sudo access with NOPASSWORD to apache user to reboot the server. than include it script.

jeethapj 05-10-2012 02:45 AM

we have a ippbx server(OS-centos).There is a GUI for ippbx.There is an option for changing the ip address from the GUI..After changing we need to restart the network services,then only the changed network address will come into effect.That is what my requirement is..

em31amit 05-10-2012 03:52 AM

give apache user to sudo permission to execute "/etc/init.d/network restart" without password,

than call system(sudo /etc/init.d/network restart) into your php script. after changing ip address from the GUI to restart network service.

jeethapj 05-10-2012 04:24 AM

I am totally new into linux.So how can i give apache user sudo permission to execute "/etc/init.d/network restart" without password.Please help

em31amit 05-10-2012 04:41 AM

steps

1) execute "visudo"
2) at the end of the file write these lines

apache ALL = NOPASSWD: /etc/init.d/network restart

3) here apache means username owned by apache webserver
4) save it using ":wq"

and modify your script. check it

jeethapj 05-18-2012 01:44 AM

I have added "admin ALL = NOPASSWD: /etc/init.d/network restart" these lines at the end of the file..Then I have run my script as system('sudo /etc/init.d/network restart')..But nothing is happening.I have tried by assigning the result to a variable,then I print the result..It is returning the value 1..Please help

em31amit 05-19-2012 02:04 AM

Quote:

Originally Posted by jeethapj (Post 4681486)
I have added "admin ALL = NOPASSWD: /etc/init.d/network restart" these lines at the end of the file..Then I have run my script as system('sudo /etc/init.d/network restart')..But nothing is happening.I have tried by assigning the result to a variable,then I print the result..It is returning the value 1..Please help

are you running webserver with admin user ? technically you're giving a permission to execute this without password as a root to "admin" user.

it should be the webserver user. normally in case of apache webserver, it is apache or httpd.

jeethapj 05-19-2012 05:25 AM

Ok sir.I have tried as "apache ALL = NOPASSWD: /etc/init.d/network restart",
"httpd ALL = NOPASSWD: /etc/init.d/network restart"..

When I checked "whoami" it is giving the result as "asterisk"..Then I tried also
"asterisk ALL = NOPASSWD: /etc/init.d/network restart"..

Then I run the script
<? php

System('sudo /etc/init.d/network restart',$result);
echo "$result";
?>

Then nothing is happening and it is displaying the result 1.
Also when I tried
<? php

System('service network restart',$result);
echo "$result";
?>

It is returning the result as 0 and also displaying


Shutting down interface eth0: [FAILED] Shutting down loopback interface: [FAILED] Bringing up loopback interface: [FAILED] Bringing up interface eth0: [FAILED]


All times are GMT -5. The time now is 12:37 PM.