Alireza
Alireza

Reputation: 6848

What's the difference between handling gzip with PHP and Apache?

How can we handle compression in both? Whose responsibility is it? Which one is better, using apache or php to compress files?
PHP compression code:

ob_start("ob_gzhandler");

Or the apache one:

AddOutputFilterByType DEFLATE text/html text/plain text/xml 

Is this right that requests first reach in apache then to PHP? If answer is positive so can we infer that we should use the apache one?

Upvotes: 2

Views: 863

Answers (5)

Geoffrey Brier
Geoffrey Brier

Reputation: 799

In my company we usually use gzip compression on static resources. Apache asks PHP to process those resources (if necessary), then it compresses the output result. I would say that it is faster in theory (C & C++ are faster than PHP) and 'safer' to use Apache compression.

NB: Safer here means that the whole page is going to be compressed while you can forget to compress part of your web page with the ob_start function.

Upvotes: 3

bkwint
bkwint

Reputation: 626

I don't see why any of the two should be faster but do keep in mind that apache can also do the compression for css files/js files... You don't want to parse these files with php to compress them before you deliver them to the browser.

So I would suggest use the apache method.

Upvotes: 3

user1189402
user1189402

Reputation:

Well here is what I know, presented in a pros and cons way. Apache:

  • The .htaccess code will always be executed faster, because servers cache .htaccess files by default.
  • With .htaccess, you can define custom rules for individual folders and the server will automatically pick them up
  • With PHP, you cannot write everything in once place. There are many other things your .htaccess should have, besides compression:

  • A charset, expiry/cache control, most likely a few URL re-write rules, permissions, robot(Googlebot etc) specific stuff.

As far as I know, you cannot do all of this solely with PHP, and since you may need to get all of this done, I don't see why you should combine both of them. I have always relied on .htaccess or server level configuration to control the above enumerated aspects, and rarely ever had a problem.

PHP:

  • perhaps a bit more hassle free. With .htaccess files on shared hosting planes you are rather limited and you might run into tedious problems.
  • Some servers won't pick up certain commands, some(like 1and1) have a default configuration which messes with your settings(and nerves).
  • probably easier to use for someone who is less of a tech person

Overall, Apache is the winner. That's what I would go with all the time!

Upvotes: 4

AMIB
AMIB

Reputation: 3430

Apache is better since it prevents memory limit errors of php and acts faster because of compiled code vs interpreted code in php and also it is more meaningful to do compression in a different layer than php

Upvotes: 2

Explosion Pills
Explosion Pills

Reputation: 191729

You would have to run your own tests to see which is faster, but I don't believe there should be any difference in how the content is served. Using PHP, you have to handle the output buffering on your own which may be more difficult. It's more transparent with the apache method.

Upvotes: 2

Related Questions