Skip to content

Instantly share code, notes, and snippets.

@tors
Forked from keikubo/README.md
Created June 11, 2012 08:04
Show Gist options
  • Save tors/2909005 to your computer and use it in GitHub Desktop.
Save tors/2909005 to your computer and use it in GitHub Desktop.
Nginx + Unicorn for Rails on Rackhub

Nginx + Unicorn for Rails on Rackhub

Description:

This script enables you to launch your Rails application in production environment (port:80) with Nginx and Unicorn.

Installation:

Please make sure that your Gemfile in your rails application includes unicorn.

gem 'unicorn'

Next, check whether you already set $RAILS_ROOT as follows.

echo $RAILS_ROOT

If you do not set it yet, please do so like below.

export RAILS_ROOT=$HOME/sample_app

Please make sure to replace sample_app to your actual Rails project path.

Then, just execute the following command.

bash -s stable < <(curl -s https://raw.github.com/gist/2129714/install.sh)

Now you may successfully check your Rails application in your browser.

Contact

© 2012 Kei Kubo [email protected]

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

www.apache.org/license/LICENSE-2.0

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.

#!/usr/bin/env bash
cd /tmp
git clone git://gist.github.com/2129714.git nginx-unicorn
cd nginx-unicorn
sed "s#RAILS_ROOT#$RAILS_ROOT#g" nginx.conf > /tmp/nginx.conf
sudo cp /tmp/nginx.conf /rhb/etc/nginx/conf.d/
./restart_nginx_and_unicorn.sh
rm -rf /tmp/nginx-unicorn
rm -rf /tmp/nginx.conf
upstream backend {
server unix:/tmp/.unicorn.sock.0;
server unix:/tmp/.unicorn.sock.1;
}
server {
listen 80;
server_name _; # all accept
log_format default_log '$host $remote_addr [$time_local] "$request" $status $request_length "$http_referer" "$http_user_agent" $request_time';
access_log /var/log/nginx/access.log default_log;
location ~ ^/assets/ {
root RAILS_ROOT/public;
gzip_static on; # to serve pre-gzipped version
expires 1y;
add_header Cache-Control public;
add_header ETag "";
break;
}
location / {
proxy_set_header HOST $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_pass http://backend;
proxy_redirect off;
}
}
sudo service nginx restart
pid=`cat $RAILS_ROOT/unicorn.pid`
kill -QUIT $pid
sleep 1
# unicorn -D -d -E production -c ./script/unicorn.rb
unicorn -D -d -E production -c /tmp/nginx-unicorn/unicorn.rb
rails_root = ENV["RAILS_ROOT"]
worker_processes 4
working_directory rails_root # available in 0.94.0+
listen "/tmp/.unicorn.sock.0", :backlog => 64
listen "/tmp/.unicorn.sock.1", :backlog => 64
timeout 30
pid rails_root + "/unicorn.pid"
stderr_path rails_root + "/log/unicorn.stderr.log"
stdout_path rails_root + "/log/unicorn.stdout.log"
preload_app true
GC.respond_to?(:copy_on_write_friendly=) and
GC.copy_on_write_friendly = true
before_fork do |server, worker|
defined?(ActiveRecord::Base) and
ActiveRecord::Base.connection.disconnect!
end
after_fork do |server, worker|
defined?(ActiveRecord::Base) and
ActiveRecord::Base.establish_connection
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment