Hosting Tutorial & Guide

Drupal Optimization - How to Optimize Drupal in Web Hosting

updated on Nov 16, 2016
Drupal Optimization - How to Optimize Drupal in Web Hosting We shared the experience and thoughts from a recent outsourcing order to help a client to performance tune their international hotel review website based on Drupal 6.1. Most of the web pages take 4 - 8 seconds to load on the production and take 3 - 6 seconds on the local environment with the same database as the local server is more powerful than the Drupal hosting server rent from GoDaddy. After our optimization, now most of the web pages on the production take less than 2 seconds to load which is over 150% faster than before.

The context of this website is that it has about 21,000 unique visits and 120,000 page views daily; and 220,000 hotels, articles, reviews; 100,000 registered users and half of them are active. The website allows a dozen of social activities such as tag, voting, reviews, subscription, RSS, single sign on from Facebook, Twitter, Google and Yahoo. And also it integrates with the external web services for booking and look up.

We used different views to load content under profiling and found the Drupal cache nearly didn't work when the user is authenticated because the most of web pages are customized per user, e.g. recommendation, tags, favorites and more. Except caching, the code execution was also not ideal. After one week profiling, log watching and investigation, we started to optimize the website from the following points mainly. Eventually we got the website more than 150% faster than before and surprised the customer as well.

Check the Server Configuration

Drupal 7 is a large application coming with a lot of powerful functionality which requires a fairly high level of server configuration to run smoothly and fast. Thus, we would like to talk about the APC code optimizer, which is in need in the server configuration. In short, the Alternative PHP Cache - a PHP accelerator extension designed to improve the performance of websites written in PHP. It's free. It works by caching the compiled binary code to avoid the overhead of compiling PHP source code for each web request. Furthermore, the PHP scripts are also cached in memory and executed there which significantly improves the performance on loading from hard disk and memory copying.

Check the Server Configuration Mostly, the minimum requirements of Drupal can be easily met by most web hosts, but that is just not enough. Drupal 7 requires a minimum of PHP 5.2.5, but PHP 5.3 or higher is recommended as Drupal definitely runs much better with latest versions of PHP. For memory requirements, Drupal 7 core requires 32 MB, while this is only sufficient for a default Drupal installation, excluding the use of any modules or something else. Typically, 128 MB or 256 MB memory is the best option, especially when your website includes rich media.

Besides PHP, Apache, MySQL versions, suPHP are also concerns. For Drupal 7, suPHP, and Apache 2.x are recommended. And in terms of the database configuration, Drupal 7 supports several databases, but MySQL 5.1.30 or higher is recommended.

If you don't want to deal with the mess brought by improper server configuration, make sure that your web host has exceeded or at least fully met all of these requirements. If the company cannot provide what you need, then you'd better cut your losses by getting away from it and moving to another web hosting company.

In case that you have no idea where to go, we recommend InMotion Hosting, one of the most reliable web hosts around the world. The web hosting packages are now priced from $3.49/mo for visitors going through the promotional link below, coming with 1-click Drupal 7.22 installation, and the following sufficient server configuration.
  • PHP 5.3.x, 5.4.x, suPHP & 256 MB memory_limit
  • MySQL 5.5 or higher
  • Apache 2.2.23

InMotion Hosting Promotional Link Activation

Enable Caching

When visitors come to your Drupal website, queries are made to the database to load the website contents. Without caching, every time a visit happens to your website, the database needs to deal with many queries, which inevitably slows down your site especially when there is a sudden large traffic brought by search engines or news websites.

At this time, you need to enable caching. When caching is allowed, much static information shown on your Drupal website, such as the images, CSS files and scripts, will be stored in a series of special files designed to hold cached data. Then, when a visitor wants to load up your website, much less requests will be made to the database due to the availability of the pre-stored information. This way, your website can be shown to the visitor much faster.

Enble Caching Usually, the website was using Views module to render the lists of articles, hotels, reviews and other entries based on the sorting and filtering. It had a customized template named EntryListTemplate.php which has more than 10 accesses to the database per one request without any cache. As we didn't have time and budget to optimize the database access by writing store procedure or reducing the database calls, we only added the cache on this template based on the sorting and filtering criteria. Fortunately, with the high volume visits on this kind of pages, the total performance was increased a lot also in practice.

In regards to how to enable caching, the whole process is quite easy. Drupal has a built-in cache system which you can enable by simply going to Administer>Site configuration>Performance, reading the options for page cache, CSS optimization, JavaScript optimization, and then checking the boxes next to Cache pages for anonymous users, Cache blocks, Compress cached pages, Aggregate and compress CSS files, and Aggregate JavaScript files.

