Bash Script: Wait for apache2 graceful-stop - apache

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?

Related

Apache server not starting 'Address already in use' ERROR (98)

I've been trying for the past few days to solve this issue in several different ways and got no results.
Below is a copy of all the last commands i ran trying to gather more info about the issue:
2019 from 181.167.101.245 bitnami#ip-172-31-2-74:~$ sudo service
apache2 restart
* Restarting web server apache2
(98)Address already in use: AH00072: make_sock: could not bind to
address [::]:8
0
(98)Address already in use: AH00072: make_sock: could not bind to
address 0.0.0.
0:80
no listening sockets available, shutting down
AH00015: Unable to open logs
Action 'start' failed.
The Apache error log may have more information. > [fail]
* The apache2 instance did not start within 20 seconds.
Please read the log files to discover problems
bitnami#ip-172-31-2-74:~$ sudo netstat -nlp |
grep 80
tcp6 0 0 :::80 :::*
LISTEN
13456/httpd.bin unix 2 [ ACC ] SEQPACKET LISTENING 1680
402/systemd-udevd /run/udev/control
Any kind of help will be greatly appreciated and let me know if additional information is needed.
As always thanks in advance.
Bitnami Engineer here,
If you are using a Bitnami ready-to-use instance with any of our applications, they usually include Apache to serve the application. In case you perform any change in the Apache's configuration files inside the /opt/bitnami directory, you need to restart the Apache's service by using the ctlscript.sh file we provide.
sudo /opt/bitnami/ctlscript.sh
You can learn more about it here:
https://docs.bitnami.com/aws/faq/administration/control-services/

Chef on AMZ ami, restart fails with httpd is stopped Starting httpd: Address already in use

I'm implementing a chef script on an Amazon AMI image via ops works. My goal is to insert the httpd.conf and a conf.d/wa.conf to setup a site on port 8889. Then restart apache with the new configuration.
However, when I try to restart I get the following error:
STDOUT: Stopping httpd: [ OK ]
httpd is stopped
Starting httpd: [FAILED]
STDERR: (98)Address already in use: make_sock: could not bind to address 0.0.0.0:8889
no listening sockets available, shutting down
Unable to open logs
End output of service httpd stop
----
I've tried a number of configurations, but all of them seem to fail. When I logon to the box after the config fails, port 8889 is not in use. I can start httpd by hand at that point, but it still fails.
Here is the chef script:
service 'httpd' do
supports :status => true, :start => true, :stop => true, :restart => true
action [:nothing]
end
template "/etc/httpd/conf/httpd.conf" do
source 'httpd.conf.erb'
action :create
notifies :restart, 'service[httpd]'
end
I've tried a number of variations, but it always seems to start before the shutdown is complete maybe?
Any help would be greatly appreciated.
thanks.
myles.
For some reason port 8889 could not be used.
I switched the server to a different port and it worked.

Fabric's sudo('service httpd start') doesn't work

[1] I executed Fabric's sudo('service httpd start').
[chikaram] sudo: service https start
[chikaram] out: Starting httpd: [ OK ]
[2] But my web page is still down!
[3] So I checked httpd status on EC2
[ec2-user ~]$ sudo service httpd status
httpd dead but subsys locked
[4] Then I started httpd on EC2.
[ec2-user ~]$ sudo service httpd start
Starting httpd: [ OK ]
[ec2-user ~]$ sudo service httpd status
httpd (pid 25217) is running...
I've tried this many times but the results always same.
Fabric's sudo('service httpd start') fails with message "httpd dead but subsys locked."
"sudo service httpd status" on EC2 works.
So as of now, I'm doing like: local('ssh chikaram "sudo service httpd start"')
Any ideas?
I faced same problem and resolved by below.
Here is my workaround.
sudo('service %s restart && sleep 5' % service,)
This program restart some service defined above(such as nginx) and wait 5 sec if service restart return code is zero.
I guess root cause is fabric session is shutdown before fork is not completed.

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.

Monit on CentOS causes httpd.pid not to be created

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.