I'm running an Apache server on CentOS and would like to be able restart the webserver from a protected page using the following:
PHP:
<?php
ignore_user_abort(true);
shell_exec('sh sh/restart.sh');
?>
restart.sh:
service httpd restart
My question is if the web server shuts down and the PHP stops executing will the sh script continue running to bring the web server back online?
You should be fine since Apache doesn't shutdown until after the command is issued. But if you really wanted to be safe, use nohup:
shell_exec('nohup sh sh/restart.sh');
If your PHP runs as apache module, then once you kill httpd your script will be terminated instantly. So you need to delegate restart to i.e. command line script (called using exec() or shell_exec())
You might be able to add an & at the end of the command. This will fork the process and run it in the background. This way it will not depend on apache still running.
shell_exec('sh sh/restart.sh &');
If this works, you should not need ignore_user_abort().
Related
So I am running a lumen application which spins up processes.
these processes are meant to run in the backround and they run as a daemon, we send a start command to the api in php and that spins up the process using shell_exec, this used to be absolutely flawless but something strange has happened in later version of php / apache2 where the process is forked and completely dependent on either apache or php-fpm depending on what mpm module / webserver you're using.
this could be tested quite easily with index.php containing
shell_exec("screen -dmS testing bash -c 'while true; do echo test && sleep 1 ; done'")
Then restart php-fpm or apache
is there any way of stopping this behaviour? I have tried > /dev/null etc etc and with & at the end without success, I really need the process to not be dependent on php-fpm or apache2.
Problem:
I am wanting to use php-cli scripts to quickly start, restart, or stop Apache 2.4 as well as MariaDB 10.5 on Windows 10. Several years ago, I did this while running Debian:
#!/usr/bin/php
<?php
system('sudo /etc/init.d/apache2 start');
system('sudo /etc/init.d/mysql start');
I would then execute the file by typing something similar to 'php -f .server.start' where .server.start was the name of the file containing the above php code. I have had some luck getting this to work by opening a command line, changing directories to C:\Path\to\Apache24 and using
<?php
system('httpd -k start');
The issue with this is I still have to navigate to that directory to make this work. The purpose is then defeated as I could simply type httpd -k start || stop || restart. I am using this machine at home for a dev box. There are times when I am interrupted for lengthy spans of time and I feel that I need a simple and quick way to shutdown both Apache and MariaDB with a command or two while I am away from my desk.
Perhaps PHP isn't the best solution here? It's what I know and am comfortable with, though, if there are other methods, such as with a batch file, I would be willing to accept any comments/feedback/direction. I have scoured the web trying to get this task done.
Windows 10 just released Anniversary Update today. Now you can use Ubuntu flavored bash command from Linux subsystem.
The question is: How to execute Windows10's Bash command from PHP?
I tried
<?php
exec('bash',$out1,$result1);
exec('ls -l',$out2,$result2);
var_dump($out1);
var_dump($result1);
var_dump($out2);
var_dump($result2);
It doesn't work. All $out are empty array, and both $results are 1.
Any idea?
Just found out that I can run web server directly from subsystem.
e.g.
$ sudo apt-get install apache2
$ sudo service apache2 start
Then put all web contents inside subsystem's directory located at %localappdata%\Lxss\rootfs
At this point I can execute bash script however I want.
You can not. Ubuntu on Windows is implemented as a different subsystem directly below the Windows Kernel. So it runs separate to normal Windows processes and can not interact with them. (At least that is how I understand it). But maybe you just have to use C:\Windows\System32\bash.exe as the command.
Bash is not meant to be used to run a server but you can run PHP-CLI on bash without an issue.
Instead of running the PHP script in Windows and then accessing bash it would be easier to create a command to run the PHP script directly in bash.
So you would run `bash.exe -c "php path/to/php-script.php"
https://blogs.msdn.microsoft.com/commandline/2016/10/19/interop-between-windows-and-bash/
So my environment is Windows server 2008 R2 with php 5.3 and 5.5 installed. I have set fastcgi.impersonate =1 in php.ini, and added the IIS_IUSRS to the Administrator group (that is not permanent, it was just a temporary action to try and eliminate problems).
If I run:
exec('dir',$output)
$output shows what I would expect. However, if I run
exec('shutdown -s -f', $output)
I get nothing from $output, and the machine does not shut down.
No errors/warnings are reported either.
I have tried putting that same command in a .bat file and running
exec('shutdown.bat',$output);
but it produces the same results as running the shutdown command via command line.
If I run the shutdown.bat file by double clicking it, or if I type the shutdown command in a cmd window, the machine will shut down. So I know that command is good. Obviously there is a higher permissions issue that I am missing, but I have no idea what it is.
So, I found out through much trial and tribulation that (at least in server 2008 r2 w/IIS7 ) executing the shutdown command from fast_cgi or php is not possible. I only had to install apache and execute the shutdown command under it, and the command worked perfectly.
I have a PHP file that when executed must restart Apache. I tried as follows:
I put in the last line:
exec('C:\apache2restart.bat');
And the file. Bat
net stop apache2 && net start apache2
But it's just stopping Apache, it does not restart. What am I doing wrong?
if a file containing only the start command works, then i suggest you to type this
sleep 10
within the two commands (or more than 10 ! just check how long stopping takes and then adjust it) in order to make the batch wait 10 seconds.
Let me know, i'm curious.
Change the file to
net stop apache2
net start apache2
Try executing a separate batch file that executes the restart script.