Besides the default caching option in Drupal 7, you can also take advantage of some other helpful modules that work well for caching for different kinds of visitors. The following 2 caching modules are highly recommended.
  • Boost. This module has been tested to be able to work on all sorts of server types including shared, VPS, and dedicated servers. It mainly provides static page caching, which should be a good solution if the visitors for your website are primarily anonymous visitors.
  • Memcached. This module works to improve dynamic web applications by alleviating database load. It is perfect for a Drupal website that has a large number of logged-in authenticated members.

Set Drupal Performance

To ensure the cache setting of Drupal is enabled in the administration setting page under the Performance option, we changed the normal caching to extended caching. We found one 3rd modules for tagging had compatible issues with the extended caching initially. With the agreement from the customer, we had removed these modules and imported some others with the similar functionality. The extended caching is about 8% faster than the normal caching for the whole website because some Drupal APIs and modules cannot work correctly with the normal caching. If you'd like to have a try, it's recommended to test through every web page after you make the change.

Also we enabled compression on the static resource files of the website. It's not about enabling GZIP and Deflate but removing the useless tabs, spaces; shortening JS variables, function names; combining all CSS files together; combining all JS files together; merging all frequently used image files together. These were the complex works actually which took us 2 weeks totally.

Use a CDN (Content Delivery Network)

Use a CDN Utilizing a CDN is also an effective and efficient way to make a great difference to the performance of a Drupal 7 website. As the abbreviation of content delivery network, CDN is a content distribution system that off-loads your static content such as images, videos and documents to a global system of connected servers from which visitors load your website content directly from the geographically closest data center instead of making queries to the database.

In this way, CDN helps decrease the load on your server, which not only makes site loading times significantly shorter, but also saves much bandwidth. Even if there is a traffic spike, your website can easily handle it without sacrificing much performance and reliability.

CCurrently, there are a few Drupal modules that provide CDN service, but we don't think they are really useful, so you'd better find a free CDN service by yourself (if the budget is sufficient, a paid one is also recommended). According to our own tests, CloudFlare CDN, CoralCDN and Incapsula CDN are the best free ones.

In the case that you may not know how CDN works and how to handle the service, we suggest you to go with a web host that offers CDN service to get help. BlueHost is a leader in this aspect. The company provides CloudFlare CDN for free as well as 24x7 around the clock technical support. In addition, BlueHost also far exceeds the system requirements of Drupal, coming with:
  • PHP 5.3.x, 5.4.x, suPHP & 256 MB memory_limit
  • MySQL 5.1.0 or higher
  • Apache 2.2.x

BlueHost Promotional Link Activation

Adjust Sequence of Resource Files in the web page

We also found the time of page generate only is much faster than the web browser loading time based on the data from server profiling and http fiddler. This was actually caused by the incorrect sequence of image, JS or CSS resource files in the web page. We know the some web browsers can concurrently downloading the resources in the web page but it's not working for all. The best practice is to make the large JS and CSS files at the bottom of the web page but not in the head section especially for the hude Google Adsense and Google Analytics files. After we adjusted the sequence, the perceived feeling on the speed of us was significantly improved.

Watch Drupal Logs & Pay Attention on 3rd Party Modules

As we continuously watch the Drupal runtime logs on the production, we found there are many errors and warning caused by the 3rd party modules. Especially the database access errors were very sensitive for us. You should know the database access error or timeout can slow down the website seriously. And we also found there is one module calling an external service to load twitter message without caching and block the whole page execution. Now our suggestion is, read the source code of 3rd party module carefully before using it on the production.

Watch Drupal Logs & Pay Attention on 3rd Party Modules These are almost all. The customer is using dedicated server that we have root and full control so it's easier for us on the performance tuning. But it's not affordable for most of readers to this article probably. If you're looking for a shared web hosting to host Drupal, your options will be limited than us. So it's important for you to find a Best Drupal Hosting at the beginning but not getting a hosting first then consider about the optimization.

Fortunately, after we reviewed a dozen of web hosting, we had come out a list of Best Drupal Hosting here with considering the web server performance, environment optimization e.g. OpCode, and reliability.

InMotion is the best Drupal web hosting provider ranked from a hundred of their competitors. The cPanel control panel integrates with Fantastico which provide the Drupal auto installer. You can easily install the latest Drupal in 3 steps by 4 mouse clicks without any complicated works. Now they're offering special discount $3.49/month. It provides unlimited disk space, network bandwidth, appropriate 256MB PHP runtime memory limit per account, unlimited hosting domain names and more.

InMotion Hosting Promotional Link Activation