Optimizing Drupal sites on shared hosting: A Case Study
The first web hosting I signed up for was ANHosting's "All Inclusive Mega Package" shared hosting. Going with an $8/per month plan made a lot of sense while I was learning to build web sites. I chose ANHosting after finding it recommended for Drupal sites on John Forsythe's blog. Over my first year building web sites I have built many sites on that hosting (and many others including some similar hosts, Pair Networks hosting, and a VPS I rent from vps.net).
Recently I learned how to optimize my sites on the shared host by using a custom php.ini developed from the default php.ini my host uses for my account. This was not a trivial thing to learn how to do, since I originally thought (mistakenly) that I was restricted to CPanel access that did not allow me to access the server's php.ini.
So the first part of this tip, is to investigate whether you are actually are allowed any ssh (command-line) access to your account. If you really do not have this access to your hosting, then the rest of this post probably does not apply to you.
If you are still reading, log into your shared hosting with a command-line like (ignore the line number "1."):
- ssh -l username mydomain.com
Enter your password when asked. At this point, you should be presented with some sort of UNIX prompt. Even if you have this much access, you probably do not have the permissions needed to use the very handy command-line tool drush. But, for the purposes of this article, we just need to find the php.ini used to set up PHP. And that does not take a high level of permissions.
The task now, is to locate the php.ini that is used when PHP starts up on your host. I first found the php.ini by hunting around using the UNIX commands ls and cd. But then I discovered that I could run PHP from the command line. So try something like:
- php -i | less
The "php" runs the PHP command, the "-i" is a flag to PHP to output information about its configuration, and the "| less" is a UNIX way of getting the output to be paginated (and going on to the next page with space, etc...) and not just letting the output run by, leaving you looking at the last lines of a long output.
What you are looking for is a line in the output like this:
Loaded Configuration File => /usr/local/lib/php.ini
[Note: Experienced UNIX users will immediately see many precise ways to search for such a line (e.g. with the command grep, but the commands are much harder to type correctly for those not experienced in UNIX).
Now all one has to do is to transfer that file to the root of one's Drupal install (where my host, at least, will use it in place of the default one. Then you can modify whatever you need to to make Drupal run smoothly.
To transfer it there, try returning to you local machine's command line with:
- exit
and then use the command:
- sftp username@mydomain.com
to start the secure version of the ftp command.
At the sftp prompt you would use a series of commands like:
get /usr/local/lib/php.ini
cd public_html/drupal
put php.ini
The last task is to modify the file you have copied into the root of the Drupal install the way you need. I use CPanel's "File Manager" and its "Edit" button to do this. The lines I change look like the following:
upload_max_filesize = 10M
post_max_size = 20M
That gets my Drupal sites on my shared host humming along. The beauty of this process is that you can modify any of PHP's starting configuration and keep all the good things that your hosting already had set up for you in the default php.ini. I found out the hard way that the php.ini I first put at the document root of the Drupal install overrides the one your host provides, and I needed some of the default configuration. This is the real reason to find the host's default php.ini and start from there to make a custom one for your Drupal site. Please contact me for assistance with setting up Drupal on your shared host, or post a comment to this article.
