Installing Nginx with Passenger on Snow Leopard Using MacPorts

Install MacPorts if you don’t have it.

Download and install the Portfile

git clone git:// ports
cd ports/nginx-0.7.64-passenger-2.2.8
sudo port -v install

You need to have ruby installed to compile passenger. In case you don’t have ruby, you can also get it from macports.

sudo port -v install ruby

If you have an older version of nginx installed using macports, it might move nginx.conf to nginx.conf.altered. Copy it back to nginx.conf if you need it. You can also use this nginx.conf.

Edit /opt/local/etc/nginx.conf and add the following lines inside http {}

passenger_root /opt/local/lib/passenger;
passenger_ruby /opt/local/bin/ruby;

Change the ruby path if you’re not using ruby from macports.

For each rails app, you need to add the following, also inside http {}

server {
  server_name labs.local;
  root /Users/crigor/admoolabs/labs/public;
  access_log /opt/local/var/log/nginx/labs.local.access.log;
  error_log /opt/local/var/log/nginx/labs.local.error.log;
  passenger_enabled on;
  rails_env development;

Check if your syntax is correct.

sudo /opt/local/sbin/nginx -t

If it is, you’ll see

the configuration file /opt/local/etc/nginx/nginx.conf syntax is ok
configuration file /opt/local/etc/nginx/nginx.conf test is successful

Start nginx with

sudo /opt/local/sbin/nginx

You won’t get any output if it starts correctly. Check if nginx is running

ps -e | grep nginx -i
(You should see something like these lines)
nginx: master process /opt/local/sbin/nginx
nginx: worker process
PassengerNginxHelperServer /opt/local/lib/passenger /opt/local/bin/ruby 3 4 0 6 0 300 1 nobody 4294967294 4294967294 /tmp/passenger.10755

MacPorts added a startup item which is disabled by default. To start it,

sudo launchctl load -w /Library/LaunchDaemons/org.macports.nginx.plist

You can stop nginx with

sudo launchctl stop org.macports.nginx

but it would just be started right away. If you want it to remain stopped, use

sudo launchctl unload /Library/LaunchDaemons/org.macports.nginx.plist

When you make changes to the config, reload the config with

sudo /opt/local/sbin/nginx -s reload

Check the other nginx options using

/opt/local/sbin/nginx -h

One more thing…
When you add a rails or rack app, you need to specify a different server_name. You need to add it to /etc/hosts localhost labs.local

Don’t you think it would be great if you can handle this automatically? Me too. Stay tuned for more. This shouldn’t be a pane pain. ;)