How to Switch Your VPS in 2 Hours

Friday last week I woke up to a barrage of emails coming into my inbox. All of these were coming from notification of spam comments upon this very blog. By the time I’d woken up I’d had something like 75 emails. This was pretty strange given that Akismet was there guarding my blog ever since it’s been available and I’d never had a email about a dodgy comment since I’d set it up (something like 5 years ago). They continued to flood in during my breakfast as fast as I could delete them.

The problem stemmed from the fact that network connectivity from my slicehost account had been knocked out of action – which had never happened before. I presumed this was down to the migration of slicehost over to Rackspace infrastructure, which I’d been informed of, but the network dying was probably unintended.

Either way, it was frustrating and I instantly decided to finally migrate away from slicehost and setup a second linode account. The funny thing is, that most of this was aided by slicehosts own documentation on configuring your server. If you’re doing little more than hosting a number of mysql / php driven sites, it is in fact really simple to switch. Here’s how I went about it, my sites all experience fairly low traffic, so I’m aware this solution might not be right for everyone.

Backup All Your Important Data

First of all, you’re going to need to backup everything you have in your sites directory. For me, that’s everything under /var/www.

tar -zcvf backup-feb-2012.tar.gz /var/www

Next, you’ll want to do the same for your Apache sites:

tar -zcvf backup-apache-feb-2012.tar.gz /etc/apache2/sites-available

Finally, I took a mysqldump of my entire mysql setup:

mysqldump --all-databases -u root -p password | gzip >dbbackup-feb-2012.sql.gz

Setup Your New VPS

Having done that, you have pretty much everything you need to bring your server back to life as it is now. Go and grab a new vps from linode or somewhere else and set it up using the details starting from here: http://articles.slicehost.com/2010/10/18/ubuntu-maverick-setup-part-1 I configured my vps to have exactly the same accounts and passwords as my original vps, to make life easier for me. Once SSH and your firewall has been setup and you’re all updated, you’ll be able to transfer all the files you just created onto your fresh vps setup, using filezilla, or a direct connection between the two machines. You’ll then need to install Apache: http://articles.slicehost.com/2010/5/19/installing-apache-on-ubuntu, PHP: http://articles.slicehost.com/2010/5/19/installing-php-on-ubuntu and MySQL: http://articles.slicehost.com/2011/3/10/installing-mysql-server-on-ubuntu

On your new machine, you’ll need to restore the files you backed up to /var/www.

tar -xzf backup-feb-2012.tar.gz
sudo cp -r var/www/* /var/www/
sudo chown -R www-data:www-data /var/www/*
rm -rf ~/var

Then the apache site files:

tar -xzf backup-apache-feb-2012.tar.gz
sudo cp etc/apache2/sites-available/* /etc/apache2/sites-available/

For every file that exists in the sites directory and is to be enabled, we need to enable it. You can use a2ensite for this if you like.

cd /etc/apache2/sites-enabled
sudo ln -s ../sites-available/ianwootten.co.uk.conf ianwootten.co.uk.conf (for each site file)

Install any apache modules you might require and restart apache. If you’re using wordpress or a php framework like me, you’ll most likely need mod_rewrite.

sudo a2enmod rewrite
sudo /etc/init.d/apache2 restart

You’ll then need to repopulate your new mysql install with all your old data.

gunzip -c databasebackup.sql.gz | mysql -u root -p

Configure Email

I use msmtp to handle all my email on my server, which sends through a single gmail automailer address for my domain. If you want to send email that way do the following:

sudo apt-get install msmtp ca-certificates
sudo nano /etc/msmtprc

Paste the following setup in the file:

defaults<br /> tls on<br /> tls_starttls on<br /> tls_trust_file /etc/ssl/certs/ca-certificates.crt</p> <p>account default<br /> host smtp.gmail.com<br /> port 587<br /> auto_from off<br /> auth on<br /> user automailer@somedomain.com<br /> password mymailpass<br /> from automailer@somedomain.com<br /> maildomain somedomain.com<br /> logfile /var/log/msmtp.log

sudo chmod 0644 /etc/msmtprc

You can test whether it’s working using a the command as:

echo -e "Subject: A Test Email\r\n\r\nThis is a test" |msmtp --debug --from=default -t mymail@gmail.com

Configure DNS

You need to configure how your vps is going to handle particular requests for various domains. You’ve probably had to do this already with your current vps, so just copy the entries through whatever management system your new host provides. For sites that have a fairly simple setup, your host may provide a simple set of default entries to use, or you can clone entries that have the same setup as each other.

We’re now at the point where we have a complete copy of our vps up and running, and the vps knows how to handle requests for domains, however currently our domain registrar points to our old vps, so the requests will never reach our new server. Head to your domain registrar and change the entries for a single domain, to point to your new vps (ns1.linode.com, ns2.linode.com etc). Now sit and wait patiently….Once that domain has propagated correctly (ping it to see if has a new or old ip), and you’re happy your site is working as expected, go ahead and change all of your domains dns entries to the ip address of your new vps.

Relax…

All in all, I don’t think this took me more than a couple of hours working with the terminal open. I certainly didn’t worry about migrating in the way I have done previously. As I opted for a uk datacentre with linode, a side effect of the change is all my sites are much much faster – and a better setup with linode is in fact cheaper than slicehost. I left my slicehost account running for an entire week prior to shutting it down yesterday – just in case…

comments powered by Disqus