{"id":1051,"date":"2015-09-04T09:49:14","date_gmt":"2015-09-04T07:49:14","guid":{"rendered":"https:\/\/elkano.org\/blog\/?p=1051"},"modified":"2015-09-04T09:49:14","modified_gmt":"2015-09-04T07:49:14","slug":"installing-mod_pagespeed-cpanel-server","status":"publish","type":"post","link":"https:\/\/elkano.org\/blog\/installing-mod_pagespeed-cpanel-server\/","title":{"rendered":"Installing mod_pagespeed on a cpanel server"},"content":{"rendered":"<p>PageSpeed is a very popular module for Apache and Nginx developed by Google that can optimize a lot of aspects of your web server.<\/p>\n<p>&nbsp;<\/p>\n<h2>Installation<\/h2>\n<p>You can follow these steps to install on a server with cpanel:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"raw\" data-enlighter-linenumbers=\"false\" data-enlighter-theme=\"enlighter\">$&gt;\/usr\/local\/cpanel\/3rdparty\/bin\/git clone https:\/\/github.com\/pagespeed\/cpanel.git \/tmp\/pagespeed\/\r\n$&gt; cd \/tmp\/pagespeed\/Easy\r\n$&gt; tar -zcvf Speed.pm.tar.gz pagespeed\r\n$&gt; mkdir -p \/var\/cpanel\/easy\/apache\/custom_opt_mods\/Cpanel\/Easy\r\n$&gt; mv Speed.pm Speed.pm.tar.gz -t \/var\/cpanel\/easy\/apache\/custom_opt_mods\/Cpanel\/Easy\/\r\n$&gt; cd &amp;&amp; rm -rf \/tmp\/pagespeed\r\n<\/pre>\n<p>Now, you can go to EasyApache in your WHM and compile the module, you have to check Mod PageSpeed in the apache modules config page.<\/p>\n<h2>PageSpeed Configuration<\/h2>\n<p>The module configuration file is:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"raw\" data-enlighter-linenumbers=\"false\" data-enlighter-theme=\"enlighter\">\/usr\/local\/apache\/conf\/pagespeed.conf\r\n<\/pre>\n<p>To activate\/deactivate the module you can set this option on\/off:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"raw\" data-enlighter-linenumbers=\"false\" data-enlighter-theme=\"enlighter\">ModPagespeed on\r\n<\/pre>\n<h2>Tmpfs for the pagespeed cache<\/h2>\n<p>Due to the hight IOPS load that the cache may involve, a good idea is to put it in memory if you have enough memory in your server. This ensures that the cache accesses will be very fast.<\/p>\n<p>To configure a 1,5GB size tmpfs cache you have to add this line to your \/etc\/fstab file:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"raw\" data-enlighter-linenumbers=\"false\" data-enlighter-theme=\"enlighter\">\r\ntmpfs\t\t\t\/var\/mod_pagespeed\/cache tmpfs  rw,uid=99,gid=99,size=1500m,mode=0777 0 0\r\n<\/pre>\n<p>After that, simply run <strong>mount \/var\/mod_pagespeed\/cache<\/strong> command.<\/p>\n<p>The uid and gid must be the user that the web server uses to run.<\/p>\n<p>Some parameters to configure cache:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"raw\" data-enlighter-linenumbers=\"false\" data-enlighter-theme=\"enlighter\"> \r\n# Cache max size\r\nModPagespeedFileCacheSizeKb          768000 \r\n<\/pre>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"raw\" data-enlighter-linenumbers=\"false\" data-enlighter-theme=\"enlighter\"> \r\n# Cache Ionode limit\r\nModPagespeedFileCacheInodeLimit      500000 \r\n<\/pre>\n<p>The cache expires old entries every 10 minutes if the value in ModPagespeedFileCacheSizeKb or ModPagespeedFileCacheInodeLimit is reached:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"raw\" data-enlighter-linenumbers=\"false\" data-enlighter-theme=\"enlighter\">  ModPagespeedFileCacheCleanIntervalMs 600000 \r\n<\/pre>\n<p>If you want to disable pagespeed in some accounts you can do it through <b>.htaccess<\/b> file<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"raw\" data-enlighter-linenumbers=\"false\" data-enlighter-theme=\"enlighter\">&lt;IfModule pagespeed_module&gt;\r\n\tModPagespeed off\r\n&lt;\/IfModule&gt;\r\n<\/pre>\n<h2>Side Effects after installing PageSpeed on Cpanel<\/h2>\n<p>I got an unwanted side effect after installing PageSpeed on cpanel shared servers. The pagespeed module does its own http requests through its internal proxy to get the resources if them can not be found in the cache. These requests are originated from the server and the user-agent is something like &#8220;Serf\/1.1.0 mod_pagespeed\/1.9.32.4-7251&#8221;.<\/p>\n<p>These requests are logged in domains access logs in \/usr\/local\/apache\/domlogs and in the logs used to audit the bandwidth used by each domain, so inevitable leads in an increase in traffic accounted by each domain.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"raw\" data-enlighter-linenumbers=\"false\" data-enlighter-theme=\"enlighter\">\r\n192.168.1.10 - - [01\/Sep\/2015:14:52:26 +0200] &quot;GET \/style.css HTTP\/1.1&quot; 200 1861 &quot;http:\/\/yourdomain.com\/&quot; &quot;Serf\/1.1.0 mod_pagespeed\/1.9.32.4-7251&quot;\r\n<\/pre>\n<p>Solution: Don&#8217;t log these internal requests made by mod_pagespeed in the apache logs!<\/p>\n<h3>Setting up SetEnvIF module to detect pagespeed requests<\/h3>\n<p>We are going to use the SetEnvIF apache module to detect the specific traffic made by pagespeed and then set an env variable. We will use this variable later to avoid logging these requests.<\/p>\n<p>Edit pre_main_global.conf in cpanel and add the following configuration:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"raw\" data-enlighter-linenumbers=\"false\" data-enlighter-theme=\"enlighter\">\r\n\/usr\/local\/apache\/conf\/includes\/pre_main_global.conf\r\n<\/pre>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"raw\" data-enlighter-linenumbers=\"false\" data-enlighter-theme=\"enlighter\">&lt;IfModule mod_setenvif.c&gt;\r\n    SetEnvIFNoCase User-Agent &quot;^Serf\/0.* mod_pagespeed\/.*$&quot; dontlog\r\n    #SetEnvIf Remote_Addr &quot;IP_SERVER&quot; dontlog\r\n&lt;\/IfModule&gt;\r\n<\/pre>\n<p>Now, if the User-Agent used by pagespeed is detected the variable dontlog is set. You can set the variable too if the request is made by the server itself (2nd line).<\/p>\n<h3>Set virtualhosts logging depending on the donlog variable<\/h3>\n<p>Because Cpanel rebuilds apache configuration file (httpd.conf) in every compilation you won&#8217;t want to make the changes directly in httpd.conf file. Instead of that, you should change the templates provided by cpanel to be sure the changes will kept.<\/p>\n<p>Depending on the apache version you are running the templates are located in:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"raw\" data-enlighter-linenumbers=\"false\" data-enlighter-theme=\"enlighter\">\r\n\/var\/cpanel\/templates\/apache2_2\/\r\n\/var\/cpanel\/templates\/apache2_4\/\r\n<\/pre>\n<p>To make custom changes in the templates you have to make a local copy of the template you want to change and make the changes in it:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"raw\" data-enlighter-linenumbers=\"false\" data-enlighter-theme=\"enlighter\"># cp \/var\/cpanel\/templates\/apache2_4\/vhost.default \/var\/cpanel\/templates\/apache2_4\/vhost.local\r\n<\/pre>\n<p>Edit vhost.local template and add <strong>env=!dontlog<\/strong> to the lines begining with CustomLog word:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"raw\" data-enlighter-linenumbers=\"false\" data-enlighter-theme=\"enlighter\">[% IF logstyle == &#039;combined&#039; -%]\r\n    [%- IF !enable_piped_logs || !supported.mod_log_config -%]\r\n    CustomLog [% paths.dir_domlogs %]\/[% wildcard_safe(vhost.log_servername) %] combined env=!dontlog\r\n    [%- END %]\r\n[% ELSE %]\r\n    TransferLog [% paths.dir_domlogs %]\/[% wildcard_safe(vhost.log_servername) %]\r\n[% END -%]\r\n[% IF supported.mod_log_config &amp;&amp; supported.mod_logio &amp;&amp; !enable_piped_logs -%]\r\n    CustomLog [% paths.dir_domlogs %]\/[% wildcard_safe(vhost.log_servername) %]-bytes_log &quot;%{%s}t %I .\\n%{%s}t %O .&quot; env=!dontlog\r\n[% END -%]\r\n<\/pre>\n<p>This setting is telling apache not log anything if the dontlog variable is set.<\/p>\n<p>The last step you need to do is to rebuild the apache configuration file:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"raw\" data-enlighter-linenumbers=\"false\" data-enlighter-theme=\"enlighter\">\/scripts\/rebuildhttpdconf\r\n<\/pre>\n<p>Now, you can verify in your domains access logs that the pagespeed internal requests are not logged anymore.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>PageSpeed is a very popular module for Apache and Nginx developed by Google that can optimize a lot of aspects of your web server. &nbsp; Installation You can follow these steps to install on a server with cpanel: $&gt;\/usr\/local\/cpanel\/3rdparty\/bin\/git clone https:\/\/github.com\/pagespeed\/cpanel.git \/tmp\/pagespeed\/ $&gt; cd \/tmp\/pagespeed\/Easy $&gt; tar -zcvf Speed.pm.tar.gz pagespeed $&gt; mkdir -p \/var\/cpanel\/easy\/apache\/custom_opt_mods\/Cpanel\/Easy $&gt; [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[150,3],"tags":[48,151,152],"_links":{"self":[{"href":"https:\/\/elkano.org\/blog\/wp-json\/wp\/v2\/posts\/1051"}],"collection":[{"href":"https:\/\/elkano.org\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/elkano.org\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/elkano.org\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/elkano.org\/blog\/wp-json\/wp\/v2\/comments?post=1051"}],"version-history":[{"count":18,"href":"https:\/\/elkano.org\/blog\/wp-json\/wp\/v2\/posts\/1051\/revisions"}],"predecessor-version":[{"id":1069,"href":"https:\/\/elkano.org\/blog\/wp-json\/wp\/v2\/posts\/1051\/revisions\/1069"}],"wp:attachment":[{"href":"https:\/\/elkano.org\/blog\/wp-json\/wp\/v2\/media?parent=1051"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/elkano.org\/blog\/wp-json\/wp\/v2\/categories?post=1051"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/elkano.org\/blog\/wp-json\/wp\/v2\/tags?post=1051"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}