Chef cookbooks to set up a Rails production server (CentOS) - ruby-on-rails

So, I got tired of seting up production servers for my Rails apps all the time and decided to learn to use a tool that can simplify that process for me. I choose Chef-solo.
I've read the documentation and watched some tutorials, but I'm not realy sure I understend the process well, so I decided to go right to practice and learn on the way.
Now the real question: can someone provide me with a set of correctly working cookbooks to set up the server so I wouldn't need to reinvent the wheel myself?
OS - CentOS v.6.0-6.3
Software:
Development Tools (groupinstall)
git
Node.js
ImageMagick
Nginx and Unicorn
MySQL and PosgreSQL
Mail Server POP/SMTP/IMAP
RVM + ruby
MemCache
Think that's it.
Ofcource I need not only to install the packages, but also to configure them properly.
Any suggestions or additional info needed?

You can try looking at my repo at https://github.com/dmytro/chef-solo
I am using it to bootstrap Rails environments for myself.
Additionally it can be combined with Capistrano deployment. I use this in my capistrano recipes https://github.com/dmytro/capistrano-recipes
I've put blog post on these. Please lake a look too: http://dmytro.github.io/2013/05/21/easy_spawn_new_hosts_with_capistrano.html

Related

Config rails server on vps hosting

I must configure my RoR vps hosting for my rails app, but on my localmachine i simple use webrick...
Are any ways to do this on server, so that it will be faster, and also, if it reboot's or memory down, or something that call's shutdown of server, then it automatically starts?
Also how about rails enterprise?
Now i have clean ubuntu server, and rvm on it
Follow this tutorial, it's great for beginner and will answer all your questions: http://coding.smashingmagazine.com/2011/06/28/setup-a-ubuntu-vps-for-hosting-ruby-on-rails-applications-2/
Which webserver to use depends on many things. Among others, there's Thin, Unicorn, Rainbows, Puma, and counting.
I suggest you look on google (or even here, I'm pretty this was answered before) for comparisons between ruby webservers, so that you can know what situation each of them are adapted to, and which one will fit your needs best.
For production deployment you need to install nginx or apache,
nginx is lightweight ,consider installing nginx,
Now there are various ways simple and easy to install is passenger with nginx
Or simply you can use nginx+unicorn or nginx+thin you can find various article just google it.

What, where and how to upload Ruby on Rails application files to the VPS?

I am using Ruby on Rails 3.0.9 and I would like to publish my web site. I already set my VPS running Ubuntu 10.04 LTS and the capistrano gem (this one I think as well as possible). Now, what I need to do is to upload all files to the www/project_name directory (I am on Mac OS)...
What I have to do to accomplish that?
You don't need your deployment machine to have Capistrano. Capistrano automates a bunch of tasks that I suggest you do at least one manually so you know what's going on. Sooner or later, you'll be debugging some Capistrano task, so you may as well figure out the guts sooner or later.
Coarsely, what you need to do is to basically duplicate your development environment on your production machine. If you have it on version control, you can git clone or svn whateveritis on your production machine. If not, you can scp it over with scp /local/rails/dir remoteuser#remotehost:www/projectname.
At this point, you should actually do the remainder of the work on the server. Since you've managed to install Capistrano, I assume you're familiar with the basics of making your way around SSH.
Once the code's over, you have to install the prerequisites. If you're using 3.0.9 you should be able to run bundle install --deployment, where the deployment flag basically tells bundler to use the identical gem set as on your development machine.
When that's done, actually getting the server online will vary based on your setup. If you're using non-standalone passenger, just follow any of the many guides at this point. If you're running standalone passenger or thin or unicorn or any other standalone rails server, go ahead and start that in daemon mode (so it won't quit on you when you end your SSH session) and make sure you se the production flag. You can either start it in sudo and have it listen on port 80 (e.g., sudo thin start -d -p 80) or have it listen on a higher-number port and use a reverse proxy on your WWW-facing server. The instructions for how to reverse proxy are all over the internet.
Let me know if you have any questions.
You have half of a deployment solution with Capistrano. Commonly Passenger is used as the other half, which sits on the server and loads your app. To accomplish this, usually, SSH keys are used. There are numerous tutorials on how to set this up. One of my favorites written by Dan Benjamin can be found on his blog Hivelogic.
Edited to provide more begginer info:
Capistrano begginer's guide from the Capistrano wiki.
Passenger Stand Alone Guide from the Passenger website.
Be sure to check out the other guides for the webserver of your choice when you're ready.
These guides will give you the background you need to get a local Passenger & Capistrano deployment going. These guides provide the knowledge you need to get achieve what you want.
Simple and short sample of deployment via SSH http://alexeypetrushin.github.com/vfs/ssh_deployment.html

Is there any advantage to using WEBrick in development mode for Rails?

I develop on a Mac, and I'm pretty comfortable setting up an Ubuntu VM with Apache installed, so "you don't need to install Apache" is not really a reason. I run Ruby from the VM anyway, so there doesn't seem to be much difference in system resources needed.
In my head, the advantage of using Apache is that it's going to perform more consistently with my production server.
So what reasons are there to use WEBrick? Does it make development easier somehow?
No there are no advantages. The only "advantage" is that it is the pre-bundled webserver so you at least "have something"
I'm sure most experienced Ruby devs use something else like thin, mongrel, unicorn, pow, passenger, etc...
I'd go for phusion passenger + passsenger pref pane: http://www.fngtps.com/2008/06/putting-the-pane-back-into-deployment
Awesomeness. This way I can act more like the production server and take it easy when adding new projects etc.
I've been using Rails for a school project, and I find the colorized logs helpful when I'm trying to figure out what the server is doing. Sure you could tail -f the apache logs, but color is nice IMO.

