Install mod_pagespeed with DirectAdmin

In this blog I will describe how you can install mod_pagespeed in DirectAdmin. You can find more information about the module at the development website of Google. In short: it can improve the loading speed of your website. It can also cause errors on your website so I do not recommend to install and enable this for all shared hosting users.

If you are using mod_ruid you will notice that mod_pagespeed fails. Symptoms are missing javascript or css files. The reason is that mod_pagespeed has problems with writing to a directory. This it required to store compressed javascript files. But because every vhost uses the same mod_pagespeed tmp directory and every DirectAdmin user runs under it’s own username you will get permission errors. Do not worry, in this blog I help you with setting up mod_pagespeed.

Preparation

First we edit the vhosts. If we do not do this some web applications that uses .js and .css files will not work anymore because of the permission problems. Solving this problem is fairly easy: give every user it’s own directory for mod_pagespeed. We create /home/username/tmp/mod_pagespeed with user_create_post.sh. Create the file /usr/local/directadmin/scripts/custom/ :

#!/bin/sh
mkdir /home/$username/tmp/
mkdir /home/$username/tmp/mod_pagespeed/
chown $username:$username /home/$username/tmp/ -R # Configure the correct permissions
exit 0;

Run the following commands:

chown diradmin:diradmin /usr/local/directadmin/scripts/custom/user_create_post.sh
chmod 700 /usr/local/directadmin/scripts/custom/user_create_post.sh

If you already have the file user_create_post.sh you can add everything except #!/bin/sh and exit 0;

[alert type=”info” heading=”Existing users”]

The file user_create_post.sh is only used for new created users. Execute the following command to create the mod_pagespeed directory for existing users:

ls /usr/local/directadmin/data/users | awk '{system("mkdir /home/" $1 "/tmp; mkdir /home/" $1 "/tmp/mod_pagespeed; chown "$1":"$1" /home/"$1"/tmp -R ")}'

[/alert]

Now we have created the folders and fixed the permissions we need to tell Mod_pagespeed where to store the files for every vhost. We can do that in the vhost using the following setting:

ModPagespeedFileCachePath /home/username/tmp/mod_pagespeed

How do we do that? With the feature in DirectAdmin to create customized vhosts. First we copy the original vhosts to the custom directory. If you already have done this you can ignore this.

cd /usr/local/directadmin/data/templates/custom
cp ../virtual_host2.conf .
cp ../virtual_host2_secure.conf .
cp ../virtual_host2_secure_sub.conf .
cp ../virtual_host2_sub.conf .

Edit all virtual_host2*.conf files and add the following after “ServerAdmin |ADMIN|”:

<IfModule pagespeed_module>
ModPagespeedFileCachePath /home/|USER|/tmp/mod_pagespeed
</IfModule>

Now we tell DirectAdmin to rewrite every vhost:

echo "action=rewrite&value=httpd" >> /usr/local/directadmin/data/task.queue
/usr/local/directadmin/dataskq #rewrite the vhosts
/usr/local/directadmin/dataskq #restart Apache

Install Mod_pagespeed

Choose one of the following commands to install Mod_pagespeed:

CentOS/Fedora 32 bit
wget https://dl-ssl.google.com/dl/linux/direct/mod-pagespeed-stable_current_i386.rpm
rpm -ivh --nodeps mod-pagespeed-stable_current_i386.rpm
CentOS/Fedora 64 bit
wget https://dl-ssl.google.com/dl/linux/direct/mod-pagespeed-stable_current_x86_64.rpm
rpm -ivh --nodeps mod-pagespeed-stable_current_x86_64.rpm
Debian/Ubuntu 32 bit
wget https://dl-ssl.google.com/dl/linux/direct/mod-pagespeed-stable_current_i386.deb
dpkg -i mod-pagespeed-stable_current_i386.deb
Debian/Ubuntu 64 bit
wget https://dl-ssl.google.com/dl/linux/direct/mod-pagespeed-stable_current_amd64.deb
dpkg -i mod-pagespeed-stable_current_amd64.deb

When the installation has finished you only have to run this command:

service httpd restart

And you are done:-)

[alert]

Disable Mod_pagespeed

If a user does not want to use Mod_Pagespeed the user can create a .htaccess with the following code:

ModPagespeed off

This changes the settings immediately and does not require a restart of the webserver.

[/alert]

Leave a Reply

Your email address will not be published. Required fields are marked *