I'm starting a new small venture, a POC if you would like, and I want to deploy my Rails application for free somewhere. I found that there is Heroku, are there another options?
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?
Are there any disadvantages?
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
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
Git - Add the OpenShift repository as one of your remote repositories and push it
rhc app-show <application name>
- This will show you all the information about your application - Copy the Git URLgit 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
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
rhc alias add <app name> <your domain>
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
deploy
under itchmod +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
git add deploy
(from inside the folder of course)How can I integrate Carrierwave so I could upload files and save on OpenShift?
There are two points
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