Nginx, Redis, Memcached, Ruby with RVM, Unicorn or passenger, Mysql or Postgresql
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:
Requirements Ubuntu 12.04+
0. Create server deploy user (optional)
sudo adduser deploy --gecos "" --disabled-password
sudo cp -R .ssh/ /home/deploy/
sudo chown -R deploy:deploy /home/deploy/
1. Clone code and setup repo.
git clone https://github.com/Shehbaz/chef-rails.git
cd chef-rails
bundle install
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.
2. Server preparation for deployment
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.
3. Specification for the server
The above command will have generated a file called[highlight3][host].json[/highlight3] in the[highlight3]chef/nodes/[/highlight3] 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
4. Let the magic happen
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.
5. Check the server
ssh into server and go to[highlight3]/user/deploy[/highlight3], 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 create[highlight3]current/config/unicorn.rb[/highlight3] and voila, everything is setup and ready to use. Put the code in current or deploy with Capistrano and its ready to use.
6. Clean the server
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
The code is originally forked from https://github.com/acidlabs/chef-rails and all the credit goes to Acid Labs for their wonderful effort.