我如何保持 websocket 服务器运行,即使在关闭 SSH 终端后也是如此?

问题描述 投票:11回答:5

所以,我使用Ratchet与PHP,目前已经上传了一个成功的websocket例子到我的服务器。

在我进入SSH后,它可以工作,然后只是手动运行 "php binchat-server.php"。

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

谢谢。

php ssh ratchet
5个回答
7
投票

making a conf file in 我当时...制作一个守护进程。如果你使用的是symfony2,你可以使用的是

// 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";
}

流程组件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,不用怕。

是你的朋友

1
投票

/etc/init/socket.conf本教程展示了一个很酷的方法,将WebSocket变成一个*nix服务,使其在关闭SSH连接时也能持久存在。

# 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  

基本上,您可以制作一个文件 内容如下

博客文章。

0
投票

/etc/rc.d/rc

启动它

对于*nix服务器来说,它应该在服务器启动时启动PHP脚本。每当服务器启动时,这应该会启动你的PHP脚本。

0
投票

棘轮文档中有一个

调配screen 页面。你检查过了吗?旧答案:在prod服务器上这可能是个坏主意(这是个人假设),但你可以使用 命令打开一个终端,启动你的守护进程,然后按Ctrl-A,Ctrl-D,你的终端仍然是 screen -r活着

,在后台打开。要重新连接到这个终端,连接回你的服务器,并输入

0
投票

来自棘轮公司

部署

  1. 页面。如果你使用的是Ubuntu,请按照以下步骤进行安装。像这样安装supervisor

  2. 安装supervisor 将Ratchet webserver配置为服务程序,如下所示。

Supervisor进程控制 etcsupervisorconf.d.conf 并填写Ratchet部署页面的conf exmple代码。

[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部署教程中应该添加的步骤... 但这种方法可能不是最好的... ...

© www.soinside.com 2019 - 2024. All rights reserved.