If you’re planning to run Rails applications on Nginx using Phusion Passenger, and do it on Ubuntu Linux, here’s what needs to be done.
Even though there’s Ubuntu nginx package available (which works perfectly when you’re running PHP apps using FCGI), if you want to take into use Phusion Passenger, you’ll need to recompile Nginx from sources.
Instructions below were verified on Ubuntu 10.04 (Lucid Lynx) Server Edition.
If you already have Nginx ubuntu package installed, uninstall it (NOTE! purge will delete all configuration files – so if you changed them – make a backup for future reference):
sudo aptitude purge nginx
Assuming you already have Rails stack installed, install Passenger gem:
sudo gem install passenger
At the time of this writing the latest version of Passenger is 2.2.11
Let’s check dependencies that Ubuntu nginx package has and install them before compilation:
aptitude show nginx | grep Depends
Depends: libc6 (>= 2.4), libpcre3 (>= 7.7), libssl0.9.8 (>= 0.9.8k-1), zlib1g
sudo apt-get install libc6 libpcre3 libssl0.9.8 zlib1g sudo /usr/local/bin/passenger-install-nginx-module
sudo /usr/local/bin/passenger-make-enterprisey
sudo vim /etc/init.d/nginx
#! /bin/sh
### BEGIN INIT INFO
# Provides: nginx
# Required-Start: $local_fs $remote_fs $network $syslog
# Required-Stop: $local_fs $remote_fs $network $syslog
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: starts the nginx web server
# Description: starts nginx using start-stop-daemon
### END INIT INFO
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/opt/nginx/sbin
DAEMON=/opt/nginx/sbin/nginx
NAME=nginx
DESC=nginx
test -x $DAEMON || exit 0
# Include nginx defaults if available
if [ -f /etc/default/nginx ] ; then
. /etc/default/nginx
fi
set -e
. /lib/lsb/init-functions
test_nginx_config() {
if nginx -t $DAEMON_OPTS
then
return 0
else
return $?
fi
}
case "$1" in
start)
echo -n "Starting $DESC: "
test_nginx_config
start-stop-daemon --start --quiet --pidfile /var/run/$NAME.pid \
--exec $DAEMON -- $DAEMON_OPTS || true
echo "$NAME."
;;
stop)
echo -n "Stopping $DESC: "
start-stop-daemon --stop --quiet --pidfile /var/run/$NAME.pid \
--exec $DAEMON || true
echo "$NAME."
;;
restart|force-reload)
echo -n "Restarting $DESC: "
start-stop-daemon --stop --quiet --pidfile \
/var/run/$NAME.pid --exec $DAEMON || true
sleep 1
test_nginx_config
start-stop-daemon --start --quiet --pidfile \
/var/run/$NAME.pid --exec $DAEMON -- $DAEMON_OPTS || true
echo "$NAME."
;;
reload)
echo -n "Reloading $DESC configuration: "
test_nginx_config
start-stop-daemon --stop --signal HUP --quiet --pidfile /var/run/$NAME.pid \
--exec $DAEMON || true
echo "$NAME."
;;
configtest)
echo -n "Testing $DESC configuration: "
if test_nginx_config
then
echo "$NAME."
else
exit $?
fi
;;
status)
status_of_proc -p /var/run/$NAME.pid "$DAEMON" nginx && exit 0 || exit $?
;;
*)
echo "Usage: $NAME {start|stop|restart|reload|force-reload|status|configtest}" >&2
exit 1
;;
esac
exit 0
sudo chmod +x /etc/init.d/nginx sudo /usr/sbin/update-rc.d -f nginx defaults
user www-data;
worker_processes 4;
error_log /opt/nginx/logs/error.log;
pid /var/run/nginx.pid;
events {
worker_connections 8192;
use epoll;
}
http {
passenger_root /usr/local/lib/ruby/gems/1.8/gems/passenger-2.2.11;
passenger_ruby /usr/local/bin/ruby;
include /opt/nginx/conf/mime.types;
# set a default type for the rare situation that
# nothing matches from the mimie-type include
default_type application/octet-stream;
# This log format is compatible with any tool like awstats
# that can parse standard apache logs.
log_format main '$remote_addr - $remote_user [$time_local] '
'"$request" $status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"' ;
access_log /opt/nginx/logs/access.log;
sendfile on;
tcp_nopush on;
keepalive_timeout 0;
tcp_nodelay on;
gzip on;
gzip_disable "MSIE [1-6]\.(?!.*SV1)";
gzip_http_version 1.0;
gzip_comp_level 2;
gzip_proxied any;
gzip_types text/plain text/css application/x-javascript text/xml application/xml application/xml+rss text/javascript;
include /opt/nginx/conf/sites-enabled/*;
}
sudo mkdir /opt/nginx/conf/sites-available sudo mkdir /opt/nginx/conf/sites-enabled
sudo vim /opt/nginx/conf/sites-available/mysite.com
server {
listen 80;
server_name www.mysite.com;
access_log /home/user/logs/www.mysite.com/access.log;
root /home/user/www.mysite.com/public;
# serve static content directly
location ~* \.(ico|jpg|gif|png|css|js|swf|html)$ {
if (-f $request_filename) {
expires max;
break;
}
}
passenger_enabled on;
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
location ~ /\.ht {
deny all;
}
}
sudo ln -s /opt/nginx/conf/sites-available/mysite.com /opt/nginx/conf/sites-enabled/mysite.com sudo /etc/init.d/nginx restart

4 Comments
Nice tutorial. However, I’m getting permission errors right before Nginx is configured. Thing is, I’m doing this as root, and I have no other users on my system right now. Any advice?
./configure –prefix=’/opt/nginx’ –with-pcre=’/tmp/pcre-8.02′ –add-module=’/usr/local/rvm/gems/ree-1.8.7-2010.02/gems/passenger-2.2.15/ext/nginx’
sh: ./configure: Permission denied
I figured it out. I had to install PCRE properly before .configure. It didn’t like that my PCRE was in a tmp folder, I guess. :S Never mind!
how would you uninstall nginx that was compiled from source?
Great how-to, worked like a charm, thank you!
2 Trackbacks/Pingbacks
[...] Making nginx utilise GC settings when spawning Passenger instances is easy, and is well described by Chris. Here are instructions that work nice with server and Nginx setup on Ubuntu as I described in earlier posts. [...]
[...] Source: http://www.ivankuznetsov.com/2010/05/running-rails-applications-using-nginx-with-passenger-on-ubuntu… og http://thekindofme.wordpress.com/2010/10/24/rails-3-on-ubuntu-10-10-with-rvm-passenger-and-nginx/ [...]
Post a Comment