2

I'm trying to switch between nginx and openresty with php using exec.

I use this to switch to openresty, exec('/bin/systemctl stop nginx ; /usr/local/openresty/nginx/sbin/nginx -p /usr/local/openresty/nginx');

and this exec('/usr/local/openresty/nginx/sbin/nginx -s stop ; /bin/systemctl start nginx'); to switch to nginx.

Both work from ssh, however, from php I am unable to start nginx. This /bin/systemctl start nginx does not appear to execute. I tried executing the code from .sh files instead but that didn't work.

Flux
  • 9,805
  • 5
  • 46
  • 92
Dan Bray
  • 7,242
  • 3
  • 52
  • 70
  • 1
    Hi, first of all starting this services will require privileges, especially that they need privileged port (that can be open only by root). Second - can say which distro are you using? There might be multiple problems like apparmor or SELinux. Is it possible to get your php code that change service? Is service changed put into background and detached from session? – Alex Baranowski Jan 28 '19 at 10:06
  • @AlexBaranowski I am using `Centos 7` and `php` is running with root permission. – Dan Bray Jan 28 '19 at 11:00
  • Can you try following trick. `exec('/usr/bin/nohup $(sleep 2; /usr/local/openresty/nginx/sbin/nginx -s stop ; /bin/systemctl start nginx) &');` . This should make subshell that detach itself from nginx. – Alex Baranowski Jan 28 '19 at 13:40
  • I think this is similar to [How to start and stop PHP dev server with exec()](https://stackoverflow.com/questions/13136275/how-to-start-and-stop-php-dev-server-with-exec). – Mohit Rathore Jan 28 '19 at 13:56

2 Answers2

0

try using

exec('/usr/local/openresty/nginx/sbin/nginx -s stop && /bin/systemctl start nginx');
Yasii
  • 1,702
  • 1
  • 10
  • 15
0

I got it working using this to switch to openresty:

shell_exec('/bin/systemctl stop nginx; /usr/local/openresty/nginx/sbin/nginx -p /usr/local/openresty/nginx');

and this to switch to nginx:

shell_exec('/usr/local/openresty/nginx/sbin/nginx -s stop; /bin/systemctl restart nginx');
Dan Bray
  • 7,242
  • 3
  • 52
  • 70