Best Cheap Drupal Hosting on a Private VPS Server

There are too many hosting options out there count, and some offer extremely low prices. However, with those low prices, comes a low quality of service. It’s also quite common that with this low price tag you are locked into a yearly deal. If you site out grows your hosting you will be forced to move into the more expensive tier, when that happens, your cost savings go out the window.

Been there, done that. Now all my sites are hosted on Digital Ocean. Here you can get a fully accessible Drupal site up and running in under 55 seconds for the low cost of $5 a month.

Why I love Digital Hosting for Drupal Servers

  1. Cheap – Their lowest tier is $5 a month, and you are getting way more out of this server than you will from a shared hosting service for the same price.  If your site needs a little more power, upgrade to the $10 or $20 tier and you will be sailing.
  2. Easy – Installing a Drupal site is literally as easy as clicking a few buttons.  These clicks do hours of work for you, installing nginx, mysql, drush, and all the other prerequisites you need to get your server up and running.
  3. No contracts – If you’re done with your site, simply destroy the droplet.  You are not charged on a yearly, or even monthly basis.  You are only charged per hour that your server instance is running, so once you’re done, you’re done.
  4. SSD – With each instance, your site is loaded up on a machine with an SSD which makes your site quite fast.  It also means you can expand your virtual RAM space with a swap file that will operate at high speeds.
  5. Root access – It’s rare to find a VPS server, with root access at this price.  For example, the $5 tier on Digital Ocean has more data transfer, SSD and a full CPU, while the cheapest HostGator option has no SSD, 0.5 CPU and costs $12
  6. Single Click Installs – Looking to setup a Drupal site, Digital Ocean can do that with a single click.  The also offer a ton of other options for a single click
  7. Documentation/Support – Their documentation and support is phenomenal.

Free $10 credit

When you sign up using my referral link, you will receive a free $10 credit which will give you enough for two-months absolutely free.  Try it out yourself, zero risk.

Let me know in the comments if you have any questions, happy to help.

Drupal 7: Big cron tasks? Spread them out.

If you’re building a module that relies on the Drupal cron to do heavy work for you, it might be better idea to split up the task into chunks to share the load.

Let’s say you have a single cron task that has some heavy database work, that could take the cronjob over a minute to execute. Here is a little algorithm you can put into place to help lighten the load on each run.

We need to assume a couple things, but these can be easily adjusted for your case. First we have to assume the table has an incremental index like a primary key, if it doesn’t there is another way to do this, but most tables should have a primary key. Let’s also assume the cron runs once an hour, and each row needs to be affected once per day. Which means we have 24 times to act on every row.

