Hi, welcome to Word of Mike, my little corner of the internet. I am a Software/Web Developer working in North Yorkshire. I mainly write about programming but my other passion is politics so beware. (click to hide)

2012-10-21 15:12:47 UTC

Setting Up A Ubuntu Server with Nginx & Passenger for Ruby on Rails


Been having to do quite a bit of this kind of thing at work lately, and in order to make it as painless as possible for myself, I've had to write it down. So here are my notes on building a Ubuntu, Nginx, Passenger & Rails stack from a clean Ubuntu 12.04 server image.

First things first, note that I do not use RVM like every other guide on the internet suggests, because frankly I've wasted so many hours on it, and it's so easy to install Ruby from source or use the package manager that I just can't see the point in using it in production at all (unless you need multiple rubies for whatever reason).

I recommend creating a 'deploy' user to use for ... deployment. Here are my notes in their raw form, I'll probably come back and tart this up when I get the time:

  1. Alias the server for SSH:
  2. on your local machine:

    mkdir ~/.ssh

    then create a file in this folder called config with the following contents (replace values as appropriate):

    Host rs
    Hostname <server_ip>
    User deploy

  3. Set up SSH keys:
  4. ssh-keygen -t dsa

    Accept defaults for all options. Now copy your public key to the remote server.

    scp ~/.ssh/id_dsa.pub deploy@rs:~/.ssh/authorized_keys

    For further keys, you must add them to the bottom of a file on their own line.

  5. Get the server libraries up-to-date:
  6. sudo apt-get -y update

  7. Install dependencies:
  8. sudo apt-get install build-essential zlib1g-dev libssl-dev libreadline-dev libyaml-dev libcurl4-openssl-dev curl git-core libxslt-dev mysql-server mysql-client libmysqlclient-dev

  9. Install ruby. You can either install it from source, install using RVM (if you're a sadist), or just use the package manager:
  10. sudo apt-get install ruby1.9.1

    Yes, this does install ruby 1.9.3, if you were wondering.

  11. If the above command didn't install RubyGems (check with: gem -v), then see the RubyGems docs.
  12. Now that RubyGems is installed, install Bundler:
  13. sudo gem install bundler

  14. Install Nginx with Passenger:
  15. sudo gem install passenger
    sudo passenger-install-nginx-module

    Choose "download, compile and install nginx for me" and then accept default options for all.

  16. Set up Nginx init script to start, stop and restart Nginx:
  17. wget -O init-deb.sh http://library.linode.com/assets/660-init-deb.sh
    sudo mv init-deb.sh /etc/init.d/nginx
    sudo chmod +x /etc/init.d/nginx
    sudo /usr/sbin/update-rc.d -f nginx defaults

    You can now control Nginx with this script and the commands:

    sudo /etc/init.d/nginx start
    sudo /etc/init.d/nginx stop
    sudo /etc/init.d/nginx restart

  18. Set up your Nginx config file (/opt/nginx/conf/nginx.conf), you should point to your Rails app's public folder. Passenger will have created a shell set up for you to edit.
  19. Deploy your app to the server. (We use Capistrano, but this short guide isn't about that aspect, so deploy as you wish.)
  20. Solve any complications that might have arisen in the above step, then set up the config/database.yml to connect to your database server.
  21. Start Nginx:
  22. sudo /etc/init.d/nginx start

You might have to install some other stuff that your app depends on, such as Redis and wkhtmltopdf in our case, but other than that you should be up and running. It's a pretty simple process if you do things right, but having spent plenty of hours with bundler errors on deployment, etc, I know where some of the nastier pitfalls can be.