Archive

Posts Tagged ‘rails’

Installing nginx and rails passenger on Mac OS X Snow Leopard

October 17th, 2009 lmmendes 2 comments

I’m new to Mac OS X, nginx and rails so after much goggling i finally got everything to work so i resolved to compile the list of instructions that i followed.

First we need to download, compile and install PRE (Perl Compatible Regular Expressions) it’s required for nginx. You can find the most recent version here http://www.pcre.org/ , at time of writing this post the most recent version is 7.9

Compiling and installing PRE

1
2
3
4
5
6
7
8
mkdir ~/apps/src
cd ~/apps/src
curl -O ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-7.9.tar.gz
tar -xvzf pcre-7.9.tar.gz
cd pre-7.9
./configure --prefix=/usr/local
make
sudo make install

Installing Phusion Passenger via gem

1
gem install passenger

Now that you have Phusion Passenger installed you can install and configure nginx and passenger using a bundled script from passenger called “passenger-install-nginx-module” but that is no fun and usually don’t install the lasted stable version of nginx, so you can run the script and follow it’s instructions (that include auto-downloading and installing of nginx) or do it my way… the hard way.

So now we need to find where “gem” installed the nginx module so we can refer to it’s during the nginx configuration, so to get the module path you just need to run this:

1
passenger-config --root

In my case the command output is

1
/Users/<usename>/.gem/ruby/1.8/gems/passenger-2.2.5

You need to write down this output, you will need it in the next step.

Now it’s time do download and install nginx we will be installing version 0.7.62 (stable).

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
cd ~/apps/src
curl -O http://sysoev.ru/nginx/nginx-0.7.62.tar.gz
tar -xvzf nginx-0.7.62.tar.gz
cd nginx-0.7.62
./configure \
--prefix=/usr/local \
--conf-path=/etc/nginx/nginx.conf \
--http-log-path=/var/log/nginx/access_log \
--error-log-path=/var/log/nginx/error_log \
--pid-path=/var/run/nginx.pid \
--http-client-body-temp-path=/var/tmp/nginx/client \
--http-proxy-temp-path=/var/tmp/nginx/proxy \
--http-fastcgi-temp-path=/var/tmp/nginx/fastcgi \
--with-md5-asm --with-md5=/usr/include \
--with-sha1-asm \
--with-sha1=/usr/include \
--with-http_realip_module \
--with-http_ssl_module \
--with-http_stub_status_module \
--add-module='/Users/<username>/.gem/ruby/1.8/gems/passenger-2.2.5/ext/nginx'
make
sudo make install

Has you can see in line 20 you need to add the previous output that i told you to write down.

The nginx install path’s, bin, etc… :

bin: /usr/local/sbin/nginx
etc: /etc/nginx/
pid: /var/run/nginx.pid
document root: /usr/local/html/
error log: /var/log/nginx/error_log

Now let’s create a demo rails application to show things working

1
rails ~/Sites/blog

Edit /etc/nginx/nginx.conf (you can use your favorit text editor, i will be using vi)

1
sudo vi /etc/nginx/nginx.conf

Find the “http” section and add this lines

1
2
passenger_root /Users/<username>/.gem/ruby/1.8/gems/passenger-2.2.5;
passenger_ruby /usr/bin/ruby;

You should get something like this:

1
2
3
4
5
6
7
http {
    include       mime.types;
    default_type  application/octet-stream;
 
    passenger_root /Users/<username>/.gem/ruby/1.8/gems/passenger-2.2.5;
    passenger_ruby /usr/bin/ruby;
. . . .

Now adding the virtual host to nginx.conf to run ~/Sites/blog app ( /Users//Sites/blog )

Add a new server configuration (vhost) inside the http section of the nginx.conf file like this:

1
2
3
4
5
6
7
  server {
          passenger_enabled on;
          listen 80;
          server_name blog;
          root /Users/<username>/Sites/blog/public;
          rails_env development;
  }

Now save the file and let’s se if it’s all working.

Start the nginx like this:

1
sudo /usr/local/sbin/nginx

Let’s edit the /etc/host file to add the new vhost name ( blog )

1
vi /etc/hosts

add this to the bottom of the file, save and exit

1
127.0.0.1       blog

Now point your browser to http://blog and you should the the standard rails app page if you experince any problem starting nginx use this command to see nginx error log

1
tail -f /var/log/nginx/error_log

All done, i hope it helps you.