I want to work on a custom linux hosting control panel based on my experience in managing LEMP/LAMP stack. This is just a pet project. I believe I have ran into some hurdle.

The CP is being written in PHP and server by NginX as default server on port 8000. I am not using any framework, just vanilla PHP.

In putty, logged in as root, I can get the system uuid like this:

[root@localhost ~]# dmidecode -s system-uuid
997C4DE8-213B-4ACC-8E23-01E79D6CC12F

When I try this in PHP with the following script:

var_dump(shell_exec('dmidecode -s system-uuid 2>&1'));

I get the following:

sh: dmidecode: command not found

How can I execute that above command get the output in PHP?

Nginx/PHP-FPM is running as user nginx. Do i need to add nginx user to root group?

I want to be able to execute very specific commands like: /etc/init.d/php-fpm restart from my control panel (to restart php-fpm gateway).

How can I achieve this? What are my options? How does control panel like cPanel, DirectAdmin, etc... do it?


I have tried the following method also. Installed sudo and used the sudo visudo command and added the following lines at the end:

nginx    ALL=(ALL)    NOPASSWD:/path/to/php_shell.sh
Defaults:nginx        !requiretty

and the content of php_shell.sh is:

#!/bin/bash
dmidecode -s system-uuid

Now, I tried to execute it like this:

var_dump(shell_exec('sudo sh /path/to/php_shell.sh 2>&1'));

I get: sudo: no tty present and no askpass program specified

share|improve this question
    
The order of rules in /etc/sudoers is significant. i.e. you probably need to move that !requiretty above the nginx line. – cas Oct 25 '15 at 20:16

dmidecode is usually in directory /usr/sbin and this is not always in the PATH, so from php use the full path, found with type -p dmidecode, eg /usr/sbin/dmidecode.

As to your sudo, you did sudo sh ... instead of sudo /path/to/php_shell.sh so your NOPASSWD entry did not match. However, it would be better to list explicitly in your sudo file the command and its arguments, rather than allowing a general purpose script to run as root. So use eg

Defaults:nginx        !requiretty
nginx ALL NOPASSWD:/usr/sbin/dmidecode -s system-uuid

If you have several commands you need to run as root, put them on the same line separated by a comma.

share|improve this answer

Your Answer

 
discard

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.