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.
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.
I am using PHP-mysql on Linux (RHEL 5.0) For First Time
When I tried to connect to MySQL from my PHP Script using mysqli_connect.
It Displayed The Following Error:
Can't connect to local MySQL server through socket '/var/mysql/mysql.sock'(2)
After googling for hour I found solution to this as stated here.
When I followed that approach and issued command:
service mysqld status
as a root user
I got : mysql is dead but subsys locked Any one know how to solve this and cause of this error ?
Also
Restarting
Starting
And Stopping of MySQL
is Giving Output as: FAILED But PHP is working fine. I've tested the phpinfo(); for Demo.
I've installed MySQL in /usr/local/mysql/bin. Can Anyone Help me in this ? Any help is appreciated.
Thanks in advance.
I had this problem with my OTRS server after I tried to update a to large package.
The solution was:
copy for safty:
cp /var/lock/subsys/mysqld /root/mysqld
than delete it
rm /var/lock/subsys/mysqld
than close all services that depends on mysql:
service httpd stop
service otrs stop
after that:
service mysqld restart
service httpd restart
service otrs restart
System is CentOS 6.x
service mysqld restart
Simply restarting the mysqld worked fro me on Centos.
tail /var/log/mysqld.log to check the error log
then do the following actions:
rm /var/lock/subsys/mysqld
chown -R mysql.mysql /var/run/mysqld
first,make sure that /etc/init.d/mysqld is running from /usr/local/mysql/bin
then,kill mysqld ,delete the lock file and restart it.
This (and all sorts of other weird errors) can also happen if you have a full disk. In my case, my /var partition had filled up. Freeing up space allowed mysqld to restart again.
First find the PID number of mysqld:
1. Use top | grep mysqld
2. kill -9 PID
Safe method, works fine with Cent OS
Rebooting the server worked for me. Please remember to start all the services once you reboot in case you haven't added 'chkconfig' for them, lets say httpd, named and mysql.
i use command kill, step by step:
use command service mysqld status for find PID mysqld
kill -9 PID
use command service mysqld status again for find PID mysqld
kill -15 PID
check again with command service mysqld status , check until the PID dead OR subsys locked
use command service mysqld start
The problem I meet in the project is because of the disk space run out.
The server that MySQL is running run out the disk space, business project's log is too large. The result is that MySQL restart failed.
It is one probability and you can have a try.
du -sh *
and
service mysqld restart
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().
A file named test.html contains:
hello
It takes a few milliseconds to respond.
A file named test.php contains:
hello
This one takes a few seconds to respond. They're both the same code and contain no actual PHP code. The file extension is the only difference!
I'm loading both via http://localhost/test.html or php
Is there any common snafu in the server settings that I missed? I'm using the standard Ubuntu 11.04, Apache2, PHP5 configuration.
Your help is appreciated... let me know what other details you need.
Well even if there is no actual php code in your php file its getting sent to the php parser by apache because of its extension.
This probably slows it by a few miliseconds and on your system might be more.
Like others have pointed out you probably have some module that is taking too much time. But about your original question my answer stands. Even without code your php file is getting parsed by php.
If you want to get an idea what is going on try the following:
sudo -i
stop apache2
. /etc/apache2/envvars
apache2 -k start -X &
strace -u www-data -tt -ff -o /tmp/strace $(ps -o "-p %p" h -u www-data) &
man strace to find out what it does and don't forget to apache2 -k stop and start apache2 when you are done :) Remember you are all-powerful as root so come out ASAP.
Try adding an .htaccess file and also doing a sudo apt-get install php-apc to see how an Opcode cache works. The next stage is to strart downloading the source and matching this up to what is happening in the system trace. Enjoy.
Even though there's no PHP code, it is being parsed as PHP. The problem could exist with Apache's configuration / installed modules, or possibly if you have a bunch of (or any one problematic) enabled module(s) in PHP.