【问题标题】:How do I keep a websocket server running, even after I close the SSH terminal?即使关闭 SSH 终端,如何保持 websocket 服务器运行?
【发布时间】:2013-10-29 12:58:14
【问题描述】:

所以,我正在使用带有 PHP 的 Ratchet,并且目前已将成功的 websocket 示例上传到我的服务器。

在我进入 SSH 之后,它就可以工作了,然后手动运行“php bin/chat-server.php”。

我想知道的是,在商业情况下,我如何保持聊天服务器运行?

谢谢。

【问题讨论】:

  • 本教程展示了一种将 WebSocket 转换为 *nix 服务的非常酷的方法,即使您关闭了 SSH 连接,它也能持续存在。 blog.samuelattard.com/…

标签: php ssh ratchet


【解决方案1】:

制作一个守护进程。

如果您使用的是 symfony2,则可以使用Process Component

// in your server start command
$process = new Process('/usr/bin/php bin/chat-server.php');
$process->start();
sleep(1);
if ($process->isRunning()) {
    echo "Server started.\n";
} else {
    echo $process->getErrorOutput();
}

// in your server stop command
$process = new Process('ps ax | grep bin/chat-server.php');
$process->run();
$output = $process->getOutput();
$lines = preg_split('/\n/', $output);
// kill everything (there can be multiple processes if they are spawned)
$stopped = False;
foreach ($lines as $line) {
    $ar = preg_split('/\s+/', trim($line));
    if (in_array('/usr/bin/php', $ar)
        and in_array('bin/chat-server.php', $ar)) {
        $pid = (int) $ar[0];
        posix_kill($pid, SIGKILL);
        $stopped = True;
    }
}
if ($stopped) {
    echo "Server stopped.\n";
} else {
    echo "Server not found. Are you sure it's running?\n";
}

如果您使用的是原生 PHP,请不要害怕,popen 是您的朋友!

// in your server start command
_ = popen('/usr/bin/php bin/chat-server.php', 'r');
echo "Server started.\n";

// in your server stop command
$output = array();
exec('ps ax | grep bin/chat-server.php', &$output);
$lines = preg_split('/\n/', $output);
// kill everything (there can be multiple processes if they are spawned)
$stopped = False;
foreach ($lines as $line) {
    $ar = preg_split('/\s+/', trim($line));
    if (in_array('/usr/bin/php', $ar)
        and in_array('bin/chat-server.php', $ar)) {
        $pid = (int) $ar[0];
        posix_kill($pid, SIGKILL);
        $stopped = True;
    }
}
if ($stopped) {
    echo "Server stopped.\n";
} else {
    echo "Server not found. Are you sure it's running?\n";
}

当然还有其他有用的 PHP 库可用于处理守护进程。谷歌搜索“php daemon”会给你很多提示。

【讨论】:

  • @mattexx // in your server start command 是什么意思??
  • 我也在这里尝试过:stackoverflow.com/questions/53489734/…。进程的默认超时时间为 60 秒,您可以更改 $process->setTimeout(3600);。除此之外,创建的进程不会与当前终端分离。所以一旦终端关闭,它往往会被终止。在用户请求期间运行它时同样适用
【解决方案2】:

棘轮文档有一个deploy 页面。你检查了吗?

旧答案: 在 prod 服务器上这可能是个坏主意(这是个人假设),但是您可以使用 screen 命令打开终端,启动您的守护程序,然后按 Ctrl-A、Ctrl-D,您的终端是仍然活着,在后台打开。要重新连接到此终端,请连接回您的服务器并输入screen -r

【讨论】:

    【解决方案3】:

    本教程展示了一种将 WebSocket 转换为 *nix 服务的非常酷的方法,即使您关闭了 SSH 连接,它也能持续存在。

    基本上你制作一个文件/etc/init/socket.conf,内容如下

    # Info
    description "Runs the Web Socket"  
    author      "Your Name Here"
    
    # Events
    start on startup  
    stop on shutdown
    
    # Automatically respawn
    respawn  
    respawn limit 20 5
    
    # Run the script!
    # Note, in this example, if your PHP script (the socket) returns
    # the string "ERROR", the daemon will stop itself.
    script  
        [ $(exec /usr/bin/php -f /path/to/socket.php) = 'ERROR' ] && ( stop; exit 1; )
    end script  
    

    博文:
    http://blog.samuelattard.com/the-tutorial-for-php-websockets-that-i-wish-had-existed/

    【讨论】:

    • 虽然此链接可能会回答问题,但最好在此处包含答案的基本部分并提供链接以供参考。如果链接页面发生更改,仅链接的答案可能会失效。
    • @Uchiha 答案已更新,包含相关内容,感谢您指出
    • 不错的答案 - 可能更多的是评论,但仍然感谢! :D
    【解决方案4】:

    来自 Ratchet Deployment 页面:

    如果您运行的是 Ubuntu,请遵循以下步骤:

    1. 像这样安装主管: Installing Supervisor

    2. 将棘轮网络服务器配置为服务程序,如下所示: Supervisor Process Control | Supervisord Install & Usage

    在 /etc/supervisor/conf.d/.conf 中创建一个 conf 文件,并从 Ratchet 部署页面填写 conf 示例代码:

    [program:ratchet]
    command                 = bash -c "ulimit -n 10000; exec /usr/bin/php /absolute/path/to/ratchet-server-file.php)"
    process_name            = Ratchet
    numprocs                = 1
    autostart               = true
    autorestart             = true
    user                    = root
    stdout_logfile          = ./logs/info.log
    stdout_logfile_maxbytes = 1MB
    stderr_logfile          = ./logs/error.log
    stderr_logfile_maxbytes = 1MB
    
    1. 运行以下命令:

      • $ supervisorctl reread
      • $ supervisorctl update
      • 最后检查你的服务是否正在运行:$ supervisorctl

    这些都是 Ratched Deployment 教程中应该添加的所有步骤.. 但是这种方法可能不是最好的..

    【讨论】:

      【解决方案5】:

      对于 *nix 服务器,在 /etc/rc.d/rc 中启动它。这应该会在服务器启动时启动您的 PHP 脚本。

      我实际上并不知道这个行业是如何做到的,因为我现在只是一个编程/Linux 爱好者和学生,但这是我在个人服务器上的路线。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-05-17
        • 2014-09-24
        • 1970-01-01
        • 2020-12-25
        • 2012-05-26
        • 2013-05-01
        • 2018-11-25
        • 1970-01-01
        相关资源
        最近更新 更多