Skip to content

Instantly share code, notes, and snippets.

@mrsweaters
Forked from scottlowe/etc_init.d_unicorn_example.co.uk
Created October 23, 2011 17:10
Show Gist options
  • Save mrsweaters/1307589 to your computer and use it in GitHub Desktop.
Save mrsweaters/1307589 to your computer and use it in GitHub Desktop.
Ruby on Rails server setup on Ubuntu 11.04 with Nginx, Unicorn, Rbenv
upstream example-workers {
# fail_timeout=0 means we always retry an upstream even if it failed
# to return a good HTTP response (in case the Unicorn master nukes a single worker for timing out).
server unix:/tmp/example.co.uk.socket fail_timeout=0;
}
server {
listen 80; # default;
server_name example.co.uk;
root /home/example.co.uk/website/public;
location / {
access_log off;
include proxy_params;
proxy_redirect off;
if (-f $request_filename) {
access_log off;
expires max;
break;
}
if (-f $request_filename.html) {
rewrite (.*) $1.html break;
}
if (!-f $request_filename) {
proxy_pass http://example-workers;
break;
}
}
}

Server Commissioning

Ubuntu mainstream packages are pretty out of date for nginx; we want version > 1.0, so we need to reference repository that has more recent versions before we install:

$ add-apt-repository ppa:nginx/stable && apt-get update

Update, upgrade and install nginx and development tools:

$ apt-get -y install nginx git-core build-essential

Extras for RubyGems and Rails:

$ apt-get -y install zlib1g-dev
$ apt-get -y install libssl-dev libsqlite3-dev
$ apt-get -y install libreadline5-dev
$ apt-get -y install curl

Add a deployment user:

$ useradd -m -g staff -s /bin/bash deployer
$ passwd deployer

Create a custom shudders file, and add the following line (sudo vi /etc/sudoers.d/our-company):

%staff ALL=(ALL) ALL

Nginx

Edit /etc/nginx/proxy_params and add shared proxy config settings (optional)

proxy_set_header Host $host;

# needed to forward user's IP address to application server
proxy_set_header X-Real-IP $remote_addr;

proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header   X-Forwarded-Proto $scheme;

proxy_send_timeout         90;
proxy_read_timeout         90;

proxy_buffer_size          4k;
proxy_buffers              4 32k;
proxy_busy_buffers_size    64k;
proxy_temp_file_write_size 64k;

Ruby & Rails Setup ror each User / App

$ sudo adduser --shell /bin/bash example-user
$ su - example-user
$ cd ~example-user

Install rbenv:

Check out rbenv into ~/.rbenv :


$ git clone git://github.com/sstephenson/rbenv.git .rbenv

Add ~/.rbenv/bin to your $PATH for access to the rbenv command-line
:

$ echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >>   
$  ~/.bash_profile

Add rbenv init to your shell to enable shims and autocompletion:


$ echo 'eval "$(rbenv init -)"' >> ~/.bash_profile

Restart your shell so the path changes take effect in order to use rbenv:


$ exec $SHELL

If the above shell reload doesn't give you the rbenv command, then you will have to exit and re-enter the shell

Use rbenv to install a specific Ruby version
:

$ rbenv install 1.9.2-p290

Rebuild the shim binaries. You should do this any time you install a new Ruby binary e.g. when installing a new Ruby version, or when installing a gem that provides a binary:

$ rbenv rehash

Set a global Ruby version for all shells:

$ rbenv global 1.9.2-p290

Unicorn

$ gem install bundler unicorn --no-rdoc --no-ri; rbenv rehash

Clone your app into ~/website (depends upon if you are using git, or some other source), and then install your bundle:

$ cd ~/website
$ bundle install

create app-specific unicorn init file here and make it executable:

$ sudo chmod +x /etc/init.d/unicorn_example.co.uk  

Check that unicorn can be started:

$ /etc/init.d/unicorn_example.co.uk start

Check that unicorn is listening on the configured port (8080 in this example):

$ netstat -natp | grep unicorn

You should see something like:

Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name
tcp        0      0 0.0.0.0:8080            0.0.0.0:*               LISTEN      5272/unicorn.rb -E 

Nginx virtual hosts

Create an Nginx virtual hosts configuration file in 'sites-available' and enter contents of nginx_virtual_host file:

$ sudo vi /etc/nginx/sites-available/example.co.uk

Create a symlink from sites-available to site-enabled:

$ sudo ln -s /etc/nginx/sites-available/example.co.uk /etc/nginx/sites-enabled/example.co.uk
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment