[英]How to run a php file(websocket server) in background into web server(www.example.com)
I have downloaded a websocket example and tested with xamp server locally and it is working fine.我已经下载了一个 websocket 示例并在本地使用 xamp 服务器进行了测试,它工作正常。 It has a php file called sever.php which i have to run it from the command prompt first to run the server.它有一个名为 sever.php 的 php 文件,我必须先从命令提示符运行它才能运行服务器。 After that i can able to connect to the websocket server and can send message.之后,我可以连接到 websocket 服务器并可以发送消息。 Everything is running fine with my laptop.我的笔记本电脑一切正常。 Now I have uploaded the same project to my web server (lets say www.example.com).现在我已经将同一个项目上传到我的网络服务器(比如 www.example.com)。 Since without running the server.php the client can not able to connect to the server, so how can i run the server.php in background into web server.由于不运行 server.php 客户端无法连接到服务器,所以我如何在后台运行 server.php 到 Web 服务器。 After search in the forum i found a function which can run php file in background as below.在论坛中搜索后,我发现了一个可以在后台运行 php 文件的功能,如下所示。
$cmd='nohup php php-socket.php >/dev/null 2>&1 & echo $!';
function execInBackground($cmd){
if (substr(php_uname(), 0, 7) == "Windows"){
pclose(popen("start /B ". $cmd, "r"));
}
else {
exec($cmd);
//echo 'command executed';
} }
From the index file i called this function to running the server but not work.从索引文件我调用这个函数来运行服务器但不起作用。 Is it this function able to run?这个功能可以运行吗? If so from where i should call this function?如果是这样,我应该从哪里调用这个函数?
In the console i am getting the following error.在控制台中,我收到以下错误。
WebSocket connection to 'ws://localhost:8090/dmp/chat_websocket/php-socket.php' failed: Error in connection establishment: net::ERR_CONNECTION_REFUSED (anonymous) @ (index):25 WebSocket 连接到“ws://localhost:8090/dmp/chat_websocket/php-socket.php”失败:连接建立错误:net::ERR_CONNECTION_REFUSED(匿名)@(索引):25
Highly appreciated if someone help me in this regards.如果有人在这方面帮助我,非常感谢。
After 2 years, 8 months no one have added a solution. 2 年 8 个月后没有人添加任何解决方案。
I have no solution for you too :).我也没有给你解决方案:)。 But I thing you need access to the command Line from your server and for sure is not the best thing your provider will give you access to it.但是我认为您需要从您的服务器访问命令行,并且肯定不是您的提供商可以让您访问它的最佳方式。
You miss something in your example is to call the function execInBackground($cmd)您在示例中错过了调用函数execInBackground($cmd)
$cmd='nohup PHP php-socket.php >/dev/null 2>&1 & echo $!';
function execInBackground($cmd){
if (substr(php_uname(), 0, 7) == "Windows"){
pclose(popen("start /B ". $cmd, "r"));
}
else {
exec($cmd);
//echo 'command executed';
}
}
Add this at the end:在最后添加这个:
execInBackground($cmd);
edit: I make some test.编辑:我做了一些测试。 I added this in the top of the page (example server.php)我在页面顶部添加了这个(示例 server.php)
/* Remove the execution time limit */
ini_set("max_execution_time", "0");
ini_set("max_input_time", "0");
set_time_limit(0);
Make sure you have something in place to stop it.确保你有什么东西可以阻止它。
After just call your script in the browser ( www.example.com/server.php ) it will run forever just close the tab.在浏览器( www.example.com/server.php )中调用您的脚本后,它将永远运行,只需关闭选项卡。 Your websocket script should not stop running.您的 websocket 脚本不应停止运行。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.