I’m going to compile in nginx server from the source. I’m assuming that you’ve packages like ruby, Rails etc. pre-installed on your system/server.
I’ve compiled in nginx from the source, so you’ll need a gcc complier.
Download the source. wget http://sysoev.ru/nginx/nginx-0.5.35.tar.gz Untar it tar zxvf nginx-0.5.35.tar.gz Run. cd nginx-0.5.35 ./configure --prefix=/usr/local/nginx make make install
If everything does fine, nginx will get installed /usr/local/nginx
Here is a sample nginx configuration file. Just copy it as nginx.conf(/usr/local/nginx/conf/nginx.conf) and modify it accordingly four your application setup.
user www www; worker_processes 3; error_log /var/log/nginx/error.log; pid /var/run/nginx.pid; events { worker_connections 1024; } http { include /usr/local/nginx/conf/mime.types; default_type application/octet-stream; log_format main '$remote_addr - $remote_user [$time_local] $status ' '"$request" $body_bytes_sent "$http_referer" ' '"$http_user_agent" "http_x_forwarded_for"'; #access_log logs/access.log main; sendfile on; tcp_nopush on; tcp_nodelay on; gzip on; upstream mongrel { server 127.0.0.1:4000; server 127.0.0.1:4001; } #Rails App here server { listen 80; root /var/www/railsapp/public; index index.html index.htm; server_name yourdomain.com www.yourdomain.com; client_max_body_size 50M; access_log /var/log/nginx/localhost.access.log; location / { proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded_for $proxy_add_x_forwarded_for; proxy_set_header Host $http_host; proxy_redirect false; proxy_max_temp_file_size 0; if (-f $request_filename) { break; } if (-f $request_filename/index.html) { rewrite (.*) $1/index.html break; } if (-f $request_filename.html) { rewrite (.*) $1.html break; } if (!-f $request_filename) { proxy_pass http://mongrel; break; } } # redirect server error pages to the static page /50x.html # error_page 500 502 503 504 /500.html; location = /500.html { root /var/www/railsapp/public; } } }
You may start the nginx server using the command
/usr/local/nginx/bin/nginx -c /usr/local/nginx/conf/nginx.conf
Now you may want an init startup script to start|stop|restart the server. Just copy the below script as /etc/init.d/nginx and set it executable(chmod 755 /etc/init.d/nginx)
#!/bin/sh # Description: Startup script for nginx webserver on Debian. Place in /etc/init.d and # run 'sudo update-rc.d nginx defaults', or use the appropriate command on your # distro. # # Author: Ryan Norbauer # Modified: Geoffrey Grosenbach http://topfunky.com set -e PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin DESC="nginx daemon" NAME=nginx DAEMON=/usr/local/nginx/sbin/$NAME CONFIGFILE=/usr/local/nginx/conf/nginx.conf PIDFILE=/var/run/$NAME.pid SCRIPTNAME=/etc/init.d/$NAME # Gracefully exit if the package has been removed. test -x $DAEMON || exit 0 d_start() { $DAEMON -c $CONFIGFILE || echo -n " already running" } d_stop() { kill -QUIT `cat $PIDFILE` || echo -n " not running" } d_reload() { kill -HUP `cat $PIDFILE` || echo -n " can't reload" } case "$1" in start) echo -n "Starting $DESC: $NAME" d_start echo "." ;; stop) echo -n "Stopping $DESC: $NAME" d_stop echo "." ;; reload) echo -n "Reloading $DESC configuration..." d_reload echo "reloaded." ;; restart) echo -n "Restarting $DESC: $NAME" d_stop # One second might not be time enough for a daemon to stop, # if this happens, d_start will fail (and dpkg will break if # the package is being upgraded). Change the timeout if needed # be, or change d_stop to have start-stop-daemon use --retry. # Notice that using --retry slows down the shutdown process somewhat. sleep 1 d_start echo "." ;; *) echo "Usage: $SCRIPTNAME {start|stop|restart|force-reload}" >&2 exit 3 ;; esac exit 0
Also don’t forget to add the user www using “useradd -s /sbin/nologin www” if the user doesn’t exists.
Now time to setup mongrel for your app. Install the mongrel gem, if it’s not installed already.
gem install mongrel
Then configure the mongrel cluster for your app and start the mongrel server.
cd /var/www/railsapp (your application directory) mongrel_rails cluster::configure -e production -p 4000 -N 2 mongrel_rails cluster::start
You can of course use the mongrel_rails cluster::start|stop|restart commands to manage your mongrel instance.
Hope that this tutorial is useful…