简体   繁体   English

如何在后台运行一个 php 文件(websocket 服务器)到 web 服务器(www.example.com)

[英]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.

相关问题 $ _SERVER [“ SERVER_NAME”]未显示www.example.com - $_SERVER[“SERVER_NAME”] not showing www.example.com 通过www.example.com/page而不是www.example.com/page.php访问URL - Accessing URLS by www.example.com/page instead of www.example.com/page.php 我可以从http://www.example.com/example.php下载.php文件吗 - Can I download .php file from http://www.example.com/example.php XAMPP 错误:www.example.com:443:0 服务器证书不包含与服务器名称匹配的 ID - XAMPP Error: www.example.com:443:0 server certificate does NOT include an ID which matches the server name Ubuntu-如何直接将example.com/opencart/index.php定向到http://example.com/和www.example.com? - Ubuntu - How direct example.com/opencart/index.php to http://example.com/ and www.example.com? PHP $ _GET www.example.com/folder/file?id=2类型网址存在问题 - PHP $_GET problems with www.example.com/folder/file?id=2 type URLs 如何将我的URL像www.example.com/profile.php?id=xyz转换为www.example.com/xyz? - How do i convert my URL's like www.example.com/profile.php?id=xyz into www.example.com/xyz? URL重写从www.example.com/blog/blog.php到www.example.com/blog/ - URL rewriting going from www.example.com/blog/blog.php to just www.example.com/blog/ 如何将网址从www.example.com/index.php?store=test重写为.htacess中的test.example.com或www.test.example.com - How to rewrite the url from www.example.com/index.php?store=test to test.example.com or www.test.example.com in .htacess 如何创建网址,例如www.example.com/EN/login - How to create URLs like www.example.com/EN/login
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM