I've written a web socket server that listens to a specific port. In order to run it I log in to EC2 instance with putty and run:
php server.php
I was wondering if this is the only and the right way to do. Normally copy my php files to the host via ftp would be enough, I don't understand why the php command needs to run the server.
Any help is appreciated.
This question is not about any particular coding problem, so is considered off-topic in terms of StackOverflow.
The way PHP works - is just a script file. Same as bash (.sh), python (.py), node.js (.js) or any other similar.
They all in fact have to be executed. In common world, Apache, nginx or any other web server will do execute those scripts for you for each request is made to web server.
As you are creating socket file, you need to create it yourself, as it creates one socket and php script will continue working as long as it will by it self. It is not executed per each request. In fact make sure it is not executed by apache so do not put in usual website directory.
Related
Is there a way for PHP to send a signal to a Windows Server to run a script?
I am writing a web application which generates database entries that are later synced by a program on a remote Windows Server. PHP & mySQL are running on AWS.
The application on the windows server syncs the mySql database with Quickbooks. Ideally I would like to send a signal from PHP so that if PHP has updated the database, letting the remote Windows server know which script to run to in order to initiate the sync program. I would need to be pointed in the right direction both on the PHP commands as well as how to setup the listening service on Windows.
I was thinking if I could get Windows to listen on a specific port for a short XML file, PHP could send a password and entity ID number to identify which script to run.
Any suggestions?
Thanks!
You can use php on both end. Just install php server on windows, then when request happened execute a windows command. Php can do that.
I would schedule a task to read a specific file from the windows server side and from the php side you could use cron jobs. This question is too broad though and there are several approaches to that. You need to be more "code" specific.
I am new to web-sockets, What I wanted to achieve is run some bash script on my linux pc that is behind the NAT, I have a server running PHP, I was thinking to use websockets, I want to run a python websocket client which listens to my PHP server and run some bash scripts on certain events. I am not sure if this is possible or makes any sense at all. Hope somebody will point me to right direction??
No, this doesn't make sense.
Javascript applications running in web browsers can make a connection to a WebSocket server, which runs as part of a web server. While it is possible (if difficult) to connect to a WebSocket server using a non-browser-based client, doing so has limited utility.
If what you want to do is launch shell scripts from a web server running PHP, you don't need sockets at all, Web- or otherwise. Execute the scripts using shell_exec() or proc_open().
I found a solution, I am using the python client from https://pypi.python.org/pypi/websocket-client/ for the client machine and using Ratchet http://socketo.me/ for my LAMP server, my client listens to the message from server and runs the script and sends back the output using same socket (OR open up the reverse ssh connection to my server and I can SSH to my client machine)
I'm very new to socket programming, but do lot of coding with php.
I have tested some socket server example codes and worked fine with localhost. I use CLI to run the server. But my concern is how do I run the socket server .php file at my hosting server? Do hosting providers normally give access to CLI to run the servers? How do I make sure my server is always running? If the hosting server is restarted, what happens to my server? In case, my server crashes (whatever reason), do I have to run it manually?
Can someone help?
If you are talking about a hosting server I expect you are talking about shared hosting. In that case it will be difficult to keep it stable if you even manage to run the service etc. I would suggests using at least a VPS for it. That way you can run it in the background, automatically start it at reboot but also install software to check the process and restart it if it failed.
For example: Testing whether the reboot startup works is impossible at shared hosting.
I don't know if your provider give you ssh access. Some provider do it but this are managed server or root server.
Then you can run your script over the CLI.
When you can run your server over CLI and when you have enough rights you can insert the script to the runlevel. And there is something that is called "shebang". With this you can give your script direct the php interpreter and run the script without the php command before.
php test.php or /usr/bin/php testScript.php
You can run direct run your script with test.php or name your script only testScript.
When you put your script to /usr/local/bin (for debian) you can run it everytime over the command like the php command.
Edit: I have forgotten something. For this solution you have to copy the /etc/init.d/skeleton to /etc/init.d/runPHPSocketServer for example and change the script values on top. Then you can insert it to the runlevel.
#: testScript or runPHPSocketServer start
When the script is under a executable directory you can insert it to your system runlevel.
#: update-rc.d runPHPSocketServer defaults
So you see there are some solution but for the most solutions you need ssh access.
I am playing around with phpwebsocket. I got the code from http://code.google.com/p/phpwebsocket/. the implementation is that I have to run the server.php using command line which starts the socket linstening on a port. but I am wondering if I upload the script to my blog server(one.com), how can I start the server.php without command line?
thanks for any tips.
Starting it on the command-line is the appropriate mechanism.
You could run it through a webserver, but only by hacking around with the default lifetime of scripts, and it's not how PHP server scripts are designed to be run.
To use WebSocket you need a process which is listing to specific WebSocket port. You need to start using command line. By using this please make sure that you have enough error catching mechanism in place in php script. So it is better to use a shell script which can restart the server if any major error occures.
http://www.techzonemind.com/php-websocket-library-two-way-real-time-communication/
I have developed a library to solve the issue. Include both client(JavaScript) and server (PHP). Also included shell script(.sh) to start the php process.
I want to ask a conceptual question: how a server written in php works?
Actually I want to know when I write a simple php code to get some information from a client, how does the whole process happen?
In java I have to start server first. Server listens to the port. When any client knocks then connection creates. Is it similar to php? Before running client application do I have to run my php code or server will do that for me? I am using localhost.
see this article: http://kevin.vanzonneveld.net/techblog/article/create_daemons_in_php/
Before running client application do I
have to run my php code or server will
do that for me.
This depends on your server architecture, you can use something like inet.d which invokes your script only if there is access on that port, or you have the standalone version, where you start the php from console und it waits for connections.
Does it similar to php ?
Yes.
Before running client application do I have to run my php code or server will do that for me.I am using localhost.
If you are writing a server in PHP then your PHP code is the server.