分享 初学者连载系列之十八:采用 Nginx 和 Passenger 部署 Rails 应用

kevinhua · 2012年04月07日 · 最后由 freemem 回复于 2013年02月03日 · 3827 次阅读

系列文章原载于自己的博客,TOPI.CO (http://topi.co) ,某天不小心就 push 错啦,懒得从头再来,上传到 Ruby-China 来,一是方便自己回顾,另外也方便跟我一样的初学者

使用 Nginx 和 Passenger 组合,部署 Rails 应用程序,是不个不错的选择。

为了使安装速度提高,可以使用淘宝镜像作为 Rubygems 的源。

Ubuntu 系统下,需要先安装 PCRE:

sudo apt-get install libpcre3 libpcre3-dev

步骤如下:

###(1) gem install passenger

###(2) passenger-install-nginx-module

###(3) 设置 Nginx 的配置文件

比较关键的设置:

user www-data #即应用程序文件夹的所有者

#设置主Server,也可以选择使用vhost server
server {
  listen        80;
  server_name   localhost;
  root      /path/to/your/rails_app/public;
  passenger_enabled on;
  ... ...
}

#注释掉location / {... ...}这个block

###Nginxd 自动启动脚本 (init.d) —— CentOS 适用

#!/bin/bash
# v.0.0.1
# create by jackbillow at 2007.10.15
# nginx - This shell script takes care of starting and stopping nginx.
#
# chkconfig: 35 60 50
# description: nginx [engine x] is light http web/proxy server
# that answers incoming ftp service requests.
# processname: nginx
# config: /usr/local/nginx/conf/nginx.conf
nginx_path="/usr/local/nginx"
nginx_pid="/usr/local/nginx/logs/nginx.pid"
# Source function library.
. /etc/rc.d/init.d/functions
# Source networking configuration.
. /etc/sysconfig/network
# Check that networking is up.
[ ${NETWORKING} = "no" ] && exit 0
[ -x $nginx_path/sbin/nginx ] || exit 0
RETVAL=0
prog="nginx"
start() {
# Start daemons.
if [ -e $nginx_pid -a ! -z $nginx_pid ];then
echo "nginx already running..."
exit 1
fi

if [ -e /usr/local/nginx/conf/nginx.conf ];then
  echo -n $"Starting $prog: "
  $nginx_path/sbin/nginx -c /usr/local/nginx/conf/nginx.conf &
  RETVAL=$?
[ $RETVAL -eq 0 ] && {
touch /var/lock/subsys/$prog
success $"$prog"
}
echo
else
RETVAL=1
fi
return $RETVAL
}
# Stop daemons.
stop() {
        echo -n $"Stopping $prog: "
        killproc -d 10 $nigx_path/sbin/nginx
        RETVAL=$?
        echo
        [ $RETVAL = 0 ] && rm -f $nginx_pid /var/lock/subsys/$prog
}
# See how we were called.
case "$1" in
start)
        start
        ;;
stop)
        stop
        ;;
reconfigure)
        stop
        start
        ;;
status)
        status $prog
        RETVAL=$?
        ;;
*)
        echo $"Usage: $0 {start|stop|reconfigure|status}"
        exit 1
esac
exit $RETVAL

###Nginx 自动启动脚本 (init.d) —— Ubuntu 适用

#! /bin/sh
### BEGIN INIT INFO
# Provides:          nginx
# Required-Start:    $all
# Required-Stop:     $all
# 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/nginx/sbin:$PATH
DAEMON=/usr/local/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
case "$1" in
  start)
    echo -n "Starting $DESC: "
    start-stop-daemon --start --quiet --pidfile /var/log/$NAME.pid \
        --exec $DAEMON -- $DAEMON_OPTS || true
    echo "$NAME."
    ;;
  stop)
    echo -n "Stopping $DESC: "
    start-stop-daemon --stop --quiet --pidfile /var/log/$NAME.pid \
        --exec $DAEMON || true
    echo "$NAME."
    ;;
  restart|force-reload)
    echo -n "Restarting $DESC: "
    start-stop-daemon --stop --quiet --pidfile \
        /var/log/$NAME.pid --exec $DAEMON || true
    sleep 1
    start-stop-daemon --start --quiet --pidfile \
        /var/log/$NAME.pid --exec $DAEMON -- $DAEMON_OPTS || true
    echo "$NAME."
    ;;
  reload)
      echo -n "Reloading $DESC configuration: "
      start-stop-daemon --stop --signal HUP --quiet --pidfile /var/log/$NAME.pid \
          --exec $DAEMON || true
      echo "$NAME."
      ;;
  status)
      status_of_proc -p /var/log/$NAME.pid "$DAEMON" nginx && exit 0 || exit $?
      ;;
  *)
    N=/etc/init.d/$NAME
    echo "Usage: $N {start|stop|restart|reload|force-reload|status}" >&2
    exit 1
    ;;
esac
exit 0

centOS 的脚本不错!

需要 登录 后方可回复, 如果你还没有账号请 注册新账号