Monit on CentOS causes httpd.pid not to be created - apache

The solution was to replace this line:
check process apache with pidfile /var/run/httpd.pid
With this line:
check process httpd with pidfile /var/run/httpd/httpd.pid
And I also removed the 'group apache'.
Original post:
After installing Monit on CentOS, and setting an alert for the Apache (httpd) service, the service no longer creates the /var/run/httpd.pid file.
The httpd service IS running properly.
On top of it, as if that's not enough, Monit reports the status of the service as: Execution failed
Naturally, the only way to restart such a service is by killing it, since the 'restart' script doesn't see any running process.
These are the contents of the /etc/monit.d/monitrc file:
set daemon 10
set logfile syslog facility log_daemon
set mailserver localhost
set mail-format { from: me#server.com }
set alert bugs#server.com
set httpd port 2812 and
# SSL ENABLE
# PEMFILE /var/certs/monit.pem
allow user:password
check process apache with pidfile /var/run/httpd.pid
group apache
start program = "/etc/init.d/httpd start"
stop program = "/etc/init.d/httpd stop"
if cpu is greater than 180% for 1 cycles then alert
if totalmem > 1200 MB for 2 cycles then restart
if children > 250 then restart
check process sshd with pidfile /var/run/sshd.pid
start program "/etc/init.d/sshd start"
stop program "/etc/init.d/sshd stop"
if failed port 22 protocol ssh for 5 cycles then restart
if 5 restarts within 25 cycles then timeout
Output of "service httpd restart":
Stopping httpd: [FAILED]
Starting httpd: (98)Address already in use: make_sock: could not bind to address 0.0.0.0:80
no listening sockets available, shutting down
Unable to open logs
[FAILED]
Any help will be greatly appreciated.

Try to replace stop program with /usr/sbin/httpd -k stop. It work for me.

I had the same problem but /usr/sbin/httpd -k stop didn't seem to help since this still tries to look up the process id from the pid file.
I opted for stop program = "/usr/bin/killall httpd". I don't think this is very elegant (probably kills open requests) but it was the only way I could find to restart apache and have the pid file recreated by monit.

I think that monit is doing a restart as 'stop; start' and is not waiting for 'stop' to finish before starting a new process, and thus is deleting the pid file at an inappropriate time. At least, that's my conclusion after tinkering with all this.
I found a reference to someone who fixed this issue by making monit sleep after the 'stop' statement.
Personally, I found that replacing 'restart' with 'start' when the http server is down worked just fine.

Related

Restart apache forcing it

I have a Centos server running Apache and sometimes the server is working fine but I need to restart it due to changes in Apache configuration files. I usually execute:
systemctl restart httpd.service
But sometimes this command hangs for 4 or 5 minutes! It takes very long to stop apache. I know the problem is stopping and not starting cause if I execute systemctl stop httpd.service it also takes 4 or 5 minutes.
Is there anyway to force apache to restart without waiting so long?
I found that reload option is better than restart httpd service.
systemctl restart httpd

Bash Script: Wait for apache2 graceful-stop

On Ubuntu 12.04 server, I have a bash script to gracefully stop my apache2 server, removing the content of /var/www, unzipping the new content and the start the apache again. (Everything is executed as root)
echo "Test";
cd /var;
service apache2 graceful-stop;
rm -R www/ && echo "Flush...";
unzip transfer.zip > /dev/null && echo "Flushed.";
service apache2 start;
The error I get is when apache starts again:
Test
Flush...
Flushed.
(98)Address already in use: make_sock: could not bind to address 0.0.0.0:80
no listening sockets available, shutting down
Unable to open logs
Action 'start' failed.
The Apache error log may have more information.
So the script doesn't wait for apache to stop.
Here what I tried so far:
I tried to wait with wait (Same error.)
service apache2 graceful-stop;
wait $!;
I tried to get the PID of apache and wait for this one (Same error)
pid=$(cat /var/run/apache2.pid)
apache2ctl graceful-stop;
wait $pid;
I tried to use apache2ctl graceful-stop instead of service apache2 graceful-stop (Same error)
What am I missing? When I use service apache2 stop, everything works fine:
* Stopping web server apache2
... waiting [ OK ]
Flush...
Flushed.
* Starting web server apache2 [ OK ]
Thanks
Edit
Here the output with the exit code of wait:
* Stopping web server apache2 [ OK ]
0
Flush...
Flushed.
* Starting web server apache2
(98)Address already in use: make_sock: could not bind to address 0.0.0.0:80
no listening sockets available, shutting down
Unable to open logs
Action 'start' failed.
The Apache error log may have more information.
[fail]
It seems that Apache itself recommends waiting a couple of seconds between restarts:
http://wiki.apache.org/httpd/CouldNotBindToAddress
It is actually relatively common that releasing and binding to a port is not immediate. It may take some time (up to several minutes) for the kernel to free up the closed socket. It is called Linger Time. It is also briefly discussed by some Apache documentation, see http://httpd.apache.org/docs/2.2/misc/perf-tuning.html search for "Lingering Close".
There is a very detailed answer about the issue to this question: Socket options SO_REUSEADDR and SO_REUSEPORT, how do they differ? Do they mean the same across all major operating systems?

Unable to start httpd on OS X Mavericks

I upgraded to OS X Mavericks and I am trying to start the apache web server
I am using the command
sudo apachectl start
to start the server but I get the following error
(13)Permission denied: make_sock: could not bind to address [::]:80
(13)Permission denied: make_sock: could not bind to address 0.0.0.0:80
no listening sockets available, shutting down
Unable to open logs
With AMPPS or any other app, you can run it through Terminal:
Start
$ sudo /Applications/AMPPS/apache/bin/apachectl start
Stop
$ sudo /Applications/AMPPS/apache/bin/apachectl stop
had the same thing this morning, out of the blue..
I found that running :
sudo /Applications/AMPPS/apache/bin/apachectl start
at least gave me some info :
AH00544: httpd: bad group name administration
so I typed (in terminal)
groups
to see which groups were available and found that there is no administration group but there is admin group
so I went into httpd.conf and found the user/groups config section and changed accordingly
and -surprise!- apache started!
Here is what u can do.
Make sure the server is turned on with sudo ( root ) but I assume this is going just fine since u are using sudo in your start post.
The other way to solve this is to check in your
/etc/apache2/httpd.conf
And change the listen port from 80 into 8080 or anything greater than 1024
Official Bug Fix is here, so you can manage it from UI.
Also sudo apachectl start/stop is for apache bundled with Mac OS X.
Type the whole path as suggested by Daniel above.
Have a read of this blog post
It sounds like you're not seeing the same "silent terminating" thing, but maybe you have a similar problem, that with Maveriks superfluous "Listen 80" line in another config file will now trip up apache. So check if you have a config file under /etc/apache2/users , and also check VirtualHosts which maybe configured in /etc/apache2/extra/httpd-vhosts.conf (or try temporarily disabling these)
Another service is running on the port 80.
Check the service by execute this command:
sudo lsof -i :80
Stop the service before you run start the apachectl.

Getting the error "Apache port:443 is being used by another application" after installing AMPPS

After installing AMPPS for Windows, while trying to launch Apache I get an error saying,
Apache port:443 is being used by another application.
I do not have any other programs (that I know of) such as Skype that are currently running. How can I monitor my 443 port or change the port for Apache?
By the way, I have McAfee as an anti-virus.
Open command prompt(start -> run -> cmd) and type the following command :
C:\> netstat -aon | findstr 0.0:443
Last column of the output is the PID of the application using port 443.
You can find the application name in Task Manager. Go to Process Tab then in Menu Bar of Task Manager go to View -> Select Column -> Check "PID" and press Ok. Search for the PID in the list(Click Below "Show processes from all users" in case if you don't find the PID), corresponding process is the application which is using port 443. Stop or Uninstall it to make your AMPPS Apache work.
For terminate any process:
open cmd as administrator
netstat -aon | findstr 0.0:443
shows: TCP 0.0.0.0:443 0.0.0.0:0 LISTENING 4876, NOTE THE PID 4876
taskkill /pid 4876 /f
For disable port of other program(vmware):
open VMware Workstation
Edit->Preferences...->Shared VMs->Change Settings->Yes->Disable Sharing
You can change the port. -> Ok
I was facing the same issue as on port 443, vmware service was running, i went to task manager and stopped the service and then started apache and it worked fine.
After getting the pid number using netstat -aon | findstr 0.0:443, if you are having trouble finding pid 443 in Task Manager then:
Kill the process 443 by using the cmd: taskkill /pid 443.
You will avoid downloading any software or any other headache.
Here is the more elaborated way to solve this issue based on comments from Jigar and Daniel Dropik(Thank You guys),
So check with which service you are getting this port issue, in my case it was with Apache and MySQL.
Starting with Apache, either click on "Logs" in XAMPP control panel and open error log to see the problem or go to XAMPP installation directory and run "apache_start.bat" batch file, this will also give to the problem cause.
Now you have got the Port number which causing trouble,
Now follow Jigar's comment and run
netstat -aon | findstr 0.0:443
Remember 443 is the port number so enter the port number causing the issue.
This command will give the PID of the process using the port like below,
TCP 0.0.0.0:443 0.0.0.0:0 LISTENING 4996
So 4996 is the process ID(PID) that you want to stop.
Now using Task Manager you could see and kill the process but some processes could not displayed by Task Manager, in this case you have to download Mycrosoft's Process Explorer, unzip the downloaded package and run the ".exe" file as an Administrator.
You will find a bunch of processes running, sort them using PIDs and you will find your service.
Select that service and stop it.
Then go to the XAMPP control panel and run Apache and you will be able to start it this time.
Follow same process for MySQL as well.
Enjoy :)
First off you must find the process using that port. we can find it with below command.
netstat -aon | findstr 443
then we might finish finded process either below command:
taskkill /PID PORTNUMBER /F
OR
you can go to taskmanager and find the process from process bar ( with swiching PID column) and click on end task.
go to Ampps\apache\conf\extra
open file httpd-ssl.conf with note++
change the port
Listen 443--->change port
VirtualHost default:443--->"change port **>
DocumentRoot "D:/Ampps/www"
ServerName localhost:443--->**change port
and saved

Monit configuration for php-fpm

I'm struggling to find a monit config for php-fpm that works.
This is what I've tried:
### Monitoring php-fpm: the parent process.
check process php-fpm with pidfile /var/run/php-fpm/php-fpm.pid
group phpcgi # phpcgi group
start program = "/etc/init.d/php-fpm start"
stop program = "/etc/init.d/php-fpm stop"
## Test the UNIX socket. Restart if down.
if failed unixsocket /var/run/php-fpm.sock then restart
## If the restarts attempts fail then alert.
if 3 restarts within 5 cycles then timeout
But it fails because there is no php-fpm.sock (Centos 6)
For anyone else with this problem on Centos 6, the php-fpm socket is in /var/run/php-fpm/php-fpm.sock
Hence final config would be like so:
check process php-fpm with pidfile /var/run/php-fpm/php-fpm.pid
group phpcgi #change accordingly
start program = "/etc/init.d/php-fpm start"
stop program = "/etc/init.d/php-fpm stop"
if failed unixsocket /var/run/php-fpm/php-fpm.sock then restart
if 3 restarts within 5 cycles then timeout
Instead of:
if failed unixsocket /var/run/php-fpm.sock then restart
you may try:
if failed port 9000 type TCP then restart
It does not require editing lighttpd/nginx config as in the location /ping case.
My /etc/monit/conf.d/php-fpm.conf on Ubuntu looks like this:
check process php-fpm with pidfile /var/run/php5-fpm.pid
stop program = "/sbin/start-stop-daemon --stop --pidfile /var/run/php5-fpm.pid"
start program = "/sbin/start-stop-daemon --start --pidfile /var/run/php5-fpm.pid --exec /usr/sbin/php5-fpm"
if failed port 9000 type TCP then restart
I´m using the ping.path directive in php-fpm to check if it´s working...
and configured it on nginx.conf (i down´t know if it´s your setup)
location /ping {
access_log off;
allow 127.0.0.1;
deny all;
root html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /usr/share/nginx/html$fastcgi_script_name;
include fastcgi_params;
}
Here is a way of checking php-fpm via a TCP connection (which is more reliable than a simple .pid):
# Empty FastCGI request
if failed port 8101
# Send FastCGI packet: version 1 (0x01), cmd FCGI_GET_VALUES (0x09)
# padding 8 bytes (0x08), followed by 8xNULLs padding
send "\0x01\0x09\0x00\0x00\0x00\0x00\0x08\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00\0x00"
# Expect FastCGI packet: version 1 (0x01), resp FCGI_GET_VALUES_RESULT (0x0A)
expect "\0x01\0x0A"
timeout 5 seconds
then restart
Source: http://richard.wallman.org.uk/2010/03/monitor-a-fastcgi-server-using-monit/
This is work for me
check process php5-fpm with pidfile /var/run/php5-fpm.pid
start program = "/usr/sbin/service php5-fpm start" with timeout 60 seconds
stop program = "/usr/sbin/service php5-fpm stop"
if cpu > 60% for 2 cycles then alert
if cpu > 90% for 5 cycles then restart
if 3 restarts within 5 cycles then timeout
group server
This is on my debian 7 running nginx and php5-fpm;
check process php5-fpm with pidfile /var/run/php5-fpm.pid
group php #change accordingly
start program = "/etc/init.d/php5-fpm start"
stop program = "/etc/init.d/php5-fpm stop"
if failed unixsocket /tmp/php-fpm.sock then restart
if 3 restarts within 5 cycles then timeout