因此,我正在使用Ratchet和PHP,并且目前已经向我的服务器上传了一个成功的websocket示例。即使在我关闭SSH终端之后,如何让websocket服务器仍然运行?
它在我进入SSH后运行,然后手动运行“php bin/chat-server.php”。
我想知道的是,在商业环境中,我该如何保持聊天服务器正常运行?
谢谢。
因此,我正在使用Ratchet和PHP,并且目前已经向我的服务器上传了一个成功的websocket示例。即使在我关闭SSH终端之后,如何让websocket服务器仍然运行?
它在我进入SSH后运行,然后手动运行“php bin/chat-server.php”。
我想知道的是,在商业环境中,我该如何保持聊天服务器正常运行?
谢谢。
做一个守护进程。
如果您使用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守护进程”会给你很多指针。
对于* nix服务器,从/etc/rc.d/rc
开始。这应该在服务器启动时启动您的PHP脚本。
我实际上并不知道这个行业是怎么做的,因为我现在只是一个编程/ Linux爱好者和学生,但这就是我将在个人服务器上使用的路线。
棘轮文档有一个deploy页面。你检查过了吗?
老答案: 它可能是一个督促服务器(这是个人的假设)在一个糟糕的主意,但是你可以使用screen
命令打开一个终端,启动你的守护进程,然后按下Ctrl-A,按Ctrl - D,并且你的终端仍然是alive,在后台打开。要重新连接到此终端,请连接到您的服务器并键入screen -r
。
本教程展示了一种非常酷的将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答案已经更新,包括相关内容,谢谢指出, – MarshallOfSound
不是一个错误的答案 - 可能是更多的评论,但谢谢! :d – think123
本教程介绍了打开的WebSocket成* nix的服务的一个非常酷的方式,使之持续即使你关闭你的SSH连接。 http://blog.samuelattard.com/the-tutorial-for-php-websockets-that-i-wish-had-existed/ – MarshallOfSound