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.
Related
I'm a LAMP guy, and now start learning WebSockets via Ratchet. So far so good following the start up docs here, and hence i'm able to run the Ratchet Server, like this:
$ php server.php
And then my Javascript Clients can connect to it, etc.
But..
As a LAMP guy, i'm very used to have Apache (or) NGINX as the "Server" for any PHP files to serve to public. Now... should i just run that above command in my terminal, and that's gonna be the Ratchet Server?
Is there a way NOT to run the server like that? (or) Is there a way to let Apache (as an example) manage the Ratchet Server? Which means, let Apache start/stop the Ratchet whenever i type:
$ service httpd start
$ service httpd stop
I'm more confident this way. Plus, the SSL handling, etc also would be then done by Apache more easily. Am i right please?
Please kindly suggest, as i'm very new to this area. Thanks all :)
You indeed are right that running it in the command line is not a production ready solution.
In the last page of the tutorial (deployment) there are some ways to do it. For example, hypervisor is entirely explained how to set it up there.
If you don't like hypervisor usage, then you could try to just write a shell script which is executed on startup, that starts the server.php (less good solution, yet easier)
The ssl part you want to use is possible using a proxy with apache.
If you are using Apache web server (2.4 or above), enable these modules in httpd.conf file :
mod_proxy.so
mod_proxy_wstunnel.so
Add this setting to your httpd.conf file
ProxyPass /wss2/ ws://ratchet.mydomain.org:8888/
If you have any more questions please let me know.
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.
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 have written a PHP Script, I run the code like this on MS-DOS on my local PC:
C:\wamp\bin\php\php5.3.0\php.exe index.php
Now I want to upload the index.php on the web but on multiple computers (without PHP/Apache installed), how can I run index.php on ms-dos via web?
If that not possible, I can open multiple putty application on each computer and login ssh?
index.php will continues looping - it wont be good to run script on the browser because it may cause timeout via browser.
You can't. If you're using putty on each computer to log into another machine to run the script, the script will still be running on "the other" machine. All you've done is make its output appear on the local computer that's running putty. It'd be exactly the same as opening multiple shells on the server and running the script in each window.
If you want a PHP script to run "locally", then you need PHP installed locally on each machine that will run the script. And like I said in your other similar question, there are PHP compilers which can produce a .exe that MIGHT be portable, but it's not guaranteed to work. PHP can be compiled, but not all scripts are compileable.
Check out bamcompile.
It lets you compile the PHP file to an exe.
Not everything works but it is worth the shot: http://www.bambalam.se/bamcompile/
I think it uses PHP 4.something so don't know wether it works for your script without seeing it
EDIT
You said you are worried about an timeout if ran in browser.
What does the script do?
Perhaps PHP isn't you're best option.
The easiest way would be to have Apache and PHP installed on those computers, and then have a script visible on that computers that runs the command for you.
exec("C:\wamp\bin\php\php5.3.0\php.exe index.php")
This way, you can start running the script without having the script time out from Apache's end.
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.