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

[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.

Related

nginx is started but not found service on ubuntu

I try to restart the apache service on ubuntu 16.04 but i can not because the port '80' is listened to by nginx.
But when I try to stop nginx, the service is not found.
netstat -ltnp | grep ':80'
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 6069/nginx
systemctl status nginx
● nginx.service
Loaded: not-found (Reason: No such file or directory)
Active: inactive (dead)
service nginx stop
Failed to stop nginx.service: Unit nginx.service not loaded.
So how can I stop nginx in order to restart apache ?
Thanks for your help
nginx on your machine was started in other way then systemd standard nginx service.
alex#openresty:~$ ps aux | grep nginx
root 2668 0.0 0.0 41040 928 ? Ss 12:33 0:00 nginx: master process /usr/local/openresty/bin/openresty -p /usr/local/openresty/nginx/
nobody 2669 0.0 0.0 41040 3316 ? S 12:33 0:00 nginx: worker process
Will show you the full path of running nginx with command line arguments. Here is example from my PC (I use Openresty bundle so your paths may vary).
Now take a look at nginx master process command line parameters. If -p is present you should use exactly the same to stop nginx. For my case it should be:
/usr/local/openresty/bin/openresty -p /usr/local/openresty/nginx/ -s stop
If -p is missed you may just
/usr/local/openresty/bin/openresty -s stop
It would stop nginx, but it may be possible that on your PC nginx is configured to run as not standard service, for example hand written systemd unit.
So on every reboot you will have nginx running again.

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?

$ service apache2 restart [fail]

I have installed Apache on my Ubuntu Server.
For a special reason I have to enable mod_rewrite on it.
So I have done this.
And in every Tutorial on the internet the last command is to restart apache.
But when I do this the console prints [fail].
Can anyone help me here?
$ service apache2 restart
* Restarting web server apache2 [fail]
I had a similar problem, and for me it was about the logged in user not having privileges so instead of
service apache2 restart
I had to do
sudo service apache2 restart
It's telling you some other service is already on port 80, perhaps it's apache
try Code:
sudo /etc/init.d/apache2 stop
followed by Code:
sudo killall apache2
then make sure no services are running on port 80 Code:
sudo netstat -l|grep www
then (re)start apache Code:
sudo /etc/init.d/apache2 restart
Using the systemd features ( starting from Ubuntu 15) , you can restart apache service as follow :
sudo systemctl restart apache2.service
Check the status:
sudo systemctl status apache2.service
There are various reason for this one .
could be the privilege problem if you have privilege problem then
please use sudo for the same .
could be the apache already running in your system then please check the
status of the service by running command service apache2 status if they
said that [FAIL] apache2 is not running ... failed! it mean it is not running
you can start by the command service apache2 start or sudo service
apache2 start
if you having not above problem please look at the PID file of
apache2 by following command cat /var/run/apache2/apache2.pid
which will give you the process ID of the apache it means you
system accidentally shutdown without deleting the PID file so
delete by following command rm - rf/var/run/apache2/apache2.pid or sudo rm -rf/var/run/apache2/apache2.pid and start again the server by
following command service apache2 start or sudo service
apache2 start
I face this issue when I was adding new web site to my web server which is hosted in Digital Ocean Cloud service. So what happened was, when I using sudo commands to restart or reload apache2 server its restring with following error messages.
For me everything worked well despite these two error messages.
Error 1 - Unable to resolve host 'YOUR HOST NAME' .
Error 2 - sum_functio_error() //I don't remember this function name I'll update this later.
So the fix was very simple.
First open your hotsts file.
sudo nano /etc/hosts
Output File
127.0.1.1 hostname
127.0.0.1 localhost
sudo nano /etc/hostname
Output File
hostnamexxx
Try below command to restart.
# /etc/init.d/apache2 restart
OR
$ sudo /etc/init.d/apache2 restart
OR
$ sudo service apache2 restart
To stop Apache 2 web server, enter:
# /etc/init.d/apache2 stop
OR
$ sudo /etc/init.d/apache2 stop
OR
$ sudo service apache2 stop
To start Apache 2 web server, enter:
# /etc/init.d/apache2 start
OR
$ sudo /etc/init.d/apache2 start
OR
$ sudo service apache2 start

ec2 LAMP instance issue

I have a micro instance on AWS that shows that it is running fine on Amazons dashboard
However when i log into the instance with SSH and try to start or stop apache
sudo service httpd stop
sudo service httpd start
I get [FAILED]
sudo apachectl start
doesnt return anything
sudo apachectl restart gives me httpd not running, trying to start
What am i missing? it seems to be doing this out of the blue
is there any command line i can test to see where the issue is coming from?
Have you changed anything in the configuration file?
/etc/init.d/httpd configtest
Is there something else listening on port 80?
fuser -n tcp 80

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.