This set of scripts and config files will help you set up the awesome combination of Unicorn and Nginx as a server environment for Ruby web applications on Red Hat's OpenShift platform while I finish my cartridge.
- Before you get started, you should read my post on how to set up Ruby 1.9 environment on OpenShift here: http://goo.gl/ufI5G This will (hopefully) get you started on building a Rails app on OpenShift the unofficial way (for now!).
Before you do anything, ssh into your application shell and set a very important environment variable:
$ declare -x RAILS_ROOT=~/appname/repo/appname
Substitute "appname" for your app's name of course.
You'll also want to make sure Unicorn is included in your Gemfile and installed on the OpenShift box. I advise making this edit locally and pushing your changes up to the server to make things line up nicely - after all you should be testing/developing in the same environment as the server to prevent crazy bugs. If you don't have a post-deploy script set up to run bundle install
, then do that at the same time.
Now you can get the bits which will do a lot of the work for you.
$ cd $OPENSHIFT_DATA_DIR
$ git clone git://gist.github.com/2832578.git nginx-unicorn
$ chmod +x nginx-unicorn/install-nginx-unicorn.sh
$ nginx-unicorn/install-nginx-unicorn.sh
You should now be able to sit back and watch it download/build/install everything into the right places. Once it's done, edit your action hooks from within your local repo and add the following:
# make sure we can use rvm and bundle - you should have set up a gemset for your app already!
source $OPENSHIFT_DATA_DIR/.rvm/scripts/rvm
rvm use 1.9.3-p125@appname
# start the nginx http server
$OPENSHIFT_DATA_DIR/nginx/sbin/nginx
# start the unicorn backend server
bundle exec unicorn_rails -c ~/appname/repo/appname/config/unicorn.rb -
# kill nginx+unicorn through their pidfile
kill `cat $OPENSHIFT_REPO_DIR/appname/tmp/pids/unicorn.pid`
kill `cat $OPENSHIFT_REPO_DIR/appname/tmp/pids/nginx.pid`
Now push these up to your app server. You might want to try starting the app server through SSH first time round to make sure nothing funky happens:
$ app_ctl stop && app_ctl start
All going well, your application should now be served by the magical combo of Nginx and Unicorn!
I have noticed that the first few requests can be painfully slow as the servers start spinning properly (part of this could also be my horrible connection). This is to be expected really as, from the client side and because of the OpenShift architecture, the traffic has to pass like so:
<----------REQUEST---------><--------RESPONSE--------->
Browser->Apache->Nginx->Unicorn->Nginx->Apache->Browser
Passing through three servers as well as all the routing is going to take a while the first time round! The upside is that this combination caches really well and requests only get better the higher they go. Some tweaking is probably needed but for now (tip: try the worker_processes
var in the unicorn config file for starters), this works until I can get round to finishing my cartridges.
© 2012 Mark Anthony Gibbins [email protected]
Twitter: @xiy
Blog: http://pyramidthoughts.wordpress.com
Licensed under the MIT license.
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.