passwnger

环境:ubuntu10.04 + nginx + passenger + ruby1.8.7 rails2.3.x

#安装nginx(手动编译)

$  mkdir -p /home/mouse/opt/src && cd /home/mouse/opt/src
$  wget http://nginx.org/download/nginx-0.7.67.tar.gz
$  tar xvf nginx-0.7.67.tar.gz

#安装编译相关类库
$  sudo apt-get install libpcre3-dev  

#编译安装 带有 passenger 模块的 nginx
$ gem install passenger
$ passenger-install-nginx-module 
选择 2. No: I want to customize my Nginx installation. (for advanced users)
src: /home/mouse/opt/src/nginx-0.7.67 prefix: /home/mouse/opt/nginx
添加 编译参数 并编译
$  --conf-path=/home/mouse/opt/etc/nginx/nginx.conf --with-http_gzip_static_module  
如果还要启动其他编译参数请自行添加
如果不想使用 passenger 自带脚本编译 nginx, 也可以手工编译 nginx 时加入 –add-module=’/home/mouse/opt/passenger/ext/nginx 参数, 来启用 passenger 模块.

整理编译自动生成的配置文件
$  cd /home/mouse/opt/etc/nginx
$  mkdir /home/mouse/opt/etc/nginx/default
$  mv *.default default/
$  mkdir conf.d
$  mkdir sites-enabled

将 /home/mouse/opt/etc/nginx/nginx.conf 替换为
user  mouse mouse;
worker_processes  1;

events {
    worker_connections  1024;
}


http {
    include       mime.types;
    default_type  application/octet-stream;
    
    sendfile        on;
    #tcp_nopush     on;
    
    keepalive_timeout  65;
    gzip  on;
    
    include conf.d/*.conf;  #包含两个目录
    include sites-enabled/*;
}  

添加 gzip_static 模块配置, 编辑 /home/mouse/opt/etc/nginx/conf.d/gzip_static.conf
gzip_static on;
gzip_types text/css application/x-javascript;

添加 Passneger 模块配置, 编辑 /home/mouse/opt/etc/nginx/conf.d/passenger.conf
passenger_root /home/mouse/.rvm/gems/ruby-1.8.7-p0/gems/passenger-2.2.15;
passenger_ruby /home/mouse/.rvm/bin/ruby-1.8.7-p0;

现在可以在添加属于我们自己项目的conf了,在sites-enabled中新建文件default
编辑default内容:
server{
  listen 80;  #端口
  server_name  localhost;
  root /home/mouse/rails-app/public;  #just for example
  passenger_enabled on;
  #rails_env: development;  采用development作为服务器启动,默认为production

  location ~ ^/(images|javascripts|stylesheets)/  {
      root /home/mouse/rails-app/public;
      expires 30d;
  }
}
更为详细配置可以参考:http://wiki.nginx.org/NginxFullExample

添加nginx启动脚本
添加启动脚本 /home/mouse/opt/etc/init.d/nginx 内容为(注意*下面path路径要根据本机实际情况设定)

#! /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=/home/mouse/.rvm/gems/ruby-1.9.2-p0/bin:/bin:/home/mouse/.rvm/rubies/ruby-1.9.2-p0/bin:/home/mouse/.rvm/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games
DAEMON=/home/mouse/opt/nginx/sbin/nginx
NAME=nginx
DESC=nginx
PIDFILE=/home/mouse/opt/nginx/logs/$NAME.pid

test -x $DAEMON || exit 0

# Include nginx defaults if available
if [ -f /home/mouse/opt/etc/default/nginx ] ; then
    . /home/mouse/opt/etc/default/nginx
fi

set -e

. /lib/lsb/init-functions

test_nginx_config() {
    if $DAEMON -t; then
        return 0
    else
        return $?
    fi
}

case "$1" in
    start)
        echo -n "Starting $DESC: "
        test_nginx_config
        start-stop-daemon --start --quiet --pidfile $PIDFILE 
            --exec $DAEMON -- $DAEMON_OPTS || true
        echo "$NAME."
    ;;
    stop)
        echo -n "Stopping $DESC: "
        start-stop-daemon --stop --quiet --pidfile $PIDFILE 
            --exec $DAEMON || true
        echo "$NAME."
    ;;
    restart|force-reload)
        echo -n "Restarting $DESC: "
        start-stop-daemon --stop --quiet --pidfile 
            $PIDFILE --exec $DAEMON || true
        sleep 1
        test_nginx_config
        start-stop-daemon --start --quiet --pidfile 
            $PIDFILE --exec $DAEMON -- $DAEMON_OPTS || true
        echo "$NAME."
    ;;
    reload)
        echo -n "Reloading $DESC configuration: "
        test_nginx_config
        start-stop-daemon --stop --signal HUP --quiet --pidfile $PIDFILE 
           --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 $PIDFILE "$DAEMON" nginx && exit 0 || exit $?
    ;;
    *)
        echo "Usage: $NAME {start|stop|restart|reload|force-reload|status|configtest}" >&2
        exit 1
    ;;
esac
exit 0

设置随服务器启动

chmod +x /home/mouse/opt/etc/init.d/nginx
ln -s /home/mouse/opt/etc/init.d/nginx /etc/init.d/nginx
update-rc.d nginx defaults  

现在就可以使用 
sudo /etc/init.d/nginx star
sudo /etc/init.d/nginx stop
sudo /etc/init.d/nginx restart

为了方便,可以使用bashrc脚本来方便我们启动nginx命令
vim ~/.bashrc
在最后添加上下面代码
#nginx 使用
alias nginx-start="sudo /etc/init.d/nginx start"
alias nginx-stop="sudo /etc/init.d/nginx stop"
alias nginx-restart="sudo /etc/init.d/nginx restart"

#这样就可以使用 nginx-start 来代替 sudo /etc/init.d/nginx start 命令了

#好了,最后就可以enjoy your nginx + passenger 服务器了

原文地址:https://www.cnblogs.com/ruiy/p/ror.html