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 |
why not try a shell script?
|
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.
|
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. |
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 |
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 |
Give the www-data group permission to execute /sbin/shutdown.
http://how-to.wikia.com/wiki/How_to_...puter_in_Linux |
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.
|
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..
|
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. |
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
|
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 |
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
|
Quote:
it should be the webserver user. normally in case of apache webserver, it is apache or httpd. |
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. |