所以,我使用Ratchet与PHP,目前已经上传了一个成功的websocket例子到我的服务器。
在我进入SSH后,它可以工作,然后只是手动运行 "php binchat-server.php"。
我想知道的是,在商业情况下,我如何保持聊天服务器的运行?
谢谢。
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,不用怕。
是你的朋友
/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
基本上,您可以制作一个文件 内容如下
博客文章。 /etc/rc.d/rc
启动它
对于*nix服务器来说,它应该在服务器启动时启动PHP脚本。每当服务器启动时,这应该会启动你的PHP脚本。部署
页面。如果你使用的是Ubuntu,请按照以下步骤进行安装。像这样安装supervisor
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
运行以下命令:
$ supervisorctl reread
$ supervisorctl update
$ supervisorctl
这些都是Ratched部署教程中应该添加的步骤... 但这种方法可能不是最好的... ...