Configure and optimize Drupal for a shared hosting
These tips are not exclusive for shared hosting but it's particularly important to keep an eye on some settings on shared hosting because there are 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(); ?>" (without the quotes), chose as input format "PHP code" (make sure you have PHP filter enabled in admin/build/modules), 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 erase it once you are done 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 famous php.ini file to configure the server 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:
SetEnv REGISTER_GLOBALS 0
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 distribution. Usually shared servers use old versions of PHP 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 do not need magic quotes and overall, magic quotes was a lousy idea from the beginning.
SetEnv MAGIC_QUOTES 0
-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.
SetEnv ZEND_OPTIMIZER 1
2 - Set up database optimization
Database optimization can be made inside your Drupal installation and will need the help of a few modules. Get your FTP ready for it. 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.
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 in admin/build/modules. 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.
These settings cannot be changed usually 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 will indeed speed things up for the final user but note that this module won't work if the content of your site changes often and neither if you use translations with "fallback" option (Drupal 6).
In the case that, despite all that, you still have problems remind that your website can have an overload when a search engine is indexing your pages and makes hundreds of page requests in just a couple of minutes. This should not prevent 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:
- Uncheck all modules that you don't use and don't forget to uninstall them and update the database. 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 has the same effect of slowing the page just a little bit. Use them with parsimony.
- Some themes are also slower to load than others. Just avoid themes with heavy images or animations to have your page load fast.
So I hope I have gone through most of the questions you were asking yourself before reading this tutorial. If this is not the case ask in the comments or launch a forum topic. I regularly come back to answer questions when I can and update the tutorial to make all the community take advantage of it.
Have fun and happy webmastering!