如何使用PHP重新启动或重新加载nginx [英] how restart or reload nginx using php

查看:96
本文介绍了如何使用PHP重新启动或重新加载nginx的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用php更改了nginx conf文件 接下来,我想重新启动Nginx. 但无法重新启动Nginx

i change nginx conf file with php next i want restart nginx. but can not restart nginx

<?php
    if(exec("service nginx restart")) {
        echo "ok shd";
    } else {
        echo "error";
    }
?>
<hr>
<?php
    if(exec("/etc/init.d/nginx reload")) {
        echo "ok shd";
    } else {
        echo "error";
    }
?>
<hr>
<?php
    if(exec("/usr/sbin/nginx -s reload")) {
        echo "ok shd";
    } else {
        echo "error";
    }
?>

输出为:

error<hr>
error<hr>
error

要执行exec("service nginx restart"),我需要以root用户身份访问php-fpm,所以我转到/usr/local/etc/php-fpm.conf

To do exec("service nginx restart") I need acces to php-fpm as root, so i go to /usr/local/etc/php-fpm.conf

user = nobody
group = nobody

并将其更改为:

user = root
group = root

,然后输入service php-fpm restart

输出:

Starting php-fpm [11-Feb-2016 05:14:33] ERROR: [pool www] please specify user and group other than root
[11-Feb-2016 05:14:33] ERROR: FPM initialization failed
 failed

这意味着它不能作为root用户运行.

and this means it can't be run as root.

那么如何使用php重新启动或重新加载nginx?

So how can I restart or reload nginx using php?

推荐答案

我需要以root用户身份访问php-fpm

I need acces to php-fpm as root

OMG否

有一个很好的理由说明PHP-FPM不能以root身份运行-因为这确实是确实要做的一件坏事.

Theres's a very good reason PHP-FPM won't run as root - because its a really, really bad thing to do.

如果您确实必须通过网络服务器公开此功能,请通过 sudo 进行.同样,对您的系统配置的任何更改都应通过sudo进行.

If you really must expose this functionality via a webserver then do it via sudo. Similarly any changes to your system config should be via sudo.

顺便说一句:脚本中缺少的一个至关重要的事情是ignore_user_abort().当(如果)nginx由PHP重新启动时,与浏览器的连接将丢失.默认情况下,PHP将在此时终止.

BTW: one crucially important thing missing from your script is ignore_user_abort(). When (if) nginx is restarted by PHP the connection to the browser will be lost. By default, PHP will terminate at this point.

这篇关于如何使用PHP重新启动或重新加载nginx的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