Php 即使在关闭SSH终端之后,如何保持websocket服务器运行?
因此,我将Ratchet与PHP结合使用,目前已将一个成功的websocket示例上载到我的服务器 它在我转到SSH之后工作,然后手动运行“phpbin/chatserver.php” 我想知道的是,在商业环境中,如何保持聊天服务器运行Php 即使在关闭SSH终端之后,如何保持websocket服务器运行?,php,ssh,ratchet,Php,Ssh,Ratchet,因此,我将Ratchet与PHP结合使用,目前已将一个成功的websocket示例上载到我的服务器 它在我转到SSH之后工作,然后手动运行“phpbin/chatserver.php” 我想知道的是,在商业环境中,如何保持聊天服务器运行 谢谢。在*nix服务器的/etc/rc.d/rc中启动它。这应该在服务器启动时启动PHP脚本 我不知道业界是如何做到这一点的,因为我现在只是一个编程/linux爱好者和学生,但这是我在个人服务器上的路线。制作一个守护程序 如果您使用的是symfony2,则可以使
谢谢。在*nix服务器的
/etc/rc.d/rc
中启动它。这应该在服务器启动时启动PHP脚本
我不知道业界是如何做到这一点的,因为我现在只是一个编程/linux爱好者和学生,但这是我在个人服务器上的路线。制作一个守护程序
如果您使用的是symfony2,则可以使用
如果你使用的是原生PHP,不要害怕,它是你的朋友
// 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守护进程”会给你很多提示。棘轮文档有一个页面。你检查过了吗 旧答案:
在prod服务器上这可能不是一个好主意(这是个人的假设),但您可以使用
屏幕命令打开终端,启动守护程序,然后按Ctrl-a、Ctrl-D,然后您的终端仍然处于活动状态,在后台打开。要重新连接到此终端,请重新连接到服务器,然后键入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
博客帖子:
从棘轮页面:
如果您正在运行Ubuntu,请暂停以下步骤:
按如下方式安装supervisor:
将ratchet Web服务器配置为如下服务程序:
在/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
运行以下命令:
$supervisorctl重读
$supervisorctl更新
- 最后检查您的服务是否正在运行:
$supervisorctl
这些是Ratched Deployment教程中应该添加的所有步骤。。但这种方法可能不是最好的 本教程展示了一种非常酷的方法,可以将WebSocket转换为*nix服务,即使在您关闭SSH连接时也可以保持它。虽然此链接可以回答问题,但最好在此处包含答案的基本部分,并提供链接供参考。如果链接页面发生更改,仅链接的答案可能会无效。@Uchiha答案已更新为包含相关内容,感谢您指出,这不是一个糟糕的答案-可能更像是一个注释,但仍然感谢!:D@mattexx您的服务器启动命令中的//是什么意思?
?我在这里也尝试过这一点:。进程的默认超时为60秒,您可以更改$Process->setTimeout(3600)代码>。除此之外,创建的进程不会与当前终端分离。因此,一旦终端关闭,它就会终止。当在用户请求期间运行此命令时,同样适用
[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