LinuxQuestions.org
View the Most Wanted LQ Wiki articles.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Server
User Name
Password
Linux - Server This forum is for the discussion of Linux Software used in a server related context.

Notices



Reply
 
Search this Thread
Old 05-07-2012, 11:54 PM   #1
jeethapj
LQ Newbie
 
Registered: May 2012
Posts: 17

Rep: Reputation: Disabled
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
 
Old 05-08-2012, 01:31 AM   #2
cbtshare
Member
 
Registered: Jul 2009
Posts: 569

Rep: Reputation: 42
why not try a shell script?
 
Old 05-08-2012, 02:54 AM   #3
jeethapj
LQ Newbie
 
Registered: May 2012
Posts: 17

Original Poster
Rep: Reputation: Disabled
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.
 
Old 05-08-2012, 03:01 AM   #4
j-ray
Senior Member
 
Registered: Jan 2002
Location: germany
Distribution: ubuntu
Posts: 1,456

Rep: Reputation: 104Reputation: 104
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.

Last edited by j-ray; 05-08-2012 at 03:08 AM.
 
Old 05-08-2012, 05:04 AM   #5
jeethapj
LQ Newbie
 
Registered: May 2012
Posts: 17

Original Poster
Rep: Reputation: Disabled
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
 
Old 05-08-2012, 05:11 AM   #6
j-ray
Senior Member
 
Registered: Jan 2002
Location: germany
Distribution: ubuntu
Posts: 1,456

Rep: Reputation: 104Reputation: 104
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
 
Old 05-08-2012, 06:49 AM   #7
scandalist
LQ Newbie
 
Registered: Apr 2011
Posts: 27

Rep: Reputation: 1
Give the www-data group permission to execute /sbin/shutdown.

http://how-to.wikia.com/wiki/How_to_...puter_in_Linux
 
Old 05-08-2012, 06:58 AM   #8
em31amit
Member
 
Registered: Apr 2012
Location: /root
Distribution: Ubuntu, Redhat, Fedora, CentOS
Posts: 190

Rep: Reputation: 55
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.
 
Old 05-10-2012, 03:45 AM   #9
jeethapj
LQ Newbie
 
Registered: May 2012
Posts: 17

Original Poster
Rep: Reputation: Disabled
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..
 
Old 05-10-2012, 04:52 AM   #10
em31amit
Member
 
Registered: Apr 2012
Location: /root
Distribution: Ubuntu, Redhat, Fedora, CentOS
Posts: 190

Rep: Reputation: 55
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.
 
Old 05-10-2012, 05:24 AM   #11
jeethapj
LQ Newbie
 
Registered: May 2012
Posts: 17

Original Poster
Rep: Reputation: Disabled
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
 
Old 05-10-2012, 05:41 AM   #12
em31amit
Member
 
Registered: Apr 2012
Location: /root
Distribution: Ubuntu, Redhat, Fedora, CentOS
Posts: 190

Rep: Reputation: 55
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
 
Old 05-18-2012, 02:44 AM   #13
jeethapj
LQ Newbie
 
Registered: May 2012
Posts: 17

Original Poster
Rep: Reputation: Disabled
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
 
Old 05-19-2012, 03:04 AM   #14
em31amit
Member
 
Registered: Apr 2012
Location: /root
Distribution: Ubuntu, Redhat, Fedora, CentOS
Posts: 190

Rep: Reputation: 55
Quote:
Originally Posted by jeethapj View Post
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.
 
Old 05-19-2012, 06:25 AM   #15
jeethapj
LQ Newbie
 
Registered: May 2012
Posts: 17

Original Poster
Rep: Reputation: Disabled
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]
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Can't run PHP script on Linux virtual server shanekelly Linux - Newbie 9 06-10-2011 10:36 AM
SAN errors while rebooting LINUX server sjarade Linux - Newbie 2 06-26-2009 12:09 PM
PHP Login script working on windows server but not on Linux Red Hat Server ahmedyk Linux - Newbie 2 01-14-2009 01:22 AM
Red Hat Linux 8.0 on IBM server is rebooting hemanth_13 Linux - General 5 12-16-2003 11:15 AM


All times are GMT -5. The time now is 09:20 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration