I want to set up a WebSocket server using PHP. I have many alternatives to do this, yes, but I wanted to ask people who have experienced which one is more reliable (strong, lightweight, and faultless). I also wrote some code, this code creates a socket server, but I'm not sure how to start it, do I need to open the page from the browser?
Usually a web socket server, as many servers, is started to operate as a daemon in the background and listen there for events coming in. On Linux (Ubuntu) you might create a unit file to be consumed by the system command systemctl, this way the daemon will be started with every boot of the system and you can start and stop it as you need to.
Next you can start it form a command shell like
nohup php websocketserver &
This will send your server into the background an stay there until the system is rebooted. Any output is logged to a file named nohup.
On Windows you better create a service, via a schedule task to have your server start at system boot.
Related
I have a website in Symfony 3, which runs a script on a remote server UNIX.
I would like to know the progress(steps) of the script in real time or every X seconds, on the website without the user needing to reload the page.
What solution should be considered?
Edit - About the script:
When a user clicks a button, Symfony run a shell script that is present on the same server as my web application. The shell script connects to SSH on a remote server (user) and installs components on the remote server.
The shell script is quite long and complex.
There are 2 possible solutions:
You can do an interval that checks every x milliseconds for any changes and will apply those changes to your front-end.
The other option is to set up using web sockets and would recommend this over interval checking. It maintains an active connection to the server so it is near instant if not instant.
I would suggest checking out this bundle: https://github.com/GeniusesOfSymfony/WebSocketBundle
I don't actually understand how
which runs a script on a remote server UNIX
you are doing this but this is the solution you can use.
Have a symfony command that listens on remote service/server/app etc.
Have a symfony controller that streams the response from the console commands and echos the result in twig template as they arrive.
With this way, users don't need to refresh the pages. Wheel has already been invented here Streaming console command output from symfony controller for you. You just need to update the command as per your requirements.
Note: If you don't want console command approach, this is commandless version. Streaming symfony response from twig template with XMLHttpRequest
is there a way how to easily run a PHP application as from command line on Windows Azure?
I have a standard Web Application (on Azure) and I want to communicate using WebSockets.
So I need to have a WebSocket Server running all the time on Azure.
I use Wrench project which I need to run "all the time" to listen on some port and deal with messages from JavaScript-sended WebSocket.
So again - how easily run a "persistent" PHP application on Azure?
Thank you in advance.
Sandrino's answer is fine, but I prefer ProgramEntryPoint for doing this sort of thing. The trouble with a background task is that (unless you build something on your own) nothing is monitoring it. Using ProgramEntryPoint, Windows Azure will monitor the process, and if it exits for any reason, the role instance will be restarted.
EDIT:
Sandrino points out that the PHP program isn't the only thing running. (There's also a website.) In that case, I'd recommend launching php.exe in Run() in WebRole.cs. Process.Start it and then do a .WaitForExit() on it. That way, if the process exits, the role itself will exit from Run(), causing the role instance to restart. See http://blog.smarx.com/posts/using-other-web-servers-on-windows-azure for an example.
In order to run your PHP script as a command line application you should use the PHP CLI (command line interface).
php.exe -f "yourWebSocketServce.php" -- -arg1 -arg2 -arg3
Now, in order to run this in Windows Azure you'll need to define a startup task that runs this command. You'll see that the default task type is simple, which means that the startup of your role will block until the task finishes. But in your case running the WebSocket in PHP will be a blocking process, that's why you should change the type to background (this will make sure the instance continues starting up while your WebSocket server is running).
Here is a WebSockets service on Azure. - Live XSockets.NET
Have a look at http://live.xsockets.net, an easy way of getting started, but it depends on what you are about to do on the "server side". This service i mention can be uses as a "message" dispatcher, to ntify "clients" on changes etc.. Hmm in other words it is a way of boosting "regular" web-apps..
every one i have studied and implemented these tutorials of ray
http://www.raywenderlich.com/3443/apple-push-notification-services-tutorial-part-12
http://www.raywenderlich.com/3525/apple-push-notification-services-tutorial-part-2
i have implemented apns on local server, now i want to do it on live server, my question is that the script "push.php", which we are running on local server by using this
/Applications/MAMP/bin/php5.2/bin/php push.php development
how can we run it on live server in production mode, did we have to ask the domain providers (we are using Host Gator Services) to run this script for us or as ray says
"However, on your production server you should start the script as follows:
$ /Applications/MAMP/bin/php5.2/bin/php push.php production &
The “&” will detach the script from the shell and put it in the background."
means we will use command line interface to run that script on live server?, i am little confused because on server side we use cron jobs to execute the scripts, but this "push.php" should never exit, so i am confused here, what to do. Plz. guide me in this, thanx in advancs. Regards Saad
Yes, the command line interface should be used to run the PHP script and keep it running in the background.
However, as you are on a shared hosting service, I doubt they will let you run PHP continuously.
You may want to try asking them if it is possible; if it is not, just edit the PHP script you cited so that, instead of opening the connection at the beginning and continue running, every time it is invoked it opens a connection to the Apple server, sends the message, closes the connection and exits. Although this is not encouraged by Apple themselves, this would allow your script to be invoked only when it is necessary by the Web server (so that no continuous running is required).
I'm planning the development of a server written in PHP that can service socket requests. I use a free host (Heliohost) for testing, and it has cPanel. So far the only thing I've been able to think of to have a PHP script always running is to write a cron job that runs a bash script to check ps to see if the PHP is already running, and if it isn't, start it.
Is there a better way? Perhaps a way for a PHP thread to be started on an HTTP request and continue to run in Apache after the request has been serviced?
You will almost certainly not have success running persistent processes from Apache. It is designed to prevent that scenario (though if you can get to the fork(2) system call, it is probably do-able). I wouldn't recommend trying it though.
What would make more sense is if you use a hosting provider that gives you the ability to write your own crontab(5) specifications and run the PHP interpreter directly. Then you could just add a line to your crontab(5) like:
#reboot /path/to/php /path/to/script.php
Your script should probably perform the usual daemonization tasks so that cron(8) isn't stuck waiting for your process to exit.
I need an application to be running in the background on my web server, but I need to be able to start/stop the application with root privileges.
In order to do this, I want to have a service running that has root privileges, so that it can kill the application, and start it up again if need be.
Finally, I need to be able to send the start and kill commands to the service via Apache/PHP, so that it can be indirectly controlled through the web.
How do I create a Linux service?
How do I communicate with a Linux service in this manner?
Thanks in advance!
Use the exec command in your PHP script to call shell files. The shell files can be setup with the "setuser" bit so it will run as its owner (instead of running with the web server's permissions).
Of course, you'll need to be very careful--lots of testing, monitoring, etc.
Finally, think about the service running as a dedicated user, not as root. Eg like apache and most other well done services do.
Added: Re: running a service in Linux. Depends on your flavor of Linux. If you want to be sure that your app service will be automatically re-started if it fails, plus logging, checkout Runit:
https://web.archive.org/web/1/http://blogs.techrepublic%2ecom%2ecom/opensource/?p=202
http://smarden.org/runit
Added: Instead of setuid bit, I think Frank's suggestion (in comment) of using sudo system is better.
So, you have three pieces here :
Your web server without root privilege
An application
A daemon that is monitoring the application
Your problem is not launching the daemon, it is writing it, and communicating with it from the web server, without needing root privilege.
A daemon can be as simple as a non interactive application launched in the background :
# my_dameon &
I am not a php developper, but searching for message queue and php, I discovered beanstalkd
Looking at the example on the first page it seems you can use it to do the following :
The apache/php sends some message to beanstalkd
Your daemon reads the message from beanstalkd. Based on the command it starts or kill or reload the background application.
You can write your daemon in php, since there are client in many languages
You can also check this question
You can create a daemon which accepts the following commands:
daemon_name start
daemon_name stop
daemon_name restart
deamon_name reload
Starting the daemon should not be hard. Just executing daemon_name start from a PHP script should run it. After starting, you can write the PID of the process to a lock file (for stopping, restarting or reloading later). The daemon should handle signals.
In a PHP script, you can then invoke daemon_name stop. This should fire up a new daemon which would check the lock file and get the PID of the running daemon and send a signal which would be handled by the running daemon. The lock file should be removed/cleared and then the stop initiating daemon/process can quit.
I think you should look at inetd, which can be configured to run all sorts of services, and it runs as root. You could then write a relatively simple program that is not itself root privileged, but which when run by root does the tasks you need done.
As far as communicating with the service you did not say what type of service it is, however assuming you're writing it yourself the most common methods would be to comunicate via UNIX sockets or MMAP. Depends on your needs really.
Oh yeah, should point out there are already applications for web management of linux systems. Webmin is a really good one which can be configured to allow as much or as little control as you need.
As #shodanex suggests, using Beanstalkd would be an excellent way to disconnect a web-front-end from a running-as-root command line worker. It could trivially be set to only run exactly what was required.
To run the worker, Pear's System_Daemon can generate and run a daemon-running script, with start/stop/restart.
When doing this be very very careful. Never use any user submitted data from the web in the exec command. this could allow someone to arbitrarily execute commands on your machine.
Also I second Frank use a sudo rule so you can run that specific command with the permissions you need but nothing else. It will be more secure that way.
Of course with
sudo apt-get install openbsd-inetd
you can create a service you want