Re: mod_pagespeed and PHP

1,619 views
Skip to first unread message

Shawn Ligocki

unread,
Nov 22, 2011, 10:04:02 AM11/22/11
to mod-pagesp...@googlegroups.com
You need to get PHP to set the Content-Type to text/html. Otherwise, mod_pagespeed won't even be called for that file.

-Shawn


On Tue, Nov 22, 2011 at 10:00 AM, Ignacio Santolin <ignacio....@gmail.com> wrote:
I'm trying to run the mod_pagespeed on a server using php scripts but
only works with static html. As I can make it work with php files?

Joshua Marantz

unread,
Nov 22, 2011, 10:05:04 AM11/22/11
to mod-pagesp...@googlegroups.com
It should work with PHP files that generates HTML, as long as the HTML is marked with content-type: text/html.

-Josh

Shawn Ligocki

unread,
Nov 22, 2011, 10:09:53 AM11/22/11
to mod-pagesp...@googlegroups.com
Some other users have fixed this by adding

in pagespeed.conf
AddOutputFilterByType MOD_PAGESPEED_OUTPUT_FILTER application/x-httpd-php
(Note: This will try to parse all files of type application/x-httpd-php as HTML.)

or

SetOutputFilter MOD_PAGESPEED_OUTPUT_FILTER
(Note: This will replace all other OutputFilters you may have set and will try to parse everything as HTML.)

-Shawn

Shawn Ligocki

unread,
Nov 22, 2011, 10:17:12 AM11/22/11
to mod-pagesp...@googlegroups.com
Actually, the right way to do this is probably to setContentType in PHP:


Let me know if that works.

-Shawn

Ignacio Santolin

unread,
Nov 22, 2011, 10:22:07 AM11/22/11
to mod-pagesp...@googlegroups.com
none of the options work properly.
A simple example ... I created a index.html file and works correctly when I change the extension to php module does not recognize the content-type text/html even though the output of it php is text/html

Ignacio Santolin

unread,
Nov 22, 2011, 10:46:47 AM11/22/11
to mod-pagesp...@googlegroups.com
i using the default Apache + PHP installation for Ubuntu 11.10

Shawn Ligocki

unread,
Nov 22, 2011, 10:58:18 AM11/22/11
to mod-pagesp...@googlegroups.com
Did you try using PHPs setContenType?

We have seen this issue several times on the list and generally the issue is that webmasters are setting headers with mod_headers instead of in mod_php. mod_headers runs after mod_pagespeed (and after the contents have been compressed, etc.) so we cannot see the results of that filter.

-Shawn

Ignacio Santolin

unread,
Nov 22, 2011, 1:29:22 PM11/22/11
to mod-pagesp...@googlegroups.com
<?php
HttpResponse::setContentType('text/html');
echo "Hello";
?>
mod_headers disabled

(same result)

index.php (firebug output)
Encabezados de la respuesta
Accept-Ranges    bytes
Connection    Keep-Alive
Content-Type    text/html
Date    Tue, 22 Nov 2011 18:12:19 GMT
Keep-Alive    timeout=5, max=100
Server    Apache/2.2.20 (Ubuntu)
Transfer-Encoding    chunked
X-Powered-By    PHP/5.3.6-13ubuntu3.2

Encabezados de la petición
Accept    text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Charset    ISO-8859-1,utf-8;q=0.7,*;q=0.7
Accept-Encoding    gzip, deflate
Accept-Language    es-ar,es;q=0.8,en-us;q=0.5,en;q=0.3
Cache-Control    max-age=0
Connection    keep-alive
Host    localhost
User-Agent    Mozilla/5.0 (Ubuntu; X11; Linux i686; rv:8.0) Gecko/20100101 Firefox/8.0

Shawn Ligocki

unread,
Nov 22, 2011, 1:40:12 PM11/22/11
to mod-pagesp...@googlegroups.com
Hm, that's very strange. Thanks for testing that with mod_headers explicitly disabled.

Do you get anything in your Apache logs that might be relevant? Specifically, do you get any mention of index.php from mod_pagespeed logs?

