send command line to a linux server with php - php

I've set a linux (ubuntu serv) as a RMTP server using nginx , and i'd like to be able to restart the service from a web interface as it does crashes from time to time.
I was thinking php would be great to send command line to linux , but the only way i found is to use exec()and give root permissions to www-data which doesn not sound like a good idea.
I also dont want to restart the server by itself , only the rmtp service (other stuffs running on this server)
so to sum up , i'd like to be able to send command line to a linux on a web interface w/e the language/technology is used
thanks guys and sorry for my bad english

You do not have to reinvent the wheel. There are tools out there for monitoring services. They will restart them if needed.
Take a look at
monit
http://mmonit.com/monit/
and
supervisord
http://supervisord.org/

Related

How can I run permanently my php chat socket app on Ubuntu or Debian sever?

I have a chat socket app in php, and I'd like to run not just on my local computer, but also on a webserver. How can I do that?
php -f library/filename.php
That's it.
What your answer is missing is how to keep it going, how to get it started on system reboot, how to restart it in case it stops for some reason, etc.
Depending on your distribution, you may be able to use screen or a similar utility to keep it alive. Or, you can write a systemd service file and let systemd manage keeping it going.

Ratchet: Should leave it run as a Server via "php" command, manually?

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.

Running exec on apache in windows

I am looking to send data from android device to the webserver and then execute a command using exc from server , i will be using apache , but i talked to a teacher of mine and he told me that he tried this thing some time ago but they were not able to run exec on apache in windows it was because web server didnt allow to rum executable despite of setting the permissions.
He said the same worked on linux perfectly , anyone faced the problem and its solution plus what if i use IIS as a server
they were not able to run exec on apache in windows it was because
web server didnt allow to rum executable despite of setting the
permissions.
He is wrong. I use a lot of exe files (mostly for some specific function like extraction of the text from pdf files, or to trigger WOL for remote computer) on the development website under Windows and everything works perfectly. Everything depends on what current exe file is going to do and the rights of the system account under which the Apache service is running.
Your teacher was right. You'll need a linux server if your going to be doing any kind of dedicated tasks. Not to fear tho, Linux has thousands of software sources that you can use, and they should have all the tools you'll need to complete your project. Good luck.

linux server restart in php script

I would like to know if it is possible to restart the Linux server using PHP script? In related to changing IP Address from Static to DHCP, I need to reboot the system so that it will take effect.
i tried this code:
system("/usr/bin/reboot");
error message is :
reboot: must be useruser
here'e the another:
system('/etc/init.d/network restart');
the error is:
Shutting down loopback interface: [FAILED] Bringing up loopback interface: [FAILED]
Hope you can help me in this.
Thank you!
Regards to all.
You can restart it if the program-users-context of your interpreter, webserver has the rights to execute these commands. A webserver or php interpreter should not be run as root. You may use sudo, sudoers in order to escalate privileges in these both cases.
You will need to use sudo like this:
system("sudo /usr/bin/reboot");
in your /etc/sudoers add the following:
apache ALL=(ALL) NOPASSWD: /usr/bin/reboot
Where apache is the username under which the PHP script runs.
Be aware of the security impication of doing this - anyone with access to PHP scripts on the server to reboot the server.
In related to changing IP Address from Static to DHCP, I need to reboot the system
No you don't. This is not Microsoft Windows. But the command for remapping the network interfaces varies from distribution to distribution - and you don't say which this is. Similarly, access the reboot, shutdown, init and telinit commands varies by distribution.
I am working in local server
So why not just do it via ssh or at the console?
Since you have stated that you're a newbie to Linux, I feel that it's worth pointing out that it's much much less common to need to reboot a Linux box compared to a Windows one.
You shouldn't need to reboot even after updating core software packages. Even if something crashes badly, you can ususally recover without a reboot.
You haven't stated why you'd want to be doing a reboot, but rebooting the whole box really should be an absolute last resort. In fact, rebooting simply to clear an issue is consdered very bad practice for a Linux administrator because it tends to wipe out evidence of what caused the problem, and does nothing to prevent the problem from recurring.
On Linux, most issues that would require a Windows box to be rebooted only require the individual program or service to be restarted.
Finally, a note on security: Doing major system operations such as this via a PHP program is bad security practice because it exposes root level functionality to non-root users. I assume (well, I hope!) that you're planning to lock down access to this PHP page, but even the best secured web page should not be considered secure enough to be running root-level operations.
In short, my advice is that you shouldn't do this. If you must do it, #qbert220's answer should work, but please don't do it.
[EDIT]
With specific regard to changing the IP address from DHCP to static, this should not require a server reboot in Linux. You simply need to restart the networking interface.
Once you've changed the config, something like this should be enough to restart your network interface with the new IP address in place:
sudo /etc/init.d/networking restart
You haven't specified what variety of Linux you're using, but here's a link to a page which details how to do it from the commandline in Ubuntu.
It does require root priviledges though, so you would need to use sudo to achieve it and to add your web user to the sudoers list, which as I said before is really not great from a security point of view.
Script must be set to run as root:
reboot.php
<?php
exec("reboot -d -f -i");
?>
Meke it a root script:
chown root.root reboot.php
chmod 700 reboot.php
But why do this with php? Just make a script in sh like so:
#! /bin/sh
PATH=/sbin:/usr/sbin:/bin:/usr/bin
reboot -d -f -i

Using knockd to do stuff | Sending TCP/UPD Requests via PHP

I was wondering, whether knockd http://www.zeroflux.org/cgi-bin/cvstrac.cgi/knock/wiki would be a good was to be able to restart apache without logging into ssh. But my programming question was whether there is a way to send tcp/udp packages via PHP so I can knock via a webclient.
I am aware that this is not the safest way of doing it, but I will only want to do things like update the svn, restart apache without having any passwords in it like with using ssh to do that.
You may use fsockopen() functions... but what you are doing(and the way you are doing it) is very risky from a security standpoit.. as it had been said, ssh is the way:)
If you really want to restart the apache server by using remote access (non-ssh) you can create a small php-daemon, that just watches for a specific file,(ex: /tmp/restart.apache) and when that file appears run exec("/etc/init.d/apache restart") (or whatever the command is for your distribution). This daemon should run as root... and the thing is that the whole security thing is up to you this way, you have to make sure this cannot get arbitrarly executed...
Your portknock ideea... a simple port scanner may restart your apache by mistake:) portknock is recommented to be used in conjunction with a ssh auth , not directly with apache:)
Seriously, you do not want to do what your trying to do.
You should look into calling your remote server through some sort of secure protocol, like SSH. And on the client side, have a small PHP utility application/script that executes remote SSH commands (preferably with a keyfile only based authentication mechanism).
Why not have a PHP script that calls "svn update"? As long as the files are writeable by the user Apache runs as, it works great. Just hit that URL to update the website
For SVN you have whole PHP api, try search SVN on php.net

Categories