How to reload apache configuration for a site without restarting apache? - apache2

I have edited the variable AllowOverride for one of my websites in sites-enabled directory. How do I reload the new configuration without restarting apache? Is it possible?

It should be possible using the command
sudo /etc/init.d/apache2 reload

Another way would be:
sudo service apache2 reload

Do
apachectl -k graceful
Check this link for more information :
http://www.electrictoolbox.com/article/apache/restart-apache/

If you are using Ubuntu server, you can use systemctl
systemctl reload apache2

Updated for Apache 2.4, for non-systemd (e.g., CentOS 6.x, Amazon Linux AMI) and for systemd (e.g., CentOS 7.x):
There are two ways of having the apache process reload the configuration, depending on what you want done with its current threads, either advise to exit when idle, or killing them directly.
Note that Apache recommends using apachectl -k as the command, and for systemd, the command is replaced by httpd -k
apachectl -k graceful or httpd -k graceful
Apache will advise its threads to exit when idle, and then apache reloads the configuration (it doesn't exit itself), this means statistics are not reset.
apachectl -k restart or httpd -k restart
This is similar to stop, in that the process kills off its threads, but then the process reloads the configuration file, rather than killing itself.
Source: https://httpd.apache.org/docs/2.4/stopping.html

Late answer here, but if you search /etc/init.d/apache2 for 'reload', you'll find something like this:
do_reload() {
if apache_conftest; then
if ! pidofproc -p $PIDFILE "$DAEMON" > /dev/null 2>&1 ; then
APACHE2_INIT_MESSAGE="Apache2 is not running"
return 2
fi
$APACHE2CTL graceful > /dev/null 2>&1
return $?
else
APACHE2_INIT_MESSAGE="The apache2$DIR_SUFFIX configtest failed. Not doing anything."
return 2
fi
}
Basically, what the answers that suggest using init.d, systemctl, etc are invoking is a thin wrapper that says:
check the apache config
if it's good, run apachectl graceful (swallowing the output, and forwarding the exit code)
This suggests that #Aruman's answer is also correct, provided you are confident there are no errors in your configuration or have already run apachctl configtest manually.
The apache documentation also supplies the same command for a graceful restart (apachectl -k graceful), and some more color on the behavior thereof.

Related

invoke-rc.d fails in logrotate script, causing log rotation to fail

I am using Ububtu 20.04 with Apache 2.4.41 and logrotate 3.14.0, which is supposed to be set up such that Apache restarts in the postrotate script. The restart isn't happening, which means that Apache is writing to the wrong log file, which then grows indefinitely (because it has a suffix .log.1 rather than .log).
The postrotate script in the default /etc/logrotate.d/apache2 file looks like this:
postrotate
if invoke-rc.d apache2 status > /dev/null 2>&1; then \
invoke-rc.d apache2 reload > /dev/null 2>&1; \
fi;
endscript
The problem is that the invoke-rc.d command isn't working. If I try it from the command line, the results look like this:
# invoke-rc.d apache2 status
invoke-rc.d: could not determine current runlevel
invoke-rc.d: policy-rc.d denied execution of status.
invoke-rc.d: emulating initscript action "status", returning "unknown"
#
Can anyone give any tips on how to resolve this? I don't really know anything about invoke-rc.d, what it does or how to configure it. Would a simpler script looking like this work OK?
postrotate
apachectl restart
endscript

Mounting a GCS bucket on AppEngine Flexible Environment

I am trying to mount a GCS bucket on AppEngine Flexible Environment app using gcsfuse.
My Dockerfiles includes the following:
# gscfuse setup
RUN echo "deb http://packages.cloud.google.com/apt cloud-sdk-jessie main" | tee /etc/apt/sources.list.d/google-cloud.sdk.list
RUN echo "deb http://packages.cloud.google.com/apt gcsfuse-jessie main" | tee /etc/apt/sources.list.d/gcsfuse.list
RUN wget -qO- https://packages.cloud.google.com/apt/doc/apt-key.gpg | apt-key add -
RUN apt-get update && apt-get install -y --no-install-recommends google-cloud-sdk gcsfuse strace
RUN gcsfuse --implicit-dirs my_bucket my_dir
I took most of this from here. It's pretty much just the standard way to install gcsfuse, plus --no-install-recommends.
If I start an app this way, it does not mount the drive. This was not too surprising to me, since it didn't seem like a supported feature of the flexible environment.
Here is the confusing part. If I run gcloud app instances ssh "<instance>", then run container_exec gaeapp /bin/bash, then gcsfuse my_bucket my_dir works fine.
However, if I run gcloud app instances ssh "<instance>" --container gaeapp, then gcsfuse my_bucket my_dir fails with this error:
fusermount: failed to open /dev/fuse: Operation not permitted
This is the same error I get if I run gcsfuse as a subprocess in my main.py.
Based on this unresolved thread, I ran strace -f and saw the exact same problem as that user did, an EPERM issue.
[pid 59] open("/dev/fuse", O_RDWR) = -1 EPERM (Operation not permitted)
Whichever way I log into the container (or if I run a subprocess from main.py), I am user root. If I run export then I do see different vars, so there is some difference in what's being run, but everything else looks the same to me.
Other suggestions I've seen include using the gcsfuse flags -o allow_other and -o allow_root. These did not work.
There may be a clue in the fact that if I try to run umount on a login that cannot run gcsfuse, it says "must be superuser to unmount", even though I am root.
It seems like there is probably some security setting that I do not understand. However, since I could in theory get main.py to trigger an external program to log in and run gcsfuse for me, it seems like there should be a way to get it to work without having to do that.
RUN commands are about creating a new layer for your dockerfile, so you're actually running that command during the image creation, which the Flex build system doesn't like.
I'm not sure why shelling out in the application didn't work, you could try 'sudo'ing it in the python subprocess, or possibly push it out of the application code by adding 'gcsfuse setup &&' to the ENTRYPOINT in the dockerfile.

What is the difference between apache2 reload, restart, graceful?

I am using apache2 for a project and I am wondering what is exactly the difference between these commands:
service apache2 restart
service apache2 reload
service apache2 graceful
There main difference between the four different ways of stopping/restarting are what does the main process do about its threads, and about itself.
Note that Apache recommends using apachectl -k as the command, and for systemd, the command is replaced by httpd -k
apachectl -k stop or httpd -k stop
This tells the process to kill all of its threads and then exit
apachectl -k graceful or httpd -k graceful
Apache will advise its threads to exit when idle, and then apache reloads the configuration (it doesn't exit itself), this means statistics are not reset.
apachectl -k restart or httpd -k restart
This is similar to stop, in that the process kills off its threads, but then the process reloads the configuration file, rather than killing itself.
apachectl -k graceful-stop or httpd -k graceful-stop
This acts like -k graceful but instead of reloading the configuration, it will stop responding to new requests and only live as long as old threads are around. Combining this with a new instance of httpd can be very powerful in having concurrent apaches running while updating configuration files.
Source: https://httpd.apache.org/docs/2.4/stopping.html
Recommendation: Use -k graceful unless there is something wrong with the main process itself, in which case a combination of -k stop and -k start or -k graceful-stop and -k start are the options of choice.
Difference between “restart” and “reload”
Restart= stop + start
Reload = remain running + re-read configuration files.
Normal restart and graceful restart, you can reference article:
https://teckadmin.wordpress.com/2013/10/23/difference-between-graceful-restart-and-normal-restart/
Seems like graceful and reload are the same for apache2
In /etc/init.d/apache2:
graceful | reload | force-reload)
# rest of the script

How to stop solr with command line

I want to stop solr by command so if find this article
http://rc98.net/solrinit
echo "Stopping Solr"
cd $SOLR_DIR
java -Xmx1024m -DSTOP.PORT=8079 -DSTOP.KEY=stopkey -jar start.jar --stop
what is Xmx1024m ?
and where i will find DSTOP.KEY ?
Please tell proper procedure to start and stop solr by command line.
solr stop -all # Stops all the ports
solr stop -p 8983 # Stop the particular port
If it shows the error ERROR: Port 8983 is already being used by another process., kill the console window which runs Solr by Ctrl-C and then try one of the above options. Because killing the console window does not stop the service.
-XmX1024m - is the java parameter and specifies the maximum java heap size, which in your case is set to 1 GB.
stopkey is a secret key on startup which must also be present on the termination request to enhance security.
You can find a detailed explanation # http://docs.codehaus.org/display/JETTY/Securing+Jetty
Example -
Starting solr with Jetty -
java -DSTOP.PORT=8079 -DSTOP.KEY=mysecret -jar start.jar
Stopping Solr with Jetty -
java -DSTOP.PORT=8079 -DSTOP.KEY=mysecret -jar start.jar --stop
the STOP.KEY is the secret key which should match during the stop command.
You could run (where 8983 is the default solr port)
lsof -i :8983
which will return PID#, then use
kill -9 PID#
to kill that pid (run it without the hash)
This worked for me if rake sunspot:solr:stop doesn't work
ps -ef | grep solr
then using the information returned
sudo kill <process_id>
example:
sudo kill 792
For local operation, you can kill the Solr server by pressing Ctrl-c in the console window
in which you started Solr. Typically, this is safe enough for development and testing

How can I stop redis-server?

I apparently have a redis-server instance running because when I try to start a new server by entering redis-server, I'm greeted with the following:
Opening port: bind: Address already in use
I can't figure out how to stop this server and start a new one.
Is there any command I can append to redis-server when I'm typing in the CLI?
My OS is Ubuntu 10.04.
Either connect to node instance and use shutdown command or if you are on ubuntu you can try to restart redis server through init.d:
/etc/init.d/redis-server restart
or stop/start it:
/etc/init.d/redis-server stop
/etc/init.d/redis-server start
On Mac
redis-cli shutdown
A cleaner, more reliable way is to go into redis-cli and then type shutdown
In redis-cli, type help #server and you will see this near the bottom of the list:
SHUTDOWN - summary: Synchronously save the dataset to disk and then
shut down the server since: 0.07
And if you have a redis-server instance running in a terminal, you'll see this:
User requested shutdown...
[6716] 02 Aug 15:48:44 * Saving the final RDB snapshot before exiting.
[6716] 02 Aug 15:48:44 * DB saved on disk
[6716] 02 Aug 15:48:44 # Redis is now ready to exit, bye bye...
redis-cli shutdown is most effective. The accepted answer does not work for me (OSX Lion). Thanks, #JesseBuesking.
For OSX, I created the following aliases for starting and stopping redis (installed with Homebrew):
alias redstart='redis-server /usr/local/etc/redis/6379.conf'
alias redstop='redis-cli -h 127.0.0.1 -p 6379 shutdown'
This has worked great for local development!
Homebrew now has homebrew-services that can be used to start, stop and restart services. homebrew-services
brew services is automatically installed when run.
brew services start|run redis
brew services stop redis
brew services restart redis
If you use run, then it will not start it at login (nor boot). start will start the redis service and add it at login and boot.
stop the redis server type in terminal with root user
sudo service redis-server stop
the message will be display after stop the redis-server
Stopping redis-server: redis-server.
if you want to start the redis-server type
sudo service redis-server start
if you want to restart the server type
sudo service redis-server restart
Type SHUTDOWN in the CLI
or
if your don't care about your data in memory, you may also type SHUTDOWN NOSAVE to force shutdown the server.
Try killall redis-server. You may also use ps aux to find the name and pid of your server, and then kill it with kill -9 here_pid_number.
Option 1: go to redis installation directory and navigate to src , in my case :
/opt/redis3/src/redis-cli -p 6379 shutdown
where 6379 is the default port.
Option 2: find redis process and kill
ps aux | grep redis-server
t6b3fg 22292 0.0 0.0 106360 1588 pts/0 S+ 01:19 0:00 /bin/sh /sbin/service redis start
t6b3fg 22299 0.0 0.0 11340 1200 pts/0 S+ 01:19 0:00 /bin/sh /etc/init.d/redis start
And Then initiate kill:
kill -9 22292
kill -9 22299
I'm using Centos 6.7 , x86_64
hope it helps
I would suggest to disable Redis-server, which prevents auto start while computer restarts and very useful to use docker like tools etc.
Step 1: Stop the redis-server
sudo service redis-server stop
Step 2: Disable the redis-server
sudo systemctl disable redis-server
if you need redis, you can start it as:
sudo service redis-server start
Another way could be:
ps -ef | grep -i 'redis-server'
kill -9 PID owned by redis
Works on *NIX & OSX
MacOSX - It Worked :)
Step 1 : Find the previously Running Redis Server
ps auxx | grep redis-server
Step 2 : Kill the specific process by finding PID (Process ID) - Redis Sever
kill -9 PID
if you did make install (e.g ubuntu) while installing redis then you can do:
redis-cli shutdown
as pointed by #yojimbo87 :)
systemd, ubuntu 16.04:
$ sudo systemctl is-active redis-server
active
$ sudo systemctl is-enabled redis-server
enabled
$ sudo systemctl disable redis-server
Synchronizing state of redis-server.service with SysV service script with /lib/systemd/systemd-sysv-install.
Executing: /lib/systemd/systemd-sysv-install disable redis-server
Removed /etc/systemd/system/redis.service.
$ sudo systemctl stop redis-server
Usually this problem arises after I shut down my computer ( or leaving running ) an irregular way.. I believe the port gets stuck open, while the process stops but continues to be bound to the previous port.
9/10 times the fix can be:
$ ps aux | grep redis
-> MyUser 2976 0.0 0.0 2459704 320 ?? S Wed01PM 0:29.94 redis-server *:6379
$ kill 2976
$ redis-server
Good to go.
Another way could be :
brew services stop redis
If you know on what port it would be running(by default it would be 6379), you can use below command to get the pid of the process using that port and then can execute kill command for the same pid.
sudo lsof -i : <port> | awk '{print $2}'
the above command will give you pid.
kill <pid>;
This would shutdown your server.
Following worked for me on MAC
ps aux | grep 'redis-server' | awk '{print $2}' | xargs sudo kill -9
If you know on which port(default:6379) your redis server is running you can go with option 1 or you can check your redis process and you can kill with option 2
option 1:Kill process on port:
check : sudo lsof -t -i:6379
kill : sudo kill `sudo lsof -t -i:6379`
option 2:
Find the previously Running Redis Server:
ps auxx | grep redis-server
Kill the specific process by finding PID (Process ID) - Redis Sever
kill -9 PID
Now start your redis server with
redis-server /path/to/redis.conf
In my case it was:
/etc/init.d/redismaster stop
/etc/init.d/redismaster start
To find out what is your service name, you can run:
sudo updatedb
locate redis
And it will show you every Redis files in your system.
If you are running redis in a docker container, none of the present answers will help. You have to stop redis container. Otherwise, redis process will keep respawning.
$ docker ps
CONTAINER ID IMAGE PORTS
e1c008ab04a2 bitnami/redis:4.0.8-r0 0.0.0.0:6379->6379/tcp
$ docker stop e1c008ab04a2
e1c008ab04a2
To stop redis server
sudo service redis-server stop
and check the status of it using
sudo service redis-server status
If Redis is installed via snap:
sudo snap stop redis.server
I don't know specifically for redis, but for servers in general:
What OS or distribution? Often there will be a stop or /etc/init.d/... command that will be able to look up the existing pid in a pid file.
You can look up what process is already bound to the port with sudo netstat -nlpt (linux options; other netstat flavors will vary) and signal it to stop. I would not use kill -9 on a running server unless there really is no other signal or method to shut it down.
The commands below works for me on Ubuntu Server
$ service /etc/init.d/redis_6379 stop
$ service /etc/init.d/redis_6379 start
$ service /etc/init.d/redis_6379 restart
Redis has configuration parameter pidfile (e.g. /etc/redis.conf - check redis source code), for example:
# If a pid file is specified, Redis writes it where specified at startup
# and removes it at exit.
#
# When the server runs non daemonized, no pid file is created if none is
# specified in the configuration. When the server is daemonized, the pid file
# is used even if not specified, defaulting to "/var/run/redis.pid".
#
pidfile /var/run/redis.pid
If it is set or could be set, instead of searching for process id (pid) by using ps + grep something like this could be used:
kill $(cat /var/run/redis.pid)
If required one can make redis stop script like this (adapted default redis 5.0 init.d script in redis source code):
PIDFILE=/var/run/redis.pid
if [ ! -f $PIDFILE ]
then
echo "$PIDFILE does not exist, process is not running"
else
PID=$(cat $PIDFILE)
echo "Stopping ..."
kill $PID
while [ -x /proc/${PID} ]
do
echo "Waiting for Redis to shutdown ..."
sleep 1
done
echo "Redis stopped"
fi
On MacOSX,
This is what worked for me
/etc/init.d/redis restart
/etc/init.d/redis stop
/etc/init.d/redis start
One thing to check if the redis commands are not working for you is if your redis-server.pid is actually being created. You specify the location of where this file is in
/etc/systemd/system/redis.service
and it should have a section that looks something like this:
[Service]
Type=forking
User=redis
Group=redis
ExecStart=/usr/bin/redis-server /etc/redis/redis.conf
PIDFile=/run/redis/redis-server.pid
TimeoutStopSec=0
Restart=always
Check the location and permissions of the PIDFile directory (in my case, '/run/redis'). I was trying to restart the service logged in as deploy but the directory permissions were listed as
drwxrwsr-x  2 redis    redis      40 Jul 20 17:37 redis
If you need a refresher on linux permissions, check this out. But the problem was I was executing the restart as my deploy user which the permissions above are r-x, not allowing my user to write to the PIDFile directory.
Once I realized that, I logged in using root, reran the restart command on the redis (service redis restart) and everything worked. That was a headache but hopefully this saves someone a little time.
To gracefully shutdown specific instances with passwords and not resorting to brute-force kill commands, use:
redis-cli -p <port> -a <pass> shutdown
root#machine:~# ps aux
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
root 105 0.1 0.0 60552 10772 ? Ssl 23:27 0:02 redis-server 127.0.0.1:10002
root 111 0.1 0.0 60552 10900 ? Ssl 23:28 0:02 redis-server 127.0.0.1:10003
root 117 0.1 0.0 60552 10872 ? Ssl 23:28 0:02 redis-server 127.0.0.1:10004
root#machine:~# redis-cli -p 10002 -a mypassword shutdown
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
root#machine:~# ps aux
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
root 111 0.1 0.0 60552 10900 ? Ssl 23:28 0:02 redis-server 127.0.0.1:10003
root 117 0.1 0.0 60552 10872 ? Ssl 23:28 0:02 redis-server 127.0.0.1:10004
root#machine:~#
Having a ton of instances warrants writing a batch script to loop through them all for a master shutdown.
The service name of redis is redis-server, so you can disable and stop redis with this command:
sudo systemctl disable redis-server
sudo systemctl stop redis-server
You can try this code:
sudo kill -9 $(ps aux | grep 'redis' | awk '{print $2}')

Resources