📜 ⬆️ ⬇️

We deploy your website on Heroku

Hello, Habrahabr! Recently, I had the need to deploy my Rails web application on Heroku and, to my surprise, I found almost nothing about this in the open spaces not only of Habr, but of 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 will talk about how to quickly and easily deploy your application to Heroku. If the article is of interest, I will write a sequel about how quickly and conveniently to “transfer” the application to your domain and some tips about the application itself, related to this.
')


Deploying the application on Heroku


check in

The first thing you need to do is register on 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.

Log into your profile and initialize the git repository

Installing Heroku Toolbelt

The first step is to install the Heroku command line software on your computer, if you have not done so 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 are any different. Windows and OS X users can download the installation files here .

Login to profile from 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 yet generated a public key for SSH, you will be prompted to do this.

Initializing the git repository

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

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


Some specific points

In order for your web application to work correctly, you need to add rails_12factor to your Gemfile (if you are using Rails 4) and specify the version of ruby ​​in the same place (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 in your home.

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 similar Procfile with the following content:
 web: bundle exec thin start -p $PORT 


Finishing touches

Our work on launching a web application has almost come to an end, there are final touches. Further it will be assumed that you use PostgreSQL as a DBMS. If you are using MySQL - a little lower, I talked about how to make friends with this database with Heroku . So, 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 can not specify the name of the application during the creation, then Heroku will invent something like deep-sea-69 .
Now your application is available at myapp.herokuapp.com myapp.herokuapp.com , but there is nothing there yet. To fix this, you need to launch your application on the Heroku server:

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


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

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


After that, perform all the migrations ...

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


... and you can see what you did:

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



We are friends with Heroku and MySQL

It so happened that for Rails, more traditional is PostgreSQL, this DBMS is also “native” for 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 worry, they are quite simple!

To achieve the goal we will use the add-on 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 as DATABASE_URL in Heroku, if necessary, change the protocol to mysql2:

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


That's all! At this my story ends. This is my first post on Habré, 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!

Source: https://habr.com/ru/post/232679/


All Articles