Skip to content

Nginx Server Configuration

Adrian edited this page · 9 revisions

If you have never ran rails on a server, this document will guide you in setting up a rails app running on nginx step by step. Basic server configuration can seem daunting at first, but this document takes all the Googling and head-scratching out of the equation.

Assumptions

  • An Ubuntu server
  • Ability to use root permissions
  • Comfort navigating the unix environment and using a terminal-based editor.
  • Ruby and bundler installed on the server.
  • Git installed on the server (See Guide)

Since we want to check that our nginx server properly runs rails before we worry about deployment, the first portion of this server configuration guide will involve you operating directly on your server. Ensure you have ssh access.

Step 1: Configuring Bundler

$ sudo mkdir ~/.bundle

Create a file named config in the ~/.bundle directory and paste in the following

BUNDLE_PATH: vendor/bundle

Step 2: Installing packages

To install nginx and git, execute

$ sudo apt-get update
$ sudo apt-get install git-core
$ sudo apt-get install nginx

Step 3: Configuring nginx

Navigate over the nginx configuration directory

$ cd /etc/nginx

Open the nginx.conf file in your favorite text editor as root

$ sudo vim nginx.conf

Clear the contents of the configuration file and paste in this gist. The only thing you'll have to change for future projects is the root path. This is simply a condensed version of the configuration file provided by the creators of Unicorn. The commented version of the file can be found at http://unicorn.bogomips.org/examples/nginx.conf

Before we can clone the Rails template on the server, we need to set up a good permissions infrastructure.

Step 4: Configuring permissions

Create a web group, which we will use to allow write permissions to directories related to displaying web content and deployment.

$ sudo groupadd web

Assign the web group to the /var/www directory created by nginx, and then make the directory group writable.

    $ sudo chgrp web /var/www
    $ sudo chmod -R 775 /var/www

Additionally, we want any directories created within /var/www in the future to be owned by the web group. The following command accomplishes this.

$ sudo chmod g+s /var/www

Verify the group assignment and permissions worked

$ ls -l /var
    ...
    ...
    drwxrwsr-x  3 root web   4096 2012-01-12 23:01 www

Now assign your current user account to the group. It's very important that you do not forgot the -a flag, or you will be removed from all other groups!!!

$ sudo usermod -a -G web USERNAME

At this time it's recommended you log out and ssh back in. This is because group changes to the currently logged in account sometimes do not take effect until the account has logged out.

Verify your account is now a part of the `web group by executing

$ groups

This command brings up a list of groups the logged in user account is in.

Now it's time to actually get our rails app on the server.

Step 5: Creating the rails app on the server

Move to the /var/www directory.

$ cd /var/www

Now grab this template! (Get rails_template function from the Create a new app wiki)

$ rails_template testapp

With a rails app on the server, we're one step closer to having the app available for the world to see. The next step is to configure unicorn.

Step 6. Configuring Unicorn

Basic unicorn configurations are taken care of by this template. All you need to do is edit config/unicorn.rb where it asks for your APP_PATH. For this example, we created the app at /var/www/testapp

    ...
    ...

    ############################################################################
    # Adjust your APP_PATH here!!!
    ############################################################################
    APP_PATH = "/var/www/testapp" # NO trailing slash

    ...
    ...

Similarly to the nginx.conf file, in the future simply edit the APP_PATH to point to the root directory of your app.

Everything is just about ready to go at this point: Now we just need to actually start the nginx server and the unicorn service.

Step 7: Starting nginx and unicorn

In my short experience with nginx and unicorn so far, these services do not die very easily, even when you want them to. For example, if you change the root path in your nginx.conf file, you need to restart nginx. Before the change takes effect. However, using the common means of restarting (/etc/init.d/nginx restart) tends to give me an error that a process is already operating on the socket...and that process is nginx!

So I've put together a few functions that I'll be using to complete this guide. Append the contents of this gist to your ~/.bashrc

The difference between the test_unicorn() and start_unicorn() functions is test_unicorn starts unicorn in the foreground and can be terminated with CTRL-C while start_unicorn() runs unicorn in the background as a daemon.

We need make sure our shell uses this new ~/.bashrc

source ~/.bashrc

Hello, world!

Make sure you're in the root directory of the app.

$ cd /var/www/testapp

Now we're going to test unicorn.

$ sudo service nginx start
$ test_unicorn

Now visit http://yourserver.com/pages/show and you should see the familiar "Find me in app/views/pages/show". Note that you should not use http://yourserver.com/ to determine if unicorn is working. nginx will serve up the static index.html document located in the public folder of the app regardless of unicorn's state, potentially leading to a false positive.

Something went wrong with that request. Please try again.