When I get a chance I'll try setting up a default Ubuntu machine with mod_php and mod_pagespeed to test this myself.

-Shawn

Ignacio Santolin

unread,
Nov 22, 2011, 1:52:03 PM11/22/11
to mod-pagesp...@googlegroups.com
enabled apache modules:
alias
auth_basic
authn_file
authz_default
authz_groupfile
authz_host
authz_user
autoindex
cgi
deflate
dir
env
mime
negotiation
pagespeed
php5filter
reqtimeout
setenvif
status

no mention about mod_pagespeed or index.php in error.log

Shawn Ligocki

unread,
Nov 22, 2011, 3:30:02 PM11/22/11
to mod-pagesp...@googlegroups.com
Ah, also, mod_pagespeed will only rewrite a file if it's first char is a "<"

It appears your file is:

Hello

Can you change it to:

<html><body>Hello</body></html>

or something similar?

-Shawn


On Tue, Nov 22, 2011 at 1:29 PM, Ignacio Santolin <ignacio....@gmail.com> wrote:

Ignacio Santolin

unread,
Nov 22, 2011, 4:12:06 PM11/22/11
to mod-pagesp...@googlegroups.com
I changed the contents of the PHP file what you specified and the same error... Still occurs when the file extension is PHP ... it works correctly if rename the extension to HTML. Not support PHP

Shawn Ligocki

unread,
Nov 29, 2011, 5:49:29 PM11/29/11
to mod-pagesp...@googlegroups.com
Alright, I fired up fresh Ubuntu 10.04 32bit machine and this worked fine for me.

Here's what I did:

$ sudo apt-get install apache2 php5 libapache2-mod-php5
$ sudo dpkg -i mod-pagespeed-beta_current_i386.deb
$ sudo apt-get -f install
$ sudo /etc/init.d/apache2 restart
$ cat /var/www/test.php 
<?php
header('Content-Type: text/html')
?>
<html>
  <body>
    <p>Hello, world!</p>
    <p><? print(Date("F d, Y")); ?></p>
    <?php echo "<style>  p { color: blue; } </style>"; ?>
  </body>
</html>

