Enable gzip compression in cPanel

Using gzip compression allows you to deliver website content faster as it can be gzipped on the server and uncompressed on the client, reducing the file size you need to transfer.

Unfortunately mod_deflate, the Apache module required to do this, is not enabled on all cPanel installs. However, if it is, or you have access to the server, you can easily enable it.

Enabling mod_deflate

If you do not have mod_deflate, you need to use EasyApache to add it. Log in to Web Host Manager and go to EasyApache (only server admins will be able to do this). Select build from the previous configuration and customise it until you get to the exhaustive options list.

Check the box next to mod_deflate and then re-build Apache.

Enabling compression

Once you have mod_deflate enabled, cPanel will have a new option. Under “software and services” in the x3 skin you fill find an option called “optimise website”. Click through to that page.

Compress content will probably be set to “disabled”.

Select “Compress the specified MIME types” instead. You could enable it for all content but I would not recommend this as some content you will not want to compress and much of it (images for example) is pretty pointless. The third option allows you to customise.

By default it should have the following options:

text/html text/plain text/xml

I recommend adding some more:

text/html text/plain text/xml text/css text/javascript

Hit “update settings” and you are done!

Saving files in memory

If you need super quick access to a file, for example a log file which isn’t going to be too big but it being used by a script which is time critical, then rather than writing it to disk, you can mount part of your file system in memory and write to it there.

This has the disadvantage that when you restart your system, you will lose the data. But for test scripts, logs or other temporary files that you don’t mind getting lost, it can really speed up performance.

Luckily, most systems come with a an area mounted in memory already – so you don’t even need to configure it!

cd /dev/shm

If said directory exists, you’ll have a memory mounted directory already and can start using it immediately.

Profiling SQL queries with EXPLAIN

If you have a complex SQL query, you might find that performance isn’t exactly ideal. Worse still, you don’t actually know which part of the query it is that is actually taking so long.

Luckily, MySQL comes to the rescue with the ability to explain.

All you have to do is start your query with the keyword EXPLAIN and MySQL will, rather than returning you a recordset of results, will instead provide a break down of everything it has done, including how it made the table joins and what order it did everything in.

EXPLAIN SELECT a INNER JOIN b ON a.col1 = b.col2 WHERE a.col1 > 1 AND b.col2 > 2;

Speeding up inserts with INSERT DELAYED

If your insert statements are not time critical, you can use insert delayed in your SQL to speed things up. The syntax is as follows.

INSERT DELAYED table (col1, col2) VALUES ('a', 'b');

Insert delayed can be used with MyISAM and Memory, but cannot be used with InnoDB.

The advantage of using insert delayed is that the MySQL server returns a success message straight away so the script can keep going, without it actually having to do the insert. This allows the MySQL server to carry it out when it isn’t busy, and do several at the same time.

It’s appropriate for tables such as logs tables where it doesn’t matter too much if they don’t go in straight away.