Skip to content

Instantly share code, notes, and snippets.

@ideomix
Forked from keikubo/README.md
Created June 19, 2012 15:47

Revisions

  1. @keikubo keikubo revised this gist Jun 14, 2012. 2 changed files with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion README.md
    Original file line number Diff line number Diff line change
    @@ -23,7 +23,7 @@ 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)
    curl -s -L https://gist.github.com/raw/2129714/install.sh | bash

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

    Empty file modified restart_nginx_and_unicorn.sh
    100755 → 100644
    Empty file.
  2. @keikubo keikubo revised this gist Jun 13, 2012. 3 changed files with 18 additions and 11 deletions.
    19 changes: 12 additions & 7 deletions install.sh
    Original file line number Diff line number Diff line change
    @@ -1,12 +1,17 @@
    #!/usr/bin/env bash

    cd /tmp
    git clone git://gist.github.com/2129714.git nginx-unicorn
    cd nginx-unicorn
    DIR=nginx-unicorn
    SCRIPT_HOME=~/$DIR

    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
    cd
    git clone git://gist.github.com/2129714.git $DIR
    cd $SCRIPT_HOME

    sed "s#RAILS_ROOT#$RAILS_ROOT#g" $SCRIPT_HOME/nginx.conf > /tmp/nginx.conf
    sudo cp /tmp/nginx.conf $SCRIPT_HOME/nginx.conf
    sudo ln -s $SCRIPT_HOME/nginx.conf /usr/local/nginx/conf.d/
    export SCRIPT_HOME=$SCRIPT_HOME
    sudo rm -rf /usr/local/nginx/conf.d/default
    $SCRIPT_HOME/restart_nginx_and_unicorn.sh

    rm -rf /tmp/nginx-unicorn
    rm -rf /tmp/nginx.conf
    5 changes: 3 additions & 2 deletions nginx.conf
    Original file line number Diff line number Diff line change
    @@ -3,10 +3,11 @@ upstream backend {
    server unix:/tmp/.unicorn.sock.1;
    }

    log_format default_log '$host $remote_addr [$time_local] "$request" $status $request_length "$http_referer" "$http_user_agent" $request_time';

    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/ {
    @@ -22,7 +23,7 @@ server {
    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_set_header X-Forwarded-Proto $http_x_forwarded_proto;
    proxy_pass http://backend;
    proxy_redirect off;
    }
    5 changes: 3 additions & 2 deletions restart_nginx_and_unicorn.sh
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,7 @@
    sudo service nginx restart
    sudo nginx -s stop
    sudo nginx
    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
    unicorn -D -d -E production -c $SCRIPT_HOME/unicorn.rb
  3. @keikubo keikubo revised this gist Mar 20, 2012. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -15,11 +15,11 @@ Next, check whether you already set $RAILS_ROOT as follows.

    echo $RAILS_ROOT

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

    export RAILS_ROOT=$HOME/sample_app

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

    Then, just execute the following command.

  4. @keikubo keikubo revised this gist Mar 20, 2012. 1 changed file with 0 additions and 2 deletions.
    2 changes: 0 additions & 2 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -11,8 +11,6 @@ Please make sure that your Gemfile in your rails application includes unicorn.

    gem 'unicorn'

    For executing Nginx + Unicorn, please set $RAILS_ROOT first if you do not set it yet.

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

    echo $RAILS_ROOT
  5. @keikubo keikubo revised this gist Mar 20, 2012. 1 changed file with 9 additions and 1 deletion.
    10 changes: 9 additions & 1 deletion README.md
    Original file line number Diff line number Diff line change
    @@ -11,10 +11,18 @@ Please make sure that your Gemfile in your rails application includes unicorn.

    gem 'unicorn'

    For executing Nginx + Unicorn, please set $RAILS_ROOT first.
    For executing Nginx + Unicorn, please set $RAILS_ROOT first if you do not set it yet.

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

    echo $RAILS_ROOT

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

    export RAILS_ROOT=$HOME/sample_app

    Please make sure that you 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)
  6. @keikubo keikubo revised this gist Mar 20, 2012. 1 changed file with 5 additions and 1 deletion.
    6 changes: 5 additions & 1 deletion README.md
    Original file line number Diff line number Diff line change
    @@ -7,7 +7,11 @@ This script enables you to launch your Rails application in production environme

    ## Installation:

    First, please set $RAILS_ROOT.
    Please make sure that your Gemfile in your rails application includes unicorn.

    gem 'unicorn'

    For executing Nginx + Unicorn, please set $RAILS_ROOT first.

    export RAILS_ROOT=$HOME/sample_app

  7. @keikubo keikubo revised this gist Mar 20, 2012. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion restart_nginx_and_unicorn.sh
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,4 @@
    sudo service nginx start
    sudo service nginx restart
    pid=`cat $RAILS_ROOT/unicorn.pid`
    kill -QUIT $pid
    sleep 1
  8. @keikubo keikubo revised this gist Mar 20, 2012. 2 changed files with 1 addition and 6 deletions.
    2 changes: 0 additions & 2 deletions nginx.conf
    Original file line number Diff line number Diff line change
    @@ -1,8 +1,6 @@
    upstream backend {
    server unix:/tmp/.unicorn.sock.0;
    server unix:/tmp/.unicorn.sock.1;
    server unix:/tmp/.unicorn.sock.2;
    server unix:/tmp/.unicorn.sock.3;
    }

    server {
    5 changes: 1 addition & 4 deletions unicorn.rb
    Original file line number Diff line number Diff line change
    @@ -6,9 +6,6 @@

    listen "/tmp/.unicorn.sock.0", :backlog => 64
    listen "/tmp/.unicorn.sock.1", :backlog => 64
    listen "/tmp/.unicorn.sock.2", :backlog => 64
    listen "/tmp/.unicorn.sock.3", :backlog => 64
    listen 8000, :tcp_nopush => true

    timeout 30

    @@ -29,4 +26,4 @@
    after_fork do |server, worker|
    defined?(ActiveRecord::Base) and
    ActiveRecord::Base.establish_connection
    end
    end
  9. @keikubo keikubo revised this gist Mar 20, 2012. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion README.md
    Original file line number Diff line number Diff line change
    @@ -25,4 +25,4 @@ Licensed under the Apache License, Version 2.0 (the "License"); you may not use

    [www.apache.org/license/LICENSE-2.0](http://www.apache.org/licenses/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.
    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.
  10. @keikubo keikubo revised this gist Mar 20, 2012. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion restart_nginx_and_unicorn.sh
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,5 @@
    sudo service nginx start
    pid=`cat unicorn.pid`
    pid=`cat $RAILS_ROOT/unicorn.pid`
    kill -QUIT $pid
    sleep 1
    # unicorn -D -d -E production -c ./script/unicorn.rb
  11. @keikubo keikubo revised this gist Mar 20, 2012. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion README.md
    Original file line number Diff line number Diff line change
    @@ -21,7 +21,7 @@ Now you may successfully check your Rails application in your browser.

    &copy; 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
    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](http://www.apache.org/licenses/LICENSE-2.0)

  12. rackhuber revised this gist Mar 20, 2012. 1 changed file with 28 additions and 0 deletions.
    28 changes: 28 additions & 0 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,28 @@
    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:

    First, please set $RAILS_ROOT.

    export RAILS_ROOT=$HOME/sample_app

    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

    &copy; 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](http://www.apache.org/licenses/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.
  13. rackhuber revised this gist Mar 20, 2012. 3 changed files with 17 additions and 4 deletions.
    12 changes: 12 additions & 0 deletions install.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,12 @@
    #!/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
    4 changes: 2 additions & 2 deletions nginx.conf
    Original file line number Diff line number Diff line change
    @@ -12,7 +12,7 @@ server {
    access_log /var/log/nginx/access.log default_log;

    location ~ ^/assets/ {
    root $RAILS_ROOT;
    root RAILS_ROOT/public;
    gzip_static on; # to serve pre-gzipped version
    expires 1y;
    add_header Cache-Control public;
    @@ -28,4 +28,4 @@ server {
    proxy_pass http://backend;
    proxy_redirect off;
    }
    }
    }
    5 changes: 3 additions & 2 deletions restart_nginx_and_unicorn.sh
    100644 → 100755
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,6 @@
    sudo service nginx start
    pid=`cat unicorn.pid`
    kill -QUIT $pid
    sleep 3
    unicorn -D -d -E production -c ./unicorn.rb
    sleep 1
    # unicorn -D -d -E production -c ./script/unicorn.rb
    unicorn -D -d -E production -c /tmp/nginx-unicorn/unicorn.rb
  14. @keikubo keikubo revised this gist Mar 20, 2012. 2 changed files with 1 addition and 2 deletions.
    2 changes: 0 additions & 2 deletions install.sh
    Original file line number Diff line number Diff line change
    @@ -1,2 +0,0 @@
    sudo service nginx start
    ./restart_unicorn.sh
    1 change: 1 addition & 0 deletions restart_unicorn.sh → restart_nginx_and_unicorn.sh
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,4 @@
    sudo service nginx start
    pid=`cat unicorn.pid`
    kill -QUIT $pid
    sleep 3
  15. @keikubo keikubo revised this gist Mar 20, 2012. 1 changed file with 0 additions and 1 deletion.
    1 change: 0 additions & 1 deletion install.sh
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,2 @@
    sudo service apache2 stop
    sudo service nginx start
    ./restart_unicorn.sh
  16. @keikubo keikubo created this gist Mar 20, 2012.
    3 changes: 3 additions & 0 deletions install.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,3 @@
    sudo service apache2 stop
    sudo service nginx start
    ./restart_unicorn.sh
    31 changes: 31 additions & 0 deletions nginx.conf
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,31 @@
    upstream backend {
    server unix:/tmp/.unicorn.sock.0;
    server unix:/tmp/.unicorn.sock.1;
    server unix:/tmp/.unicorn.sock.2;
    server unix:/tmp/.unicorn.sock.3;
    }

    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;
    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;
    }
    }
    4 changes: 4 additions & 0 deletions restart_unicorn.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,4 @@
    pid=`cat unicorn.pid`
    kill -QUIT $pid
    sleep 3
    unicorn -D -d -E production -c ./unicorn.rb
    32 changes: 32 additions & 0 deletions unicorn.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,32 @@
    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
    listen "/tmp/.unicorn.sock.2", :backlog => 64
    listen "/tmp/.unicorn.sock.3", :backlog => 64
    listen 8000, :tcp_nopush => true

    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