bash - what is the best way to run shell script (with root privilege) via php?

751

I've made a simple bash script for server admininstration and I cannot figure how can I run it in safely inside a php page: I'd like to create a php admininstration page but I obviously don't want to hard-code root password anyware. Let's make an example (this is a foo script, of course)

#!/bin/bash
touch /$1

this simple/stupid script will not work if the user who run it as no writing permission on /.
Actually the script add apache virtualhosts, ftp users and so on... any ideas?
thanks

342

Answer

Solution:

Use

sudo /path/to/executable/file

and set upsudo so it can execute the following command for the current user as a root.

http://www.sudo.ws/sudo/sudoers.man.html - here is the sudoers manual, the configuration file, that you have to modify.

zerkms ALL = (ALL) NOPASSWD: /sbin/iptables -L FORWARD -n -v -x

This is example from my/etc/sudoers. Here I allowed to run command/sbin/iptables -L FORWARD -n -v -x asroot without asking a password for userzerkms.

People are also looking for solutions to the problem: php - Select Data from multiple databases?

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.