PHP Socket Server Starts as a Service

I created a PHP socket server and started it in localhost (127.0.0.1) via the command line. i.e

C: \ xampp \ php \ php server.php

this works fine in localhost.but my problem is that I have to run this server.php file on a domain server. I copied the server.php file to the web host directory (http://example.com/server.php) and changed the ip address to the IP address of the domain in the php file. Now the problem is that I am running the file in a browser, for example

http://www.example.com/server.php

the socket server starts normally and within 5 minutes it automatically stops by the browser (error 500). how can i run this server.php file via command line. need help.

thanks.

+4
source share
2 answers

You cannot start the server by calling it through HTTP. Assuming you are using Apache, this happens: the Apache workflow takes your request and runs the underlying PHP script. Then, depending on your php.ini settings, the script exits after a certain time ( max_execution_time ).

For this to work the way you expected it to work, the PHP script will need to unlock itself and start a new session (via posix_setsid() ). This will require that worker threads run as root, although this is an absolute security issue.

If you have access to the server (via SSH), you can start the server in the same way as locally, except that - if you do not have root privileges - the PHP script cannot listen to privileged ports (ports <= 1024).

+1
source

What you want to do is run the php script as a server, and when you access it using your web browser, the server is called an http server (apache or something else).

Then the http server calls the php module, which runs your script. Since the request has a maximum execution time, you cannot make it work as a server!

What can you do, given that you have enough rights on your server, you need to create a php script that will start the server using server-side execution.

0
source

Source: https://habr.com/ru/post/1386089/


All Articles