* About to connect() to 172.25.36.185 port 80 (#0)
*   Trying 172.25.36.185... connected
* Connected to 172.25.36.185 (172.25.36.185) port 80 (#0)
> GET /test.php HTTP/1.1
> User-Agent: curl/7.19.7 (x86_64-pc-linux-gnu) libcurl/7.19.7 OpenSSL/0.9.8k zlib/1.2.3.3 libidn/1.15
> Host: 172.25.36.185
> Accept: */*
< HTTP/1.1 200 OK
< Date: Tue, 29 Nov 2011 14:48:31 GMT
< Server: Apache/2.2.17 (Ubuntu)
< X-Powered-By: PHP/5.3.5-1ubuntu7.3
< X-Mod-Pagespeed: 0.10.19.3-1179
< Cache-Control: max-age=0, no-cache, no-store
< Vary: Accept-Encoding
< Content-Length: 127
< Content-Type: text/html
<html>
  <head/><body>
    <p>Hello, world!</p>
    <p>November 29, 2011</p>
    <style>p{color:#00f}</style>  </body>
</html>
* Connection #0 to host 172.25.36.185 left intact
* Closing connection #0

Notice the X-Mod-Pagespeed header was added and <style> was rewritten.

Is this comparable to what you did? I know you are using Ubuntu 11.10, but that shouldn't make much of a difference.

Can you try using the header() function? I couldn't get HttpResponse::setContentType() to work.

-Shawn

S B

unread,
Nov 30, 2011, 11:27:40 PM11/30/11
to mod-pagesp...@googlegroups.com

Hi,

 

I’m also new to using mod_pagespeed and I’m trying to get it to work with my PHP site. I tried Shawn’s example below and it worked exactly as he said it should (so it is installed on my server correctly).

 

I’m testing it against my index.php and while it is ‘optimizing’ it, I’m actually seeing only a marginally better performance and I’m not sure why that’s happening.

 

https://browsermob.com

BEFORE

90 objects         720 KB 5.43s

 

AFTER

91 objects         677 KB 5.38s

 

http://tools.pingdom.com

 

BEFORE

Tested from Dallas, Texas, USA on December 1 at 08:12:32

Perf. Grade       81/100

Requests          88

Page size          774.5kB

Load time         1.98s

 

AFTER

Tested from Dallas, Texas, USA on December 1 at 07:40:49

Perf. Grade       81/100

Requests          89

Page size          746.2kB

Load time         2.63s

 


Shawn Ligocki

unread,
Dec 1, 2011, 10:58:07 AM12/1/11
to mod-pagesp...@googlegroups.com
Can you send a URL? There are many reasons that it might not optimize your site much.

-Shawn

S B

unread,
Dec 1, 2011, 11:22:06 AM12/1/11
to mod-pagesp...@googlegroups.com

Sure. I’ve set it up on a test site - http://www.lildevelops.com/ - but it is temporarily disabled. Do you want me to re-enable it?

Shawn Ligocki

unread,
Dec 1, 2011, 11:34:02 AM12/1/11
to mod-pagesp...@googlegroups.com
Yes, that would be great.

-Shawn

S B

unread,
Dec 1, 2011, 1:57:58 PM12/1/11
to mod-pagesp...@googlegroups.com

 

Kinda crazy – but I switched it on about an hour back and restarted apache, but I only see it working on the subdomains, but not the main www one! See: http://test1.lildevelops.com/

Shawn Ligocki

unread,
Dec 1, 2011, 2:10:00 PM12/1/11
to mod-pagesp...@googlegroups.com
Well I can't debug something I can't see :)

So let me know if you can get a test URL that fails, so I can look at it.

-Shawn

S B

unread,
Dec 1, 2011, 3:12:19 PM12/1/11
to mod-pagesp...@googlegroups.com

 

I guess the problem is different from what I initially thought it was – mod_pagespeed is working fine on the subdomains (and being effective), but not all on the main www domain.

 

Do you have any idea why that might be happening?

Naomi Forman

unread,
Dec 1, 2011, 3:20:10 PM12/1/11
to mod-pagesp...@googlegroups.com
S,

Thank you for clarifying what the problem is.  I think we're on the same page now.

Unfortunately, we can't help you figure out *why* it's happening without some more information.

Could you please send your pagespeed.conf file?  Could you also please send your error logs?

Thanks!
-Naomi

S B

unread,
Dec 1, 2011, 3:39:16 PM12/1/11
to mod-pagesp...@googlegroups.com

Hi Naomi,

 

Thanks for replying. I’ve attached my pagespeed.conf file.

 

tail -f /var/log/httpd/error_log shows unrelated errors:

 

[Thu Dec 01 20:32:23 2011] [error] [client 59.178.163.42] File does not exist: /var/www/html/beta/video_files/intro/splash_519947884_Desert.jpg, referer: http://www.lildevelops.com/

[Thu Dec 01 20:32:30 2011] [error] [client 59.178.163.42] File does not exist: /var/www/html/beta/video_files/intro/splash_519947884_Desert.jpg, referer: http://www.lildevelops.com/

[Thu Dec 01 20:32:32 2011] [error] [client 59.178.163.42] File does not exist: /var/www/html/beta/video_files/intro/overlay_655776117_Chrysanthemum.jpg, referer: http://www.lildevelops.com/

 

Note that the www.lildevelops.com application is located in /var/www/html/beta/ (and it doesn’t work)

 

And the others (on which pagespeed works) are located in /home/<name>/public_html/

 

 


pagespeed.conf

Ignacio Santolin

unread,
Dec 4, 2011, 10:17:16 PM12/4/11
to mod-pagesp...@googlegroups.com
anything new?

Shawn Ligocki

unread,
Dec 5, 2011, 11:23:43 AM12/5/11
to mod-pagesp...@googlegroups.com
You guys seem to have moved this discussion to a different subject ("mod_pagespeed on a PHP site leads to minimal improvement"), responding there.

-Shawn


On Sun, Dec 4, 2011 at 10:17 PM, Ignacio Santolin <ignacio....@gmail.com> wrote:
anything new?

Shawn Ligocki

unread,
Dec 5, 2011, 11:29:41 AM12/5/11
to mod-pagesp...@googlegroups.com
Howdy,

Your pagespeed.conf doesn't reveal anything. The only guess I have is that for some reason where you are serving the content from is causing the differences ... but I can't think of a reason as to why. If you can send me your entire log (or at least a large section including mod_pagespeed messages) that might help. But I really have no idea why this would be working for one vhost but not others.

Acutally, just to check, you are running these sites as multiple vhosts on one Apache config, right? Where do you include the pagespeed.conf file? In the main config or in the vhosts themselves? This may just be a vhost configuration issue ... maybe.

-Shawn

S B

unread,
Dec 6, 2011, 5:12:53 AM12/6/11
to mod-pagesp...@googlegroups.com

>>> Do you have any ModPagespeed directives in the <VirtualHost> sections?

 

No – not at all.

 

>>> If you can send me your entire log (or at least a large section including mod_pagespeed messages) that might help.

 

There are some error messages being generated which look like this:

[Tue Dec 06 06:37:55 2011] [error] [mod_pagespeed 0.9.18.7-900 @28011] /var/www/mod_pagespeed/cache/sX0sHMkrUWR7DPcapcOS.lock:0: failed to stat (code=2 No such file or directory)

[Tue Dec 06 07:51:03 2011] [error] [mod_pagespeed 0.9.18.7-900 @10158] /var/www/mod_pagespeed/cache/EvSIAgJja7fj9j2G1K7t.lock:0: failed to stat (code=2 No such file or directory)

 

All of them are similar – the only change in all these lines is the datetime, the number after the @, and the filename.lock.

 

Viewing it in context of the surrounding lines, I think its happening because of an unrelated issue in our mod_rewrite rules wherein files are being searched for in folders which do not exist.

...

[Tue Dec 06 06:37:52 2011] [error] [client 50.22.133.58] File does not exist: /home/test1/public_html/codelibrary/css/indicator.gif

[Tue Dec 06 06:37:55 2011] [error] [mod_pagespeed 0.9.18.7-900 @28011] /var/www/mod_pagespeed/cache/sX0sHMkrUWR7DPcapcOS.lock:0: failed to stat (code=2 No such file or directory)

[Tue Dec 06 06:40:40 2011] [error] [client 66.249.71.19] PHP Notice:  A session had already been started - ignoring session_start() in /home/test1/public_html/codelibrary/inc/config.php on line 14

[Tue Dec 06 06:43:29 2011] [error] [client 66.249.71.19] PHP Notice:  A session had already been started - ignoring session_start() in /home/test1/public_html/codelibrary/inc/config.php on line 14

[Tue Dec 06 06:46:18 2011] [error] [client 50.22.133.58] File does not exist: /home/test1/public_html/codelibrary/css/indicator.gif

...

[Tue Dec 06 07:51:01 2011] [error] [client 50.22.133.58] File does not exist: /home/test1/public_html/codelibrary/images

[Tue Dec 06 07:51:03 2011] [error] [mod_pagespeed 0.9.18.7-900 @10158] /var/www/mod_pagespeed/cache/EvSIAgJja7fj9j2G1K7t.lock:0: failed to stat (code=2 No such file or directory)

[Tue Dec 06 07:53:30 2011] [error] [client 122.180.148.196] File does not exist: /home/design1/public_html/content_files

...

 

>>> Acutally, just to check, you are running these sites as multiple vhosts on one Apache config, right? Where do you include the pagespeed.conf file? In the main config or in the vhosts themselves? This may just be a vhost configuration issue ... maybe.

 

Yep – these sites are multiple vhosts within on Apache’s httpd.conf file. I’m not sure how the two are linked – I installed mod_pagespeed and didn’t do anything after that.

 

I’ll send the httpd.conf file to you directly if you don’t mind.

Reply all
Reply to author
Forward
Message has been deleted
0 new messages