MyBB Community Forums

Full Version: PHP Help
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I want a PHP script to restart a service eg. Apache and to reboot the system.

I've tried several solutions, but they have all failed. Solutions tried:

Creating a bash file with the command in it, chmod 4755, chown root, then running the PHP exec('/path/to/file.sh') command. That didn't work, so i tried using system() instead. That also failed.

Secondly i tried using a cronjob that runs a bash file that checks for a certian file every minute. Then the PHP script creates that file. When the bash script finds the file it restarts the service. But that also doesn't work. - For some reason, php won't open the file to create it...

Anyone know any other solutions?
Well you may want to try:
shell_exec("service httpd restart");

Even though using this kind of functions is always a security risk if you're not doing it correctly.
But the above should restart Apache
as long as the shell_exec function is not disabled on the server. are the other functions you tried enabled on the server? most hosts block such functions in their PHP setups for security.
This is on my own server, but i was under the impression that you had to run that command with root priviledges, which apache doesn't have...?
(2010-10-04, 09:06 PM)magicstuff Wrote: [ -> ]This is on my own server, but i was under the impression that you had to run that command with root priviledges, which apache doesn't have...?

Check this:
http://www.linuxforums.org/forum/securit...pache.html
(2010-10-04, 07:16 PM)magicstuff Wrote: [ -> ]Secondly i tried using a cronjob that runs a bash file that checks for a certian file every minute. Then the PHP script creates that file. When the bash script finds the file it restarts the service. But that also doesn't work. - For some reason, php won't open the file to create it...

IMHO this is the best way to run commands, rather than creating an empty file you could have php generate the file with the commands that you want to run and the bash file would read the file and execute the commands listed there and remove the file when done. This way you wouldn't have to worry about apache's execute permissions as you can set those on the bash script that runs in cron. You will however need to put in proper safeguards because it would then be that much easier for you to get hacked.

Most of the time, a file not getting created by php is caused by permissions, check your logs for why the file is not created.
(2010-10-04, 09:08 PM)Pirata Nervo Wrote: [ -> ]Check this:
http://www.linuxforums.org/forum/securit...pache.html

That looks like it might work, i'll have a look later on when i have access to the server i'm testing this on :p