Some Apache, PHP & WordPress tuneage.

After enabling a few plugins, the WordPress installation started feeling a little sluggish. With a little bit of googling you’ll see this is a common complaint with WP sites. While I don’t have to worry about having anything digged, apart from possibly some embarassing photos, it’s still a fun project to optimize your server which lead me onto tonights project.

Tuning Apache

There’s a few things you can do to get the most of your Apache installation, although it’s highly tuned out of the box.

You might want to consider disabling some unused modules, if you have more than you need running and consuming resources. All I did here was clean out my virtual host configuration files to improve readability. I removed the cgi-bin and doc references as they’re not needed.

Enable content caching of static content

What do I mean by static content? Static content can be classed as resources that are not created dynamically, like images, text files, CSS whilst dynamic content would be things like PHP or ASP scripts.

First of all we need to enable the Apache module ‘cache’ by running:

a2enmod cache

Some examples:

Add the following to either your main Apache configuration file, or a virtual host file to enable in-memory caching of static content for some of the common cacheable resource types (HTML,CSS,JPEG,GIF)

CacheEnable mem /
ExpiresActive on
ExpiresDefault "access plus 1 month"
ExpiresByType text/css "access plus 1 month"
ExpiresByType text/html "access plus 1 month"
ExpiresByType image/jpeg "access plus 1 month"
ExpiresByType image/gif "access plus 1 month"

For a syntax guide see the mod_expires documentation and go to wikipedia for a list of MIME types.

Put your pages on a diet with mod_deflate

Mod deflate is the Apache module responsible for compressing your static content before it’s transmitted down the wire to browsers that are capable of handling compressed files (most are).

On the one hand this will increase CPU usage but if you’ve got the spare cycles, and it’s not like it’s going to bring your server to it’s knees, then you should probably do this as it should mean reduced bandwidth usage on your site, and faster response times.

a2enmod deflate

Make sure Apache still sends out ‘304 Not Modified’ HTTP headers

A problem with using mod_deflate is that it appends -gzip to the end of any E-Tags outside of the quoted string, contrary to the HTTP specification. This means that we always receive 200OK responses to our GET requests along with the full body content, effectively bypassing the caching. What we really want is the server to return a ‘304 Not Modified’ response, meaning it doesn’t need to send the content again.

To accomplish this, we need to add the following rules to our apache.conf:

RequestHeader  edit "If-None-Match" "^(.*)-gzip$" "$1"
Header  edit "ETag" "^(.*[^g][^z][^i][^p])$" "$1-gzip"

Side note: this also means you need to enable the module ‘headers’ with:

a2enmod headers

Tune PHP

Install E-Accelerator

E-Accelerator, formerly Turk MMCache, improves the performance of your PHP scripts by caching them in a compiled state in shared memory meaning they don’t have to be recompiled every time a page is requested.

Tune WordPress

Install WP Super Cache plugin

This one is pretty self explanatory… you just need to install the plugin from your administrative control panel. Well okay, there are a few extra steps. Make sure you have ‘permalinks’ enabled under “Settings -> Permalinks” and then enable the cache in the “WP Super Cache” settings page.

This should drastically speed up your site. It creates static pages for all of your lovely WordPress content, and serves that with an .htaccess rule rather than regenerating the content on the fly, reducing CPU load and page load times.

You might be wondering why we need to do this if we have enabled caching in Apache. Well, the reason is that Apache can’t cache dynamically generated content – that is, content generated by a scripting language like PHP. WP-Supercache creates static versions of our WordPress pages that can be cached, and also saves the script from having to be recompiled in PHP.

Restoring ‘lost’ offline files

So… I was working quite happily on a project I kicked off lastnight, and Visual Studio defaults to saving your project data in “My Documents”. In my particular environment, this is syncronized with Offline Files to a network share.

Feeling pretty pleased with myself I logged in in the morning to take another look but then realised I had to quickly shut down the system. Something went wrong with the syncronization – it didn’t happen.

Getting to work, all of lastnights files were missing as the sync must have happened in the opposite direction(!)
Later on in the day after I’d given up hope and begun to think about how quickly I could rewrite the app, the “Offline Folders” window pops up and in it I see my source files… with “Only local copy exists” (where?)

A bit of googling/headscratching later and I found that you can restore these files.

using csccmd.exe “Client-Side Caching Command-Line Options command-line tool” (!) you can extract files with the status “Only local copy exists” with the following command:

csccmd.exe /extract /target:restoredirectory /recurse /onlymodified

This will scan through the offline files and restore them to a directory of your choice. I’m thinking that other versions of your files or archived offline files must be stored in some kind of binary locally, as a Google Desktop search revealed not a trace of the sourcefiles I was looking for.

….Lesson in this is to commit to Subversion, even if you’ve just started something or don’t think there’s much to put in there yet….

With thanks to JacksonTechnical

Eclipse proxy authentication problems

So… I spent a good half hour today trying to figure out why eclipse wouldn’t connect to the android SDK update site.

Proxy host/port… check.
Proxy authentication… check.
The site exists… check.

No wait, last time I was in there I was sure I’d clicked ‘save’… why isn’t it saving my username and password. I’ve had this problem before when I first started at Orange… took me a good day or two to get my environment fully set up.

After filling it in again and going around the same circle for a little while, and monkeying around with the secure storage section I came across this:

Adding this line to your eclipse.ini will solve the issue:

In a few words the above command says that Eclipse can access the web via the *.pac files of the HTTP clients (eg Internet Explorer or Firefox). You’ll still need to enter your proxy settings again, one final time…

With thanks to ‘Comments after the EOF’:

Updating the GUI from another thread made easy


I’ve found out to my absolute horror that even simple operations done in a background thread that need to update the interface are required to force those interface calls back into the same thread as the interface was generated in…

With a bit of research, I found out that this was done with the Invoke method. Initially, I created what felt like hundreds of delegates/functions to handle each control’s update, but now, although this solution I have posted could be better, it is a decent timesaver at least for me, so hopefully it will help someone else…

Basically, what we have below is a static class (ThreadSafe.cs) that has some delegates such as SetText(Control, string) that lets you set the text of any control with some text. The following example is rather basic, but there are numerous others in ThreadSafe.cs such as adding items to listviews, changing checkbox check states etc. Give it a look.

Here is a basic example for changing the Text property of a control.


ThreadSafe.SetText(this.whateverControl, "text to change");

couldn’t be easier.

The Delegate

public delegate void SetTextDelegate(System.Windows.Forms.Control ctrl, string text);

This defines the signature of the SetText method.

The Method

public static void SetText(System.Windows.Forms.Control ctrl, string text)
    if (ctrl.InvokeRequired)
        object[] params_list = new object[] { ctrl, text };
        ctrl.Invoke(new SetTextDelegate(SetText), params_list);
        ctrl.Text = text;

There are also classes and inherited classes for other controls, listviews, buttons, comboboxes etc., that should save you time writing your thread-safe GUI code. Hope this helps someone. If it does or for help, please leave a comment!

Download Helper Class

ThreadSafe helper