I am running a LEMP stack and wish to write a simple control panel for it.
So, I want to be able to restart php-fpm
from a php script. To achieve this, this is what I did.
Created a binary wrapper in c
like this php-shell.c:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_CMN_LEN 100
int main(int argc, char *argv[])
{
char cmd[MAX_CMN_LEN] = "", **p;
if (argc < 2)
{
fprintf(stderr, "Usage: ./php_shell terminal_command ...");
exit(EXIT_FAILURE);
}
else
{
strcat(cmd, argv[1]);
for (p = &argv[2]; *p; p++)
{
strcat(cmd, " ");
strcat(cmd, *p);
}
system(cmd);
}
return 0;
}
This program was compiled like this:
gcc php_shell.c -o php_shell
I have then added nginx user to sudo visudo
like this:
Defaults:nginx !requiretty
nginx ALL=(ALL) NOPASSWD:/path/to/php_shell
Then I executed the command in a php script like this:
var_dump(shell_exec('sudo /path/to/php_shell "service nginx restart" 2>&1'));
As soon as I run this script php script, I get 502 Gateway Error
and it appears all php-fpm
processes has been killed off and it does not start back up.
Any ideas? Am I doing this wrong? I want to be able to restart nginx server from php script by executing service nginx restart
. How can I achieve this?