Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Redirecting Apache Logs to STDOUT

I have a docker container running an apache server in background (apachectl start &) and i am directing the logs to STDOUT/STDERR via CustomLog /dev/stdout and ErrorLog /dev/stderr directives.

Now , when i invoke the script containing the apache start command (apachectl start &) , i am unable to view the logs on the console.

However , if use apachectl start -DFOREGROUND as the command , i can very well see the output on console.

Any idea as to how do we get the logs on console for apache which is running in the background?

Thanks for the help

like image 878
Ritesh Avatar asked Oct 26 '16 13:10

Ritesh


People also ask

How do I display Apache logs?

You can access Apache logs from var/log/log_type. For example, you can access Apache logs from the Apache Unix/Linux server by looking in the following directories: /var/log/apache/access. log.

How do I send Apache logs to syslog?

Apache doesn't only support logging to files. For example, you can also send logs directly to a syslog service using a custom logging pipeline. The most common method is to use the /usr/bin/logger command, which forwards logs over a syslog socket to the syslog service.

Where do Apache logs go?

LogFormat Directive In Linux, Apache commonly writes logs to the /var/log/apache2 or /var/log/httpd directories depending on your OS and Virtual Host overrides. You can also define a LogFormat string after the filename, which will only apply the format string to this file.


1 Answers

If your container runs a main Bash script, you can add these redirections:

sudo ln -sf /proc/$$/fd/1 /var/log/apache2/access.log
sudo ln -sf /proc/$$/fd/2 /var/log/apache2/error.log

It will redirect the Apache output to the current Bash's stdout/stderr. Make sure to (re)start Apache after you create the symlink, otherwise Apache will not see it.

It is fine if Apache runs as a systemd/init.d service. In other words, Apache doesn't need to run within Bash.

For us, the purpose of Docker containers is to run continuous integration tests, so I added the lines to our .gitlab-ci.yml file.

Some background: Each process has its own stdout/stderr. Another instance of Bash will not necessarily be connected to the same stdout/stderr. Similarly, the OP's Apache directive ErrorLog /dev/stderr will write it to the syslog or the journal if Apache runs as a service.

like image 180
Gogowitsch Avatar answered Oct 11 '22 17:10

Gogowitsch