Troubleshooting performance problems and solving the dreaded Internal Server Error

A user on ProNet recently posted this problem:

We're helping a client build a new site on MT 4.34, with Professional Pack and the More Custom Fields plug-in.

As we've been adding content we're getting "Internal Server Error" messages more and more frequently usually when rebuilding individual entries. It only happens occasionally when republishing an index template on its own, and I just rebuilt 12 entries in a single category at once without getting the error.

I don't believe this is a permissions issue because I haven't changed any of the MT files for several weeks, and this problem is new.

Any pointers where to start troubleshooting?

We're running on one of Media Temple's DV accounts.

Internal Server Errors absolutely suck because the bury any indication of the root cause. This is nice from a security standpoint, but for developers, it is a massive inconvenience. So how to solve the Internal Server Error.

First of all, the cause of the Internal Server Error can be many. If the error is occurring during the publishing process of Movable Type or Melody, then this article is for you. If you are getting the error when simply trying to access the CMS, then something else is likely to be the matter and you should move on now to a better article to help you.

Now, if the problem is happening while publishing is taking place then it is almost certain that the error is being triggered by the web server timing out the connection. This is happening because Movable Type is taking too long to respond, or in other words, a template is taking too long to publish. There are several reasons why this could be happening:

  1. The template is making use of a plugin or service that goes out over the open Internet to retrieve content. If your connection is slow, or if your firewall prevents these connections, then this added latency can be bogging the template down.

  2. Your template has an infinite loop. If you have a template that includes a template module that includes itself, just as one example, then you could be publishing something forever. Literally.

  3. Your template code is really inefficient. Most of the time if things are timing out trying to publish they are trying to pull too much data from the database. This could be an "unbounded Entries tag for example common in a lot of archive templates or in a Google Sitemap for example.

So how do you go about diagnosing the problem? Here are some tips:

  1. Install the Template Profiler plugin from Endevver. This will allow you to profile your entry template to see if there is a template tag in particular that is demanding too much memory or resources. It is generally easy to find the outliers.

  2. Install the Template Optimizer plugin from Endevver. This can be used to recommend and then apply a set of changes to your templates to help optimize their publishing settings. It scans index templates, modules and widgets.

As for the possibility that Media Temple is at fault. I doubt it. Although users of shared hosts should be on the look out for special scripts that periodically wake up and kills processes that are consuming too many resources. Media Template dedicated virtual servers however do not have processes that reep resource hogs. That is because resources are managed entirely by the virtualization layer rendering such scripts unnecessary. So I don't think that is happening. Plus, dv instances have plenty of resources for Movable Type.

In lieu of doing the above, one thing you can do is wrap sections of code in <mt:ignore> tags to comment them out and isolate which portion of your template is generating the performance problem.

We hope this helps. Obviously, if you have trouble doing this on your own, Endevver is happy to help. Just give us a ring.