Freedom, Community & Sustainability

Configure and optimize Drupal on a shared hosting

January 26, 2011 -- William
Last modified on February 2017
Duration: +- 15 minutes

These tips are not exclusive for shared hosting but it's particularly important when you are on a shared hosting to keep an eye on some settings because of some limitations that can be very restrictive. On shared hosting you do not have access to the server configuration (of course, since it's shared) and you certainly have a limited database size and limited simultaneous connections to that database. I will cover these issues and some other useful stuff you might want to do while you're at it.

Checking PHP info.

To start you need to know what are the settings of your hosting provider otherwise you're just out in the dark. This is easy to do and you shouldn't be scared by the amount of information you will have access to, just learn one setting at a time and don't worry about it too much.

If your Drupal is not yet installed do this now, we are going to work directly on Drupal.

Create a new page, enter the following text :

<?php phpinfo(); ?>

Select "PHP code" as input format (make sure you have PHP filter module enabled if you don't see this input format), uncheck all Publishing options (you don't want anybody, except you, to see this file) and hit save.

You will have before your eyes everything there is to know about your server. Check for example if register_globals is set to on=1 or off=0. If Drupal is working it's certainly off. Keep reading to see what other information you can check on this page. It's a good practice to delete this page once you are done with it because you don't want to expose accidentally your server configuration.

1- Set up PHP environment:

On shared hosting you do not have access to the php.ini file to configure the server PHP settings so what you need is a workaround using the .htaccess file.

Open the .htaccess file that is at the root of you Drupal installation. Note that, on your computer, this file is probably hidden and you won't see it on your local Drupal installation (although the file is there).

  •    First thing to must do, otherwise Drupal will give you an error, is to turn off register_globals. This is a security issue so if your server have them turned on, you should add the following line to the .htaccess file:

The SetEnv allows you to change the PHP environment for everything that is inside the folder where you put the .htaccess file (including all folders inside it too). You can even put an .htaccess file into every folder if you want different configurations on each folder.

  •    You might also have to set the PHP version to run your Drupal installation. Usually shared servers use old PHP versions just to be sure everybody can use it. To set PHP 5 for example add:
SetEnv PHP_VER 5
  •    For security/practical reasons it's a good idea to turn magic quotes off. Drupal doesn't need magic quotes and overall, magic quotes was a lousy idea from the beginning.
  •    To speed things up you should definitely enable Zend Optimizer. Pages load in half of the time it takes without it and there's no downside to it so, just add this unless you have very specific reasons not to.

2 - Set up database optimization

Database optimization can be made inside your Drupal installation and will need the help of a few modules. There are two modules you need to install to have your database automatically optimized on a regular basis. These modules are: poormanscron (is now part of Drupal 7 core) and db maintenance.

PoorMansCron allows your site to to have cron triggered on a given interval. The cron routine will take care of launching DbMaintenance for you. Note that poormanscron will place a small JavaScript on every page to check if the interval has expired or not. In case it is expired it will launch cron and reset the interval. This means that cron will not be launched unless somebody visits your site. Usually cron running on the background is not noticeable. It will clean the cache and overhead thus optimizing your database, avoiding it getting too big and cluttered. You can compare this to emptying your trash and running a defrag.

These two modules are pretty straight forward, you download, install and set up. Don't waste time looking for poormanscron, there's no new item on the administer page, it is visible under the "site information" of your admin menu.

If you feel your database still has a weak performance or you easily get the message that max_user_connections is causing problems, you have to reduce Drupal's use of the database.

The first, easy and immediate thing you can do is disable database logging. Note that disabling this option is possible only since Drupal 6. Database logging will log everything that happens on your site into your database. That's very useful isn't it? Yes it is but it also means that Drupal will constantly make queries to your database and this is a heavy feature. You can also use other programs to do that like munin for example.

Also disable the syslog check-box because according to the documentation it's not suited for shared hosting.

Then you should definitively check your MySQL settings on the phpinfo file you created at the beginning of this tutorial. Check if:

mysql.allow_persistent is off. Persistent connections will never expire and can overload a limited SQL server.

mysql.connect_timeout is not -1 (no timeout) and the value do not exceed, let's say, 60 (= 1 minute)

mysql.max_links should be as high as possible. Drupal can require lots of connections, you will be on the safe side if this number is near 100 but sometimes 5 to 10 connections is enough on a low traffic / low configuration site.

Sometimes these settings cannot be changed because they are part of the deal you make with your provider. Consider changing your hosting plan or changing your provider if these settings are too limited. You can start by contacting them first and explain your issues.

Another option you have to speed up your site is to use the Boost module. It's not really optimization in the sense that the core of your site won't get any faster. What it does is to create a cache of your rendered pages and display the cache to the user. This is the best way to speed things up for the final user. Note that this module won't work very well if the content of your site changes very often and neither if you use translations with "fallback" option (Drupal 6 only).

If despite all that you still have problems, remind that your website can have an overload when a search engine is indexing pages and makes hundreds of page requests in just a couple of minutes. This should never impede your site from working because search engines are going to crawl your site everyday, hopefully.

3 - Some good practices:

After doing these elaborate steps don't forget the most simple actions to keep everything running swiftly:

  • Disable all modules that you don't use and don't forget to uninstall and delete the ones that you won't use anymore. For every module installed Drupal will have to perform some extra work even if the modules are not being used!
  • Mind that every block, image, widget or app you add to your page will slow it down a little bit. Use them with parsimony.
  • Some themes are slower to load than others. Just avoid themes with heavy images or animations to have your page load fast.

That's about it! I hope to have answered most of the questions you were asking before reading this tutorial. If this is not the case ask in the comments.

Have fun and happy webmastering!

Add new comment