Should I deploy my Ruby on Rails application on Heroku
Yes, you should definitely deploy your application in heroku. To do this, this is what you will need to do:
- Make sure you have git installed in your computer
- Create a heroku account here
- Install the heroku gem and do the rest as mentioned in this page
- Track your application with git, and create your heroku application as shown here
- After you do this, heroku will provide you with a URL for your application, such as http://blah-bleep-123.heroku.com. Now, the next step would be to associate your domain to this heroku URL.
- Configure your domain DNS Server as shown in this page. Mind you, after you change your DNS, it might take upto 48 hours for it to work. You can change your domain DNS by logging into the site where you bought your domain, for e.g. godaddy.com, hostingdude.com, etc.
Add this code to your ApplicationController. You can follow this from this page as well
class ApplicationController
before_filter :ensure_domain
APP_DOMAIN = 'www.mydomain.com'
def ensure_domain
if request.env['HTTP_HOST'] != APP_DOMAIN
# HTTP 301 is a "permanent" redirect
redirect_to "http://#{APP_DOMAIN}", :status => 301
end
end
end- Make sure you migrate all your database in heroku, by doing
heroku rake db:migrate
- After you have completed all these steps, you should be good. Check out your domain URL, everything should work pretty :).
- If you see any errors in your page, you can view the log by
heroku logs
- You can access console as
heroku console
With features as these, heroku is very convenient to work with.
Please let me know if you need more help.
Deploying Ruby on Rails - Is there a good alternative for Heroku?
Yes there are
This is a very good post I found on the subject
http://blog.sudobits.com/2013/02/15/heroku-alternatives-for-deploying-rails-applications
I went over the options there one by one and, to my humble opinion, OpenShift is the best option for a small-medium website, at least for the beginning of developing and creating a POC\Prototype
Why is it better?
- It gives you what Heroku will give you
- It give you local (persistant) storage - So you don't need to pay for S3 on Amazon or equivalent - I guess that in some point you would like to do that (to use S3), but at least for the begining you don't need to put money on it
- It seems that the website is running faster
- I find it more flexible in terms of controlling what is happening on the machine
Are there any disadvantages?
- The only disadvantage I could find on Openshift is that it seems that the deployment takes much more time then on Heroku and there is considerable delay on the first time you address the website (But after the first time is working faster - Not related to browser caching if you think this is the issue)
It might be that the Gemfile section under this document https://www.openshift.com/blogs/converting-an-existing-rails-app-to-run-on-openshift can solve this issue...I will try it later
What are the steps I need to do in order to deploy my already existing application on Openshift?
In many places I found explanations about how to create and deploy new application, but it was a little bit hard to understand how to put my already existing application on OpenShift
This is the reason I want to explain that
Assumptions
- Your application is git controlled already
- Its path is /home/dev/MyApp
- you are now under /home/dev
- Create Openshift account on https://www.openshift.com
- Use the command line tools, I found them much more informative
- Go to https://www.openshift.com/get-started
- Follow steps 1..3 in order to install and setup the command line tools
- cd to your rails application folder /home/dev/MyApp
- Create a new application domain - Run :
rhc domain-create <domain name>
Create your application in OpenShift by running
rhc app-create -a MyApp -t ruby-1.9 --no-git
-a sets the application name under OpenShift - It can be something totally different from your rails application name
-t sets the application type - I think that for now ruby 1.9 is their highest supported version
--no-git tells not to create git - Because we already got one
Setup your DB
Install the appropriate DB cartridge for your OpenShift application by calling
rhc cartridge add <DB cartridge type> -a <Application Name>
For example :
rhc cartridge add mysql-5.1 -a MyApp
It also supports MongoDB and PostgreSQL
(see here https://www.openshift.com/developers/technologies)
Change your database.yml to relate to the OpenShift database - Now, that is very easy since OpenShift got a lot of its configuration as environment variables and you can simply use it wherever you need - For example :
production:
adapter: mysql
encoding: utf8
database: <%=ENV['OPENSHIFT_APP_NAME']%>
pool: 5
host: <%=ENV['OPENSHIFT_MYSQL_DB_HOST']%>
port: <%=ENV['OPENSHIFT_MYSQL_DB_PORT']%>
username: <%=ENV['OPENSHIFT_MYSQL_DB_USERNAME']%>
password: <%=ENV['OPENSHIFT_MYSQL_DB_PASSWORD']%>
socket: <%=ENV['OPENSHIFT_MYSQL_DB_SOCKET']%>Make sure everything is working locally
- Run : 'bundle install'
- Run : 'rails s' - See that everything is OK
Git - Add the OpenShift repository as one of your remote repositories and push it
- Make sure all your work is updated , commited and syncronized with your GitHub - This can save a lot of headach later
- Run :
rhc app-show <application name>
- This will show you all the information about your application - Copy the Git URL - Run :
git remote add openshift <OpenShift repo URL>
Take whatever OpenShift is adding by merging
Run :
git merge openshift/master -s recursive -X ours
Commit the changes :
git commit -am 'adding OpenShift files
- Push to OpenShift :
git push openshift
That is all , now your application should be deployed on OpenShift
How do I open my deployed web site?
Using the rhc app-show <application name>
command you can see your website url
It will usually be http://<application name>-<domain name>.rhcloud.com
It is quite easy to change it to your own domain
- Just run
rhc alias add <app name> <your domain>
- Then in your DNS management - Edit the CNAME 'www' definition to point to
http://<application name>-<domain name>.rhcloud.com
How do I connect to my OpenShift machine?
Again , using rhc app-show <application name>
you can see the SSH address
Simply run ssh <SSH address>
in order to connect
How do I run the migrate and seed automatically upon deployment?
One nice thing in OpenShift is the ability to add custom actions (action-hooks) that are being triggered in different stages of the deployment
You can read more about it here https://www.openshift.com/developers/deploying-and-building-applications
For now, I will only talk about the deploy action-hook
- Under your application folder go to .openshift/action_hooks and create a file named
deploy
under it - Make this file executable - Run :
chmod +x deploy
Put some code into it
For example:
#!/bin/bash
echo "Starting deploy"
pushd ${OPENSHIFT_REPO_DIR} > /dev/null
echo "Change directory to ${OPENSHIFT_REPO_DIR}public"
cd ${OPENSHIFT_REPO_DIR}
cd public
echo "Creating soft link to ${OPENSHIFT_DATA_DIR}uploads named uploads"
ln -s ${OPENSHIFT_DATA_DIR}uploads uploads
echo "Running bundle exec rake db:migrate RAILS_ENV=production"
bundle exec rake db:migrate RAILS_ENV="production"
echo "Running bundle exec rake db:seed RAILS_ENV=production"
bundle exec rake db:seed RAILS_ENV="production"
popd > /dev/null
- The soft link will be explained later - it is needed for the routing to find the Carrierwave uploaded files
- Add the file to your git -
git add deploy
(from inside the folder of course) - Commit your changes and push to openshift remote
How can I integrate Carrierwave so I could upload files and save on OpenShift?
There are two points
- Where will the files be saved ? - This is easy
- Will the routing know how to fetch my uploaded files ?
Setting the saving path:
Set the initializers\carrierwave.rb file to be
CarrierWave.configure do |config|
if Rails.env.production?
config.storage = :file
config.enable_processing = true
config.root = ENV['OPENSHIFT_DATA_DIR']
config.cache_dir = config.root + 'uploads'
end
end
leave the uploaders as they are by default, meaning storage :file
and
def store_dir
"uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
end
The use of the $OPENSHIFT_DATA_DIR folder will make sure we will be able to write the files and that it will stay there
Making sure the routing will find the files:
It took me a while to come up with that trick (not too much, just a couple of hours)
Rails routing knows how to relate only the the folders that are under the application folder - soo, on OpenShift machine, it will look for the uploaded files folder (named uploads
in our case) under the ${OPENSHIFT_REPO_DIR}\public folder , this is where the soft link I put in the deploy file is becoming handy - It simply cheats the system and making it to go and fetch those file from a folder which is not under the application folder
I know there are a lot of posts about those issues and how to deploy and everything, but I just wanted to put some order into it.
Of course there might be mistakes and inaccuracies in what I wrote since I didn't documented every step of the way but just from what I remember, feel free to correct me if I'm wrong in some details.
One of the best sourced is the OpenShift documentation
I hope those things will help people and save you time and money
Enjoy
Why I get an Error when trying deploy Ruby on Rails app to Heroku?
OK, I solved the problem. Maybe it will help someone in the future:
- update Ruby version to 2.6.1
gem uninstall bundler
and install againgem install bundler -v 2.0.2
- delete Gemfile.lock and
bundle install
git push heroku master
- it works :)
Deploy Rails app to Heroku without using git?
Yes you can deploy to heroku without using git.
You can use a plugin heroku push. You can find it at https://github.com/ddollar/heroku-push.
Deploying Rails App via Heroku uses an old version of Node
Not sure exactly what it is, but the issue had something to do with the Heroku buildpack I was using (heroku-buildpack-bundler2)
Here's what I did to solve it:
$ heroku buildpacks:set heroku/ruby
$ heroku buildpacks:add --index 1 heroku/nodejs
$ git push heroku master
All works fine now :-)
Deploy Ruby on Rails App to Heroku with mysql database
It looks like you need change your gemfile to conform with Heroku's production standards as they use PostgreSQL:
group :production do
gem 'pg'
gem 'rails_12factor', '0.0.2'
end
gem 'mysql2'
After updating your gemfile, run bundle install
in your terminal. Git commit your changes and push to Heroku to see if that fixes the issue.
Why do people use Heroku when AWS is present? What distinguishes Heroku from AWS?
AWS / Heroku are both free for small hobby projects (to start with).
If you want to start an app right away, without much customization of the architecture, then choose Heroku.
If you want to focus on the architecture and to be able to use different web servers, then choose AWS. AWS is more time-consuming based on what service/product you choose, but can be worth it. AWS also comes with many plugin services and products.
Heroku
- Platform as a Service (PAAS)
- Good documentation
- Has built-in tools and architecture.
- Limited control over architecture while designing the app.
- Deployment is taken care of (automatic via GitHub or manual via git commands or CLI).
- Not time consuming.
AWS
- Infrastructure as a Service (IAAS)
- Versatile - has many products such as EC2, LAMBDA, EMR, etc.
- Can use a Dedicated instance for more control over the architecture, such as choosing the OS, software version, etc. There is more than one backend layer.
- Elastic Beanstalk is a feature similar to Heroku's PAAS.
- Can use the automated deployment, or roll your own.
Pushing the app to production on Heroku is failing
Your code is deployed just fine. But something about your application is throwing an error when you try to load the homepage.
Heroku is really friendly for checking the logs.
You can check the logs in your dashboard. The URL will be like this:
https://dashboard.heroku.com/apps/(yourappname)/logs
Or, install the Heroku Command Line Interface
then from the console, within your project file, do this:heroku logs -t
This will print out the server logs on your local console.
I'd bet a dollar that you actually forgot to perform a migration:
heroku run rails db:migrate
This is usually the thing I forget to do.
Related Topics
Where Does Ruby Keep Track of Its Open File Descriptors
How to Set the Httponly Flag on a Cookie in Ruby on Rails
Adding a "Like/Unlike" Button to a Post in Rails
How to Pass an Argument When Calling a View File
How to Modify the Input Type of the Rails Datetime_Select Helper
Delete Non-Utf Characters from a String in Ruby
Why Doesn't "Case" with "When > 2" Work
Passing Binding or Arguments to Erb from the Command Line
Ruby String with Usd "Money" Converted to Number
Reversing the Order of an Array in Ruby
Good Explanation of Ruby Object Model -- Mainly, 'Classes Are Objects'
What's the Difference Between "=" & "=>" and "@Variable", "@@Variable" and ":Variable" in Ruby
Facebook Open Graph from Rails Heroku
How to Select the Longest String from a Ruby Array