function MODULE_cron() {
  // Setting how many cron runs there are in a day
  $cron_runs = 24;

  // Get the the total number of rows that need to be acted upon
  $rows = db_query("SELECT COUNT(*) FROM {tbl_name}")->fetchField();

  // Rows per cron
  $rpc = $rows / $cron_runs;

  // Get the last row we acted on, else set to 0
  $last_row = variable_get('MODULE_last_row', 0);

  // Set new last row
  $new_last_row = $last_row + $rpc;

  // Get all the rows we want to act on
  $result = db_query("SELECT * FROM {tbl_name} WHERE primary_key > :last_row AND primary_key <= :new_last_row",array(":last_row" => $last_row, ":new_last_row" => $new_last_row);

  foreach($result as $row) {
    // DO WORK

  // Get the new last row, if over the limit, reset to 0
  $new_last_row = ($new_last_row > $rows ? 0 : $new_last_row);

  // Set the new last row that we acted on
  variable_set('MODULE_last_row', $new_last_row);

The beauty here is that, even if the list grows over time, it will then increase the amount of rows to act on. It scales well and can help take the load off each cron run. Very usefulif you have a table with a lot of data.

Add HTTPS (SSL) to Your Drupal 7 Site on Linux/Apache

1. Getting your SSL

Purchase a new SSL from a registrar, I use NameCheap, you can get a simple PositiveSSL from Comodo for $9.00/year.
After your purchase is complete, you will need to generate a key and csr from your server.

# sudo mkdir /etc/apache2/ssl
# sudo openssl req -nodes -newkey rsa:2048 -keyout websiteurl_com.key -out websiteurl_com.csr

The key file is used later, and the csr file is used to generate your SSL certificate. Finish the process and wait for your certificate to arrive in a zip file to your email address. Place all those files in your /etc/apache2/ssl directory.

2. Enabling SSL on Apache for Drupal

First we need to make sure SSL is enabled on Apache.

# sudo a2enmod ssl

Now we need to edit your vhost file for your Drupal site. Here is a very simple vhost file for a Drupal 7 site, yours may be different:

<VirtualHost *:80>

  ServerAdmin webmaster@localhost

  DocumentRoot /var/www/domain

<VirtualHost *:443>
  ServerAdmin webmaster@localhost

  DocumentRoot /var/www/domain

  SSLEngine on

  SSLCertificateFile /etc/apache2/ssl/domain_com.crt
  SSLCertificateKeyFile /etc/apache2/ssl/domain_com.key
  SSLCACertificateFile /etc/apache2/ssl/domain_bundle.crt


Here you are basically adding turning on SSL for all http requests going to port 443, the default SSL/TLS port. One thing that may not have been provided to you in your SSL zip file is a bundle file, it’s basically three crt files all concatenated together. Each SSL is different so you are going to have to figure this one out on your own.

# sudo service apache2 restart

Your site should now be working over https

3. EXTRAS: Redirecting not SSL traffic to SSL or http traffic to https

1. SecurePages is a great way to help manage your SSL redirects within Drupal. To enable secure pages, download the latest stable version of the module and place it in your modules folder. Add $conf['HTTPS'] = TRUE; to your settings.php file. Enable the module, go to the configuration screen and select which pages you want it to redirect to HTTPS.

2. Apache redirects is another option, very similar to securepages, but it’s don’t on the Apache level. You can redirect all traffic to https by adding this to your vhost file

RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}


If your SSL isn’t working here are a couple common issues.

1. If you are using CloudFlare Free, you’ll have to disable CloudFlare as they only allow non-https traffic on the free plan

2. Your apache server needs to listen on port 443. This should be set up by default in the ports.conf, however if not add in Listen 443 to the configuration.

3. Your server is blocking port 443. If you have any sort of firewall or IP Tables setup, make sure port 443 is open.

4. Your chain or bundle files where generated incorrectly.

Drupal 7: Lock/Password protect our site from anonymous users and add login/register block

There are a few ways to do this, probably the most common way is to put your site in maintenance mode and edit maintenance-page.tpl.php. This method works well and can produce the desired results, however, it’s not the proper way of doing things. What if you still want to have the use of maintenance mode for it’s intended purposes. Well this could still work, but you’re going to have to fiddle with the permissions each time. Also the maintenance mode message will appear on your site, so that will have to be removed and if you want it back, you’re going to have to edit code.

Here is another way, without having to fiddle around with any backend code.

  • Make sure PHP filter is enabled in the modules
  • Create a new block
  • Enter your template information in there, here is an example based on a bootstrap design

<div class="container">
  <div class="row">
    <div class="panel panel-default">
      <div class="panel-heading">
        <h3 class="panel-title">Access Denied - Please Login or Register</h3>
      <div class="panel-body">
        <div class="row">
          <div class="col-sm-6">
            <h3> Login </h3>
              $loginForm = drupal_get_form('user_login');
              print drupal_render($loginForm); 
          <div class="col-sm-6">
            <h3> Register </h3>
              $registerForm = drupal_get_form('user_register_form');
              print drupal_render($registerForm); 
  • Set the text format to “PHP”
  • Go to Visibility Settings > Pages > Pages on which this PHP code returns TRUE
  • Enter this code:

  global $user;
  $url = $_SERVER['REQUEST_URI'];
  if ($user->uid || strpos($url,'user')) {
    return false;
  else {
    return true;
  • Save block
  • Place the block into the content region and save the changes.
  • Go to permissions and change the Node permission - View published content for anonymous users. Uncheck the box.


Drupal and Drush: Import/Export SQL Database with Drush

Moving sites from one server to another can be a tedious task, however with command line and drush we can simplify this task into a few easy steps.

Export the database

# drush cc all && drush cc all
# drush sql-dump > /tmp/website-dump.sql

Clearing the cache first removes unwanted cached tables which make the SQL file quite large and can also cause problems on import with the “MySQL Server Gone Away” errors

Copy the database over to your other server

You can use whatever method you like (FTP, etc) but to keep things on the command line, let’s use secure copy.

# scp /tmp/website-dump.sql dest_username@dest_host:/tmp/website-dump.sql

If the site you wish to import to is on the same server, you can skip the step above

Use drush to import the database

Navigate to your drupal install and run “drush status” to make sure the database connection is there.

# drush sql-drop
# drush sql-cli < /tmp/website-dump.sql


Responsive Twitter Widget

Grab your twitter widget.

Add this to your CSS file.

.twitter-widget-0 {
  width: 100%;


Drupal 7: Basic Drush Commands

If you have SSH (command line access) to your Drupal website, then drush is a must.  Here are a few of my favourite and most useful Drush commands.

1. Clearing the cache with drush

When developing clearing the cache can save a lot of headaches, however doing it through the user interface takes it’s time and using drush helps to speed up this process.

drush cc all

It is also possible to pick the specific cache you wish to clear but most of the time I am using this command in development and clearing all the caches works perfectly.

2. Downloading and enabling modules with drush

This is one of the best features of drush and probably the most used

drush dl <project_name> -y

drush en <project_name> -y

It is also possible to specify the version you wish to download, for example if you are looking for the development version

drush dl <project_name>-7.x-1.x-dev -y

3. Disabling and uninstalling modules with drush

Another feature which allows quick disabling of modules including their dependencies all in one line

drush dis <project_name> -y

drush pm-uninstall <project_name> -y

Don’t forget you can chain commands together in the normal bash way

drush dis <project_name> && drush pm-uninstall <project_name> && drush updb && drush cc all && rm -rf path/to/project/folder

4. Resetting user passwords with drush

Sometimes you need to reset passwords for specific users or even the admin password, in Drupal 7, passwords are salted and must be created with the password script, where in Drupal 6, the passwords were just a MD5 hash. Thankfully drush has a command to automate the whole process

drush upwd <username> --password

Another useful feature is generating a password reset link

drush uli <username>

5. Setting maintenance mode with drush

The vset command allows you to modify variables in the variable table from the command line, it follows this general rule

drush vset <variable_name> <value>

Which means to change the maintenance_mode variable on, we use this command

drush vset maintenance_mode 1

To change the maintenance_mode variable off, we use this command

drush vset maintenance_mode 0

6. Changing the default theme with drush

Have you ever blown up your site with a bad change but need to get into the interface so you can change something back? Well whatever reason you have where you need to change the default theme, this command can save lives.

drush vset theme_default <theme_name>

The theme must be enabled, but you can do with with the drush en command listed above.

7. List all enabled modules, exclude core with drush

drush pml --status=enabled --type=module
drush pml --status=enabled --type=module --no-core

8. MySQL dump with drush

Perform a mysqldump (or equivalent) of the connected drupal database into a .sql file

drush sql-dump --result-file=/home/user/dump.sql

9. Backup and restore sites with drush

You backup your site into a single archive file, and then restore it with these simple commands

drush archive-dump --destination=/home/user/backup.tar.gz

drush archive-restore /home/user/backup.tar.gz

10. Update drush with drush

A very simple command to get the latest version of drush

drush self-update

For a full list of commands, check out

The Mental Benefits of the Foot Massage

I’m not talking about some sort of holistic magic on how massaging a certain part of your foot cures another part of your body.  I can’t attest to that, but what I can say is in this busy world where we are lives are filled with plethora of stimuli, we need some time to disconnect.

You can disconnect but not entirely, enter Pocket, my favourite web clipper for reading web articles later.  The biggest issue most people find with Pocket and other web clippers is that you clip articles and never get a chance to read them. Your reading list piles up and never gets emptied.  Finding that time to just sit and read can be difficult but that is where I have found foot massages.

I’ll let you know that I am currently in Thailand and foot massages are cheap as chips, literally.  A foot massage can run you anywhere between 50 baht to 150 baht (a few dollars) depending on how long and where you go.  That being said foot massages anywhere can really be worth it if you get the value out of it like I do.  I use them to take a break from work, and relax, but I also bring my phone to catch up on my web readings.

A foot massage on it’s own can be very relaxing and feel great, but I get bored sitting there for 30 minutes to and hour so I decided one day to pop out my phone and catch up on my readings.  I’ve never felt such a moment where I was totally removed from the rest of the world and could entirely focus on my reading.  Even when I read on my computer I am distracted and it turns more into a skimming session.

Moral of the story is take advantage of your breaks; relax and get some reading done.  After an hour long foot massage you will be refocus, more relaxed and ready to get on with the rest of your day.  I believe in the biz they call that one a double whammy.

Tutorial: Free Drupal 7 Web Server on Amazon Web Services (AWS) Elastic Compute Cloud (EC2)


  • Amazon account
  • A terminal – (PuTTY is recommended for Windows users)

Getting Started

Sign into your Amazon account and go to the AWS console page.  Click on “EC2″


Security Groups

Next we want to create a new “Security Group” for your future EC2 instance.


Then click on the “Create Security Group” button.


In the popup, enter your new group name and description. I’ve chosen the name and description to be “Drupal 7 Web Group”. Then click “Yes, Create”.


Once the group has been created, we need to allow for HTTP/HTTPS/SSL traffic to be allowed by creating inbound firewall exceptions for this security group. Select the newly created security group, and at the bottom of the page, switch the tab over to “Inbound”.


From the drop down list select “SSH”.


Click “Add Rule”.


Repeat these steps for HTTP, HTTPS, and any other protocols you might be using, however SSH and HTTP are required. MYSQL is not required unless you will have other external servers accessing the MySQL server hosted on your new EC2 instance.


Now you must apply the changes, for the changes to be saved.


After the changes have been applied, we need to create a new “Key Pair”, so select “Key Pairs” from the sidebar.


Choose the button at the top, “Create Key Pair”.


Enter a new name for the key pair, I chose, “Drupal7WebGroup”.


Then choose, “Create”.



The key file (.pem) should automatically be downloaded by your browser. You are going to want to keep this file in a safe place, we will be using it later.


Creating An EC2 Instance

On the sidebar, choose “Instances”


Then choose the button to “Launch Instance”.


Stick the with default, “Classic Wizard” and choose “Continue”.


Here you have a choice on which Amazon Machine Image (AMI) you wish to use. If you are familiar enough with Linux they choose your favourite flavour, but for the purposes of this tutorial, I am going to choose, “Ubuntu Server 12.10 64-bit”.


On the next page, we get a choice of hardware. If you just signed up for Amazon Web Services (AWS), you are eligible for a free year of micro hosting on the lowest tier. It’s a great place to start and has enough resources to run a small Drupal 7 without any problems. I recommend the lowest tier if you’re just starting out, you can always upgrade it later with no problems.


You can leave the rest of the settings default, or change them as you see it, then choose “Continue”.


The next page allows for some more configuration, but the default settings will work just fine, choose “Continue”.


The next page is an overview of the storage device configuration, nothing to do here but click “Continue”.


Now you want to name your instance, for this tutorial, we will name it “Drupal 7 Web Server”, then press “Continue”.


Now we get to apply the key pair we created earlier to this instance. Select the key pair you created earlier, then press “Continue”.


Next we choose our existing security group that we created earlier, then press “Continue”.


Our instance is complete, and here is the overview page. Choose “Launch” to launch the instance.


Assigning an Elastic IP to Your EC2 Instance

On the sidebar, choose “Elastic IPs”.


Then choose the button at the top to “Allocate New Address”.


Choose “EC2″ and click “Yes, Allocate”.


Select the new Elastic IP address from the list, and choose the button at the top, “Associate Address”. Then choose the new web server in the select box, and click, “Yes, Allocate”.


Converting Your Private Key (.pem) File to PuTTY’s Private Key (.ppk) File

Now that we have a IP associated with the instance we can connect over SSH, however if we are using PuTTY we need to convert that .pem key file to a .ppk file that works with PuTTY, so let’s open the program PuTTYgen.exe

Choose “Load” and find your .pem file that was saved early when creating key pairs.


Change the file type at the bottom to “All Files (*.*)” and then find the .pem file you saved earlier. Choose “Open”.


Now save this file to a safe place by clicking the button, “Save private key”. Ignore the warning about the passphrase.


Connecting Over SSH to Your New EC2 Instance with PuTTY

Open PuTTY, and in the Host Name (or IP Address), enter in “ubuntu@ip_address” where the IP address is the elastic IP that we assigned to the instance earlier.


Then in the left panel, open “SSH” and select “Auth”. In the “Private key file for authentication” browse for your key and open it.


Next, go back to the left panel and click “Session” at the top, type a name in the blank field and save your session for later use.


Now click, “Open”. If everything was done right you should get a pop up that asks you to add the rsa2 key fingerprint. Choose “Yes”.


You are now connected to your server.

Setting up the LAMP Stack (Apache2, PHP5, MySQL5)

Run these commands to get everything set up properly and efficiently

Update the releases

sudo apt-get update

Preform and upgrade on server libraries, Type “Y” at the prompt.

sudo apt-get upgrade
sudo apt-get install lamp-server^ phpmyadmin

Choose “Y” at the prompt to install the packages.

Enter a strong password for the MySQL root user. Twice.


On the following screen press SPACE, TAB, ENTER.


For the PHPMYADMIN setup, press , then enter a strong password twice, could be the same as the root password you used before. Twice.

Now we need to enable the Apache modules needed for Drupal.

sudo a2enmod rewrite expires headers

Then restart your apache2 server.

sudo service apache2 restart

Your web server is now configured, install Drupal in /var/www, or else you can create virtual hosts to handle multiple site installations.

Pointing your DNS at your new web server

You need to go to your domain manager and change the A Record to your new server’s Elastic IP address that we set up earlier.

Drupal 7: Node.tpl.php Templating – Rendering Multiple Image Fields

When building a node template in Drupal 7 using the image field may require a little more PHP than rendering a normal field, here are a couple snippits that can be used to properly render each image field where you see fit.

Iterative Approach 1 – Fixed Number of Images

Image field name assumed field_image and there are a possible 6 images to be displayed. Gathering the images into an array to be displayed later. I like to separate this from gathering step from the actual rendering of the images.

$images = array();
$images[0] = render($content['field_image']['0']);
$images[1] = render($content['field_image']['1']);
$images[2] = render($content['field_image']['2']);
$images[3] = render($content['field_image']['3']);
$images[4] = render($content['field_image']['4']);
$images[5] = render($content['field_image']['5']);

Next you can display the images anywhere you want in the template file by echoing/printing each image if it exists. Something like this, I added in the extra div classes to show how to add new classes around each image.

<?php if (!empty($images[0])) { echo '<div class="active item" data-slide-number="0">'; echo $images[0]; echo '</div>'; } ?>
<?php if (!empty($images[1])) { echo '<div class="item" data-slide-number="1">'; echo $images[1]; echo '</div>'; } ?>
<?php if (!empty($images[2])) { echo '<div class="item" data-slide-number="2">'; echo $images[2]; echo '</div>'; } ?>
<?php if (!empty($images[3])) { echo '<div class="item" data-slide-number="3">'; echo $images[3]; echo '</div>'; } ?>
<?php if (!empty($images[4])) { echo '<div class="item" data-slide-number="4">'; echo $images[4]; echo '</div>'; } ?>
<?php if (!empty($images[5])) { echo '<div class="item" data-slide-number="5">'; echo $images[5]; echo '</div>'; } ?>

The main problem with this approach is it’s slightly less scalable as the next approach, if you have more than six images, you’ll have to add a few more lines of code, repeating the process. Not really conforming with the DRY programming paradigm.

Iterative Approach 2 – Any Number of Images

Image field name assumed field_image

$images = array();
$i = 0;
foreach ($content['field_image'] as $image) {
  $images[$i] = render($image);

Next we can echo/print each image as before, using the same principles as above or echo/print each image individually.

foreach ($images as $printable) {
  echo $printable;