Moving

Moving a Rails app from Heroku to your own server

So you want to move your Heroku hosted app onto your own server – great, this post will show you how. I’m going to assume you already have a web server set up and configured for hosting Ruby on Rails, you want to put your database in MySQL, and (optionally) that you already have a local Ruby on Rails development environment set up.

The steps below are intended to be performed on the web server you’re moving your app too. If you feel safer you can perform these steps on a local development machine, and then copy the code and MySQL database up to your production server at the end.

How to download your source code from Heroku:

In this step we download the app code from Heroku – you can skip this step if you already have access to your code.

Install the Heroku Gem (if you don’t have it installed already. Do this by typing:

gem install heroku

Log in to Heroku -

heroku login

You will be prompted for your Heroku username and password, and you should receive a Authentication successful. message once you’ve logged in.

Get your Heroku App name. This part is a pain in the butt, if you don’t know it. Heroku gives apps crazy names like quiet-frost-4223 by default, which may not mean much if your domain name is thevagabaondgeek.com. The easiest way to find the right app (assuming you have more than one – I have 22), visit the heroku address for each app.

To get a list of your existing Heroku apps, type

heroku apps

If your list is:

  • afternoon-lightning-97
  • blazing-waterfall-3921
  • young-mountain-7308

then you would visit

  • afternoon-lightning-97.heroku.com
  • blazing-waterfall-3921.heroku.com
  • 

  • young-mountain-7308.heroku.com

and one of these will take you to the app that you are trying to move.

Now that you know the app name you can clone it -

heroku clone young-mountain-7308

will download the code for that app to your server. You should run this command wherever your hosted web sites are stored. You can also specify a destination. Eg: If your webroot is /var/vhosts you can type

heroku clone young-mountain-7308 /var/vhosts/vagabondgeek

which will create a directory called /var/vhosts/vagabondgeek and download your code there.

How To Download your Postgres Database from Heroku and Import it into MySQL

This step is fairly simple, but you do need to have the Heroku gem installed on your server as well. Please note that if you have a large database this may take an extremely long time!

First, create a MySQL database to import the data into. For my blog, I’m creating vagabondgeek_production. Type the following command:

mysql -u root -p -e'create database vagabondgeek_production'

When you hit enter, you’ll be prompted to enter root’s MySQL password, and then once you hit enter the database will be created.

Now, use this command:

heroku db:pull mysql://user:password@mysql_host/db_name

Make sure you are in the app’s directory on your server when you do this.

In the above command, user:password should be replaced with the username and password of an account that has permission to create new databases in MySQL (possibly root), mysql_host should be replaced with the address of your MySQL server (probably “localhost”), and db_name should be replaced with whatever you want to call your new MySQL database (in my case, vagabondgeek_production).

If you recieve an error message: Taps Load Error: cannot load such file -- taps/operation, you will also need to install the “taps” gem by typing

gem install taps

You will recieve a WARNING: Destructive Action message when you run this command. Please read this message carefully and make sure you understand it. If you typed the command correctly, it is telling you that it is going to overwrite the database on your server (if it exists, which it probably doesn’t). To confirm you are ready, type the application name and hit enter.

It should step through Receiving schema, Receiving indexes, Receiving data, Resetting sequences, and then take you back to the command line.

Your Heroku Database has been imported into MySQL on your server.. If you opted to do this on your local machine, then you’ll need to export it as a .sql file, upload to your server and then import.

Install the MySQL Gem, configure database.yml to use MySQL

Unless you already had the MySQL gem in your Gemfile you’ll need to add it. Edit Gemfile in your favorite text editor and add the line

gem 'mysql2'

to the file, then run bundle install from the command line to install the gem. You may also wish to remove the Postgres gem, by adding a # to the beginning of the appropriate line, so it looks like

#gem 'pg'

Missing database.yml file- Your project probably won’t have a config/database.yml file as Heroku doesn’t require it, so you will need to create a new one with your MySQL server configuration.

In your app directory, create and edit the file config/database.yml.
As an example, below is what I put in my database.yml file:

production:
    adapter: mysql2
    username: mysqlusername
    password: mysqlpassword
    host: localhost
    database: vagabondgeek_production

Of course, you should change the mysqlusername and mysqlpassword to a username/password that has access to that database.

That’s it!

Well almost… You’ve moved your app to your server, now you have to configure your web server for the site, and change the DNS pointers. I’ll leave that one up to you, or to a future article. Now enjoy running your app on your own server!

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>