Rails application on shared hosting?

Is there a way that I can deploy my locally made rails app on a shared host that has Ruby installed?
Capistrano to deploy and (Mongrel or Passenger... preferably the latter... or if you HAVE to, FastCGI although FastCGI is pretty slow with Rails) to serve.
If you have a shared host you'll have to vendor your Rails version and all your gems, as you probably won't have access to install gems. Even if you did, you won't want Rails picking old versions of things.
Personally, I would use Capistrano. There are plenty of books and tutorials around that will tell you how to use it to deploy Rails applications. It is very easy to configure.
Dreamhost supports Rails, many others probably do also.
As for installation of your app: it depends a bit on what's already available on your shared host, and what you're allow to do / install / configure there. For Dreamhost, here's their documentation. Other shared hosting solutions may or may not be similar depending on their policy.
The best way I've found is to install the passenger gem. Once you've uploaded your app to your account all you need to do is add a few lines to your .htaccess file and you should be off.
Definitely capistrano + git (git, if you have SSH access, like f.e. in HostingRails shared host), nothing can be easier.
Neat Capistrano guide: http://www.linuxjournal.com/article/10100
How to config Capistrano + git: http://github.com/guides/deploying-with-capistrano
Some nice Capistrano recipes: http://github.com/josh/slicehost
Assuming your http server is nginx or apache, install Phusion Passenger
On the Passenger site, there are very good docs for configuring Apache/nginx with Rails
As a first step, just copy your Rails app somewhere to the host and get Passenger working
Once you have that working, you will need a better way to deploy/update the app; as others have suggested, Capistrano is a popular way to do it and you should have no problem finding some tutorials online to help you do it
I have done steps 1-3 and it is incredibly easy and works perfectly fine (I was just too lazy to do step 4).
Found a good reference : REFERENCE LINK this will help to deploy ROR App on a shared host.

Why is it supposedly "hard" to deploy Ruby on Rails to production?

I admit that I don't follow much of anything "right" on deploying test versus production code. I have been using ASP.NET, and I typically run it locally in Visual Studio, it works, I upload it, I test it again on the production server.
I have read several people say that deploying Rails apps is harder and there are special programs/ways on the ruby site about deploying RoR. I've only toyed with RoR. What is special about deployment? You don't just copy and paste the code and run it (from development machine to the production)? Is it because one is in Apache and the other running on the built in server?
This will be on a Mac Server if it matters.
Deploying RoR is not difficult anymore, especially with Phusion Passenger.
What is somewhat difficult, is getting a automated production environment setup with capistrano, vlad, etc. If you don't mind simply copying your code to the server, you can do that just fine. Most people choose not to do it that way because you lose out on a lot of the benefits that the automated deployment tools give you.
I guess people consider a Rails app harder to deploy than say some PHP apps or such where you just plop the code somewhere and point Apache or whatever at it. But, as mentioned above, you could do that now with Phusion Passenger.
We use Nginx+Passenger, but not for simplicity of deployment. Capistrano is our deploy tool of choice, and really, unless you have a very simple app, you're going to want something like Capistrano anyway. For example, with our deploy, we do a slew of things:
run any database migrations
generate release notes automatically, based on all the commits to Git between the last deploy and this one
notify various people via email (with differing lists depending on whether the deploy is to our staging environment or production) - we do this via cap_gun which integrates with Capistrano.
Notify New Relic RPM of the deploy so it can mark it in our RPM analysis
Notify Hoptoad of the deploy, so it too can have that data when reporting any exceptions
produce our sitemap.xml file, and ping Google to tell them there's a new one
update crontab files (I store our crontab files for each server in our git repo, and then on deploy it sees if there is a new version and updates accordingly, etc.).
flush/restart memcached
There are other ways aside from Capistrano, but it's a proven tool, with a lot of flexibility, yet pretty simple to setup a vanilla configuration.
So, my take is that once you get into any app that is beyond just the very simplest of apps, you're going to need/want to be doing things other than just simply updating the code. In the beginning though, if you just need the code updates, and maybe Rails migrations, then you can do simpler things like Passenger and code sync, or look at tools like Heroku or Engine Yard's stuff where they do a deploy by doing a Git clone (and then offer some additional abilities).
Another super easy way to deploy is with http://heroku.com/
Some of the issues you face with deploying rails to production:
Database connection.
You need to be sure that the database connector is set up for the production environment.
Database migrations.
You have to run database migrations against the production database even though you may have already run them in production/testing/staging
Ruby version. The version or sub-version or Ruby can trip you up, e.g. An error occurred while installing debugger-linecache (1.1.1), and Bundler cannot continue
Gem dependency.
Your production environment may have different packages and gems from development. Bundler will figure this out for the most part and install the dependencies but occasionally there are still issues that you have to resolve manually.
Dependencies.
Some gems on some machine have particular dependencies. I have seen frequent problems with using gems on my unix box that work on OSX and vice-versa.
Note the last 3 shouldn't affect you if on the same machine but I included them based on the title and to be comprehensive.
It's not especially hard. If you stick to conventions then with a little bit of configuration it boils down to this:
cap deploy
...however there is sometimes a bit of effort needed up front to get the workflow in place.
The good news is that lots of people have packaged up solutions and stacks for RoR that you can just plug and play. For example, google ec2onrails - this is a packaged Ubuntu image and set of capistrano tasks for running rails apps in Amazon's EC2 cloud, with lots of common stuff set up already out of the box.
Choose a good hosting provider and you should be able to find something similar for that also.
An easy way to deploy Rails apps is to use Phusion Passenger. Deployment doesn't get much easier than that for any programming language or framework. You can do that on a Mac server.
Another really easy way to deploy rails is with jruby and the glassfish gem.

Resources