Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I currently running a websever with Nginx+PHP-Fpm and each sites on its own username/GID. Then I have a bash script that I use to create new user along with other necessary stuffs. I know I can call a script using shell_exec/suexec but I am wondering what is the safest way to call a bash script without jeopardizing the security of the server?

At first I was thinking of setting a cron job to run the script but the account need to be up instantly. So cron not gonna work.

Also I've done some searching and some suggest to use php daemon to monitor a queue then run the the script when there's a job. Not really sure about this (daemon) since I never have any experience on it.

How I can run my script safely?

Just to update that I've decided to go with Beanstalkd + pheanstalk combination.

Php script will send the job to queue and it then will be run by a daemonized php script run by a seperate user that has privileges needed.

share|improve this question
Your english is better than some "native" english speakers also on this site. – Tom O'Connor Feb 15 '12 at 15:00

On the specific topic of using exec in php and how to be safe, The only thing you need to take care of is to escape arguments.

i.e. don't do this:

exec('myscript ' . $_POST['arg']);

if it's not obvious - just think about what happens if $_POST['arg'] contains ; rm xyz - or worse, if you are sending the output of that command to the screen and it contains ; more /etc/passwd; more db-config.php etc.

therefore - escape your arguments:

$foo = escapeshellarg($whatever);
exec("myscript $foo");

However, if your bash script requires more privileges than you would ordinarily permit your nginx user to have - You are better of heeding the advice you have found which disassociates the user running your bash script from the nginx user.

Similar to your php daemon suggestion in the question, the php app sends a request for the bash script to run, synchronously or asynchronously, using a job queue system. The job queue just sends the request to your bash script, optionally returning the result. In this way you can start the "create new user" bash script as a user with appropriate permissions, without giving your nginx user any extra privileges and risking these permissions being exploited somewhere else if your php application code. Gearman in particular, is very easy to setup.

share|improve this answer
Thank you for the suggestions. I think I will approach the Gearman method for now. – Sam W. Feb 14 '12 at 2:15

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.