Gzip pre-compression

I'm using mod_deflate to compress my .css and .js files on the fly:

<ifmodule mod_deflate.c>
AddOutputFilterByType DEFLATE text/text text/html text/plain text/xml text/css application/x-javascript application/javascript
</ifmodule>

The problem is that this compresses the files on the fly, which takes some system resources when dealing with large files.

So I want to pre-compress my files so that they will be served directly to drop down server load. I want to store them with the other css and js files like style.css and style.gz then use php headers to check if the client accepts the gzip encoding to decide whether to serve the .css or the .gz file. FOR THE CHECK IT'S NOT THE PROBLEM. The problem is how to PRE-compress the files.

I came across some topics like this but none of them worked for me. It looks simple but it gave me a 500 Internal Server Error.

Is there any tool or whatever that does this job?


Solution 1:

You could write a simple bash script:

#!/bin/bash
for file in *.css *.js; do
  gzip -c $file > $file.gz;
done