February 18th, 2013, 10:12 PM
PHP exec java command as root
I am running php on CentOS, I need to execute a java command which will start a server, this command needs to be executed as a root user. PHP is running as apache user. The java command is like "java -Xmx256M -jar boot.jar --server 1985 > /dev/null 2>&1 &".
I been trying to research for the last 2 days, and am not that good with linux, I did read about sudoer, i tried to add an entry in the etc/sudoers "apache ALL=(ALL) ALL" . It doesnt work. Please somebody help me out. Thank you
February 19th, 2013, 02:17 AM
Once you've added the appropriate settings to /etc/sudoers you will be able to run it as root. You need to run it via the 'sudo' executable though, as in:
What I would do is wrap that command into a bash script and save it somewhere, say /root/bin/startServer.sh. Then you can easily add a specific entry to /etc/sudoers allowing apache to run only that script file. Then in PHP all you'd need is:
exec("sudo java -Xmx256M -jar boot.jar --server 1985 > /dev/null 2>&1 &");
Last edited by kicken; February 19th, 2013 at 02:19 AM.
Recycle your old CD's, don't just trash them
If I helped you out, show some love with some reputation, or tip with Bitcoins to 1N645HfYf63UbcvxajLKiSKpYHAq2Zxud