#293 Nginx & Unicorn pro
Oct 24, 2011 | 19 minutes | Tools, Production
Nginx and Unicorn make a killer-combo for hosting a Rails application in production. Learn how to configure each in a Vagrant virtual machine in this episode.
- source codeProject Files in Zip (47.9 KB)
- mp4Full Size H.264 Video (57.9 MB)
- m4vSmaller H.264 Video (25.5 MB)
- webmFull Size VP8 Video (25.4 MB)
- ogvFull Size Theora Video (61.4 MB)
The latest version of Nginx on Ubuntu
apt-get install nginx
If the add-apt-repository ppa is not found install:
apt-get install python-software-properties
Thanks! Great episode. I would really like to know more about the benefits of unicorn over passenger. I've found some info in the comments on the github unicorn site.
Matt Freels says
Unicorn can restart your app without any lost requests or stalls.
Passenger drops your app and its workers, and then lets the next request bring the new version up.
I would say the biggest difference is that where Passenger is opinionated and fairly all-or-nothing, Unicorn gives you more transparency and control over its configuration (while still nicely managing workers).
Question is where is passenger ok to use? It seems easier to set up? (or is it?)
Does Passenger support http streaming in Rails3.1 ??
Yes it does.
But are there any clear instructions on how to do so George? Please see my question on SO: http://stackoverflow.com/questions/7867574/rails-3-1-1-http-streaming-with-apache-passenger
I'd appreciate it if you could point me to some good reference.
Dido on that request.
I discovered this link. It might be of help:
Thin vs unicorn vs passenger (http://www.ruby-forum.com/topic/1822610)
Hey Ryan B, read on #railscasts that you had the flu. Thanks for your Railscasts despite your being sick!
These deployment related screencasts are great! Keep them coming!
Yeah! read that on twitter too. Go to bed next time you get sick, we can wait. Hope you feel better.
+1 For more deployment screencasts please! I'd be fascinated to see your deployment process.
More episodes on deployment are planned, thanks for the feedback.
Nice railscast, I can't wait to see it with capistrano.
It would be interesting to show us how to setup and run multiple apps with different version of ruby and rails with this.
Take a look at this article. It's really easy. You'll need a reverse proxy and that's pretty much it
+1 Really looking forward to the Capistrano version. I've been running Unicorn + nginx for a little while now and I still learned a bunch from this intro railscast. Thanks again!
+1 Definitely looking forward to the Capistrano deployment episode.
My deployment recipe
Great timing, I was just investigating the topic as I start witching to ruby 1.9 and wanted to keep that as painless as possible.
Can't wait for the deployment screencast now!
You've inspired me to give Unicorn a try. Quick question: Don't you need to run the following after you've created the /etc/init.d/unicorn symlink to unicorn_init.sh so that Unicorn automatically starts when the server is rebooted?
I found this to be the case for a production server I'm running on Ubuntu. Can't wait to see more episodes on deployment. Thanks!
I didn't find this to be the case on my setup, but you may be right as I'm not the most proficient at Ubuntu. Thanks for mentioning.
I found this to be the case on my Linode running with lucid. Thanks for the tip.
I cannot seem to get that to work under Ubuntu. Any ideas?
is it also possible to run multiple ruby versions at the same time? (e.g. 1.8.7 and 1.9.2)
Yes, with rbenv or rvm.
Worth noting that you can use
bundle install --binstubs --shebang ruby-local-execinstead of replacing with
I can't find documentation for the
--shebangflag anywhere, but it has the desired effect.
Excellent episode, looking forward to chef/capistrano in the future!
--shebangoption is defined here in the source.
nginx chef cookbook using the nginx app template described in this episode.
I always let the apps define the rack server. Some are fans or Thin while others go with Unicorn (or Rainbows). Capistrano+Foreman are a good combo for this. This is how I go about deploys.
Hey, I'm having trouble with redirecting. I have the same nginx.conf as in the tutorial, and when I do
redirect_to posts_pathin my controller, I get redirected to
http://unicorn/posts, instead of
http://example.com/posts. What could've I missed?
Even if I do just
redirect_to '/', I get redirected to
I don't know what happened, but restarting nginx and unicorn multiple seems to have solved the problem and it hasn't reappeared yet. I still wonder what caused it though.
i am getting this error when try to run sudo service unicorn restart
/usr/bin/env ruby-local-exec no such file or direcotry
i am using rbenv and have done 'rbenv local 1.9.2-p290' in my project
what can be the issue?
I get the same thing. Followed the examples as well.
I'm on Debian Squeeze though.
I'm using rvm and have managed to deploy 1 app with a certain degree of success. I'm now struggling to deploy another where my gems are defined in a gemset. Unicorn seems to be expecting them in the global space. Any idea what I can stuff in my unicorn.rb to tell it to use my gemset in this instance?
Anyone have experience setting up progressive uploads with an nginx/Unicorn setup? If so, can you post a gist of your setup?
I think I followed every step of this screencast, but I could not start unicorn. It seems that the pid files are busy (?). Any idea on how to solve this problem?
I'm using unicorn 4.2.0, which is the newest version of the gem.
When I run " sudo service unicorn restart" I get:
And here is my sderr log:
andreteves, did you have any luck resolving this issue? I'm getting the same error?
Having the same issue on a windows host using a packaged box created on osx. I resolved moving the project dir out of the shared /vagrant folder. I think it's a permission issue of the shared folder on a windows host since I'm unable to see changes made in the windows side from the vm side.
I think I resolved this by removing the deferred option in config/nginx.conf
listen 80 default deferred;
Thanks so much Ryan.
I has been beating my head against the wall trying to deploy on a clean unix box for days. This was straight forward and super easy to follow. I also love using vagrant to test my setup!
Keep em coming, best $9 a month I can imagine sending.
I'm anxious to see your capistrano chapter to this series. Any ideas when you're going to do it? Would be a great final installment.
Would anyone know the equivalent fix for rvm (i.e ruby-local-exec) below:
rbenv local 1.9.2-p290
sed -i 's/env ruby/env ruby-local-exec/' bin/*
Hey Ryan, great post!
I'm just having a har time to figure out, how unicorn.rb should be with RVM ? Could you give us an example?
How different would the process be if the target was a non-vagrant Ubuntu server?
How would I ago about dealing with the absence of the vagrantfile?
I am trying to build a deployment that has the fewest possible components, and it seems to me that while nginx and unicorn are necessary, vagrant is not.
Your correct, vagrant is not necessary. You can set everything up as usual. But where you might ask?
On server, I create the projects directory under /var/rails and somtimes suffixed with ./client-foo/project-bar
This is in tradition where apache sites are stored under /var/www.
So in other words, instead of:
Hello, great screencast..
I don't think it's a good manner to get the unicorn started by symlinked script in /etc/init.d, because the script which can be changed by deployer user is ran by root user at boot so it makes huge security vulnerability when the deployer user account is hacked throught the webapp..
Probably better would be to use some gem like whenever and get the init script started by the cron "every :reboot"
Problem in Ubuntu Server
If I run $ /etc/init.d/unicorn start, it runs just fine.
If I run $ service unicorn start, I get this error:
/usr/local/rvm/rubies/ruby-1.9.3-p194/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:in 'require': cannot load such file -- bundler/setup (LoadError)
from /usr/local/rvm/rubies/ruby-1.9.3-p194/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:in 'require'
from /var/rails/mbiri/shs/bin/unicorn:14:in ''
I cannot figure out why /etc/init.d would work, but service would not. It's driving me crazy trying to find the answer. I also think this is the reason why unicorn won't start on boot, because I'v already ran update-rc.d
Solved! I received some help on this. I figured it had something to do with environment variables getting lost, and I was right. unicorn needs to have an rvm ruby wrapper around it, by executing:
rvm wrapper 1.9.3 ruby-1.9.3 unicorn
and it creates a new init script to run called:
afterwards you'll need to modify the unicorn_init.sh $CMD to read something like:
/usr/local/rvm/bin/ruby-1.9.3_unicorn -D -c $APP_ROOT/config/unicorn.rb -E production
See http://stackoverflow.com/questions/10454292/ubuntu-12-04-unicorn-init-sh-start-works-but-service-unicorn-init-start-do for more info.
I spent sooo much time on this same issue. Thank you for posting it.
Note that if you're using RVM for multiple sites, you'll want be more specific with your symlinks:
So I followed the episode exactly and I'm getting 404 errors for anything in my public folder? Any ideas why...
I know about the rails config option to serve_static_assets, but wouldn't I want nginx to do that?
I'd like to share Unicorn+Nginx configuration with preload_app and optimized deployment scripts: https://gist.github.com/3052776
Nice episode, one question... I am trying to integrate this with facebook login using omniauth, but the return url using this configuration will be "unicorn" what doesn't match the url on facebook app. How can I make facebook accept this configuration? Thanks for any help.
I keep getting this error when trying to run the
But I don't have any trouble running:
Can anyone help point me in the right direction?
I found a great unicorn init.d script working with
RVM init.d script
aaaaand it's broken
Does anyone have any ideas?
Hello, It works for me, but I can't make it run on production :( please help. I have no errors, and I have done exactly as the tutorial says. I'm on ubuntu, digital ocean.
I put <%= Rails.env %> on the third line
I have restarted everything a lot of times. and I'm pretty sure that the "- E production" flag it's on the right place.
Hi, Ryan, is this a typo ?
You have pointed to unicorn.todo.sock. What is in that file? Where can I find information?
In linux/unix, almost everything is a file. Unicorn simply listens on a socket instead of a TCP port.
I keep getting this error. There is no unicorn.sock file in that dir so I'm guessing that it's just not getting created?
The answer for me was to add
to my unicorn.rb file.