Setup Ruby on Rails Server Quickly
With so many options for servers like DigitalOcean, AWS, Azure etc. setting up Ruby on Rails Server and transfer has become one of the common tasks. As a rails shop setting up production server is not our priority. But one cannot deny that it is one of the tasks which we have to perform regularly. We found out that setting up a production environment and then fine tuning it to our needs can take one complete day or in some situations, more then one day. As a company comprising of mainly developers, we thought there should be a way to automate it. Also I have to admit that we cannot afford system admin on most of our projects. Interestingly we found some tools which can automate server setup and out of them Puppet and Chef looked promising. We selected chef because it was in ruby and was quite close to the concepts we already are used to, like gem bundle etc. You can find the details on Chef, knife-solo and librarian-chef by googling them, they will be used to setup the server.
Even if you are not aware of above mentioned concepts and do not have time to learn it, the following tutorial can be used but it is recommended you take a look at the mentioned concepts first.
Lets get to business and setup our server in few easy steps:
[subtitle3]Requirements[/subtitle3] Ubuntu 12.04+
[subtitle3] 0. Create server deploy user (optional) [/subtitle3]
sudo adduser deploy --gecos "" --disabled-password
sudo cp -R .ssh/ /home/deploy/
sudo chown -R deploy:deploy /home/deploy/
[subtitle3] 1. Clone code and setup repo.[/subtitle3]
git clone https://github.com/Shehbaz/chef-rails.git
bundle exec librarian-chef install
Use master branch if you want to go for postgres and passenger combination. Use unicorn-mysql branch for Unicorn and Mysql combination.
[subtitle3] 2. Server preparation for deployment[/subtitle3]
bundle exec knife solo prepare [user]@[host]
The above command will install the chef on the host, that is required to take care of installing all the required libraries we are going to need.
[subtitle3] 3. Specification for the server[/subtitle3]
The above command will have generated a file called[host].json in thechef/nodes/ directory. You will need to edit and add information in it. To make your life easy, there is a localhost.json.example also present, you can use it as guide or simple copy paste it.
Take a look at sample Host Example
[subtitle3] 4. Let the magic happen[/subtitle3]
bundle exec knife solo cook [user]@[host]
Sit back and relax, it will install everything you need on your server and configure it for use. You can edit [host].json to add more things, remove stuff or whatever you feel happy to do. After the command is finished, you will have a ready to use server.
[subtitle3] 5. Check the server[/subtitle3]
ssh into server and go to/user/deploy, the above recipe is configured to work with Capsitrano so create a current folder if you are not planning to use it. You also need to createcurrent/config/unicorn.rb and voila, everything is setup and ready to use. Put the code in current or deploy with Capistrano and its ready to use.
[subtitle3] 6. Clean the server[/subtitle3]
bundle exec knife solo clean [user]@[host]
Thats it, see how easy it was to setup Ruby on Rails server.
I will be writing another post which will cover rails application production ready application, which you can use with this code and with few commands you will have a running production server and Ruby on Rails application