We deploy our site on Heroku

Hello, Habrahabr! Recently I had a need to deploy my Rails web-application on Heroku and, to my surprise, I did not find almost anything about this in the vastness of not only Habr, but the Runet as a whole, so I decided to share my experience with you. Details about what and how I did are under the cut!




Today I’ll talk about how to quickly and easily deploy your application on Heroku. If the article is of interest, I will write a sequel on how to quickly and conveniently "transfer" the application to your domain and some tips about the application itself related to it.



Deploying an application on Heroku


registration

The first thing you will need to do is register with Hreoku . After you confirm your e-mail you will need to fill in some more data in your profile. In general, nothing complicated, nothing unusual.

Logging into your profile and initializing the git repository

Install Heroku Toolbelt

The first step is to install Heroku command-line software on your computer, if you haven’t already. For Ubuntu and Debian, the installation process is to execute the following command:

user@host:~/rails/myapp$ wget -qO- https://toolbelt.heroku.com/install-ubuntu.sh | sh


For other Linux distributions - this one:

user@host:~/rails/myapp$ wget -qO- https://toolbelt.heroku.com/install.sh | sh


Honestly, I don’t know if these scripts differ in anything. Windows and OS X users can download the installation files here .

Login to the profile from the command line

After all the necessary tools are installed, you need to log in to Heroku from the command line:

user@host:~/rails/myapp$ heroku login
Enter your Heroku credentials.
Email: user@example.com
Password:
Could not find an existing public key.
Would you like to generate one? [Yn]
Generating new SSH public key.
Uploading ssh public key /home/user/.ssh/id_rsa.pub

If you have not generated a public key for SSH, then you will be asked to do this.

Git repository initialization

If you are not using a version control system or using one that is different from git, then you will have to create a local git repository on your working computer as Heroku only supports this version control system. Everything is very simple:

user@host:~/rails/myapp$ git init && git commit -am "init"


Some specific points

For your web-application to work correctly, you need to add rails_12factorto your Gemfile (if you use Rails 4) and specify the ruby ​​version (for all versions of Rails):

gem "rails_12factor", group: :production
#
# Другие ваши gem-ы
#
# Версию Ruby следует указывать в конце файла
ruby "2.1.2"


Please note that you must use the same version of Ruby on your own.

If you use a web server other than WEBrick (Thin in my case), which, by the way, is recommended by the Heroku service itself, then you will need to create a Procfile of approximately the following contents:
web: bundle exec thin start -p $PORT


Finishing touches

Our work on launching a web application has almost come to an end, and the finishing touches remain. Further it will be assumed that you are using PostgreSQL as a DBMS. If you use MySQL - a little lower I talked about how to make this DBMS friends with Heroku . So, let's create an application in Heroku:

user@host:~/rails/myapp$ heroku create myapp
Creating myapp... done, stack is cedar
http://myapp.herokuapp.com/ | git@heroku.com:myapp.git
Git remote heroku added


Generally speaking, you may not have to specify the name of the application when creating, then Heroku itself will come up with something like that for you deep-sea-69.
Now your application is available at myapp.herokuapp.com, but there is nothing there yet. To fix this, you need to run your application on the Heroku server:

user@host:~/rails/myapp$ git push heroku master


You may need to make an asset precompile so that everything works fine (I personally had to, if someone tells me how to get Heroku to do everything automatically - I will be grateful):

user@host:~/rails/myapp$ rake assets:precompile RAILS_ENV=production


After that, do all the migrations ...

user@host:~/rails/myapp$ heroku run rake db:migrate


... and you can see what you got:

user@host:~/rails/myapp$ heroku open



Friends Heroku and MySQL

It so happened that PostgreSQL is more traditional for Rails, this DBMS is also “native” to Heroku. But it may happen that you use another popular DBMS - MySQL. Then for the correct operation of your application, you need to make a few more gestures. Do not be afraid, they are simple enough!

To achieve the goal, we will use the supplement ClearDB. Install it and get data about the database allocated to us:

user@host:~/rails/myapp$ heroku addons:add cleardb:ignite
-----> Adding cleardb to sharp-mountain-4005... done, v18 (free)
user@host:~/rails/myapp$ heroku config | grep CLEARDB_DATABASE_URL
CLEARDB_DATABASE_URL => mysql://adffdadf2341:adf4234@us-cdbr-east.cleardb.com/heroku_db?reconnect=true


Next, just set the received address to quality DATABASE_URLin Heroku, changing the protocol to mysql2 if necessary:

user@host:~/rails/myapp$ heroku config:set DATABASE_URL='mysql2://adffdadf2341:adf4234@us-cdbr-east.cleardb.com/heroku_db?reconnect=true'


That's all! This concludes my story. This is my first post on the hub, so I will be very grateful for any comments on the style and design and, of course, I will be happy to answer your questions in the comments!

Also popular now: