15

I have discovered that my access_log is occupying most of my HDD. It's over 200 GB in size. How can I reset it ?

I am using Apache 2.2.3 on a CentOS server with Plesk.

Thank you guys !

user290367
  • 161
  • 1
  • 1
  • 5

8 Answers8

18

knx'answer is good, but I would suggest to rename the log, and create a new one, so that you can restart apache without waiting for the access log to be compressed, which can take a while if it's big.

needs access to ssh

First, rename the current log file:

mv /var/log/apache/access.log /var/log/apache/access.log.1

Second, create a new log file and give the same permissions, owner/group and selinux context as the original one:

touch /var/log/apache/access.log
chown --reference=/var/log/apache/access.log.1 /var/log/apache/access.log
chmod --reference=/var/log/apache/access.log.1 /var/log/apache/access.log
restorecon --reference=/var/log/apache/access.log.1 /var/log/apache/access.log

(probably need to be root to do that)

Next, restart apache

Then Gzip the old file (text files compression ratios are really good). If we assume the file is named /var/log/apache/access.log then do this:

gzip -c /var/log/apache/access.log.1 > /var/log/apache/access.log.1.gz

these 4 points are what logrotate do automatically.

Stu Thompson
  • 38,370
  • 19
  • 110
  • 156
Thomas BDX
  • 2,632
  • 2
  • 27
  • 31
  • You don't need to play with permissions, just copy the file and empty old file. "cp access.log access.log.1 && > access.log" – Entropyk Sep 24 '15 at 12:55
  • 1
    @CoolArts copying a 200GB log files takes time and requires an extra 200GB lying around (from the question) and that also leaves the issue that apache keeps writing to the log file while it's being copied, and emptied, so potential loss of data. – Thomas BDX Sep 24 '15 at 17:53
  • Why can't we just delete and touch a new access file like in some of the answers below? – CodeFinity Sep 20 '16 at 13:10
  • @VisWebsoft, that's a good question: when you delete a file in *nix systems, if the file is used by another process, the space is not released until that process terminates or releases file handle. See this UNIX SE question for a practical use case : http://unix.stackexchange.com/q/68523/43548 – Thomas BDX Sep 20 '16 at 19:50
7

If you have access by SSH to the server, then you can:

1) Gzip the old file (text files compression ratios are really good). If we assume the file is named /var/log/apache/access.log then do this:

gzip -c /var/log/apache/access.log > /var/log/apache/access.log.gz

2) Clear the current file

echo > /var/log/apache/access.log

3) Restart apache

Also as Dez has suggested consider using logrotate for production grade apache log archiving.

knx
  • 340
  • 2
  • 13
5

Use the logrotate daemon in order to have a clean maintenance of your logs, specially, the apache related logs.

A brief info about logrotate: http://www.scriptinstallation.in/logrotate.html

Dez
  • 5,702
  • 8
  • 42
  • 51
2

If on Ubuntu do:

sudo su
cd /var/log/apache2
rm access.log
rm error.log
touch access.log

When creating that access log it magically starts the error log too.

malhal
  • 26,330
  • 7
  • 115
  • 133
2

I know this post is ages old, but I just had same problem and no answer covers it correctly.

The point is the apache creates the file as access_log, according to its configuration. However, logrotate only looks for *.log, hence the name does not match the search pattern.

Solutions: Either you add *_log to logrotate configuration, or change the apache configuration to make it create the log file named access.log. Changing apache configuration requires apache reload.

rexkogitans
  • 279
  • 2
  • 17
1

A simple solution is to disable access_log, commenting only one line on the configuration file.

Source: https://www.mydigitallife.info/how-to-disable-and-turn-off-apache-httpd-access-and-error-log/

For Plesk users: https://stackoverflow.com/a/41000240/1792240

Community
  • 1
  • 1
Davide
  • 1,635
  • 1
  • 16
  • 29
0

Rename the file to different filename and create new file with the name access_log and restart apache (otherwise apache keeps the lock on the file and don't "see" the file change)

Thomas BDX
  • 2,632
  • 2
  • 27
  • 31
YOU
  • 120,166
  • 34
  • 186
  • 219
0

Rename the file, create a new access_log, then restart Apache.

Liam
  • 19,819
  • 24
  • 83
  • 123