环境: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 服务器了

passwnger的更多相关文章

随机推荐

  1. jQuery name checked 模糊查找匹配ID

    ("div[name='jobTitle']") $("#aDiv").find("input[type='checkbox']:checked&qu ...

  2. BZOJ 3715: [PA2014]Lustra

    Description Byteasar公司专门外包生产带有镜子的衣柜.刚刚举行的招标会上,有n个工厂参加竞标.所有镜子都是长方形的,每个工厂能够制造的镜子都有其各自的最大.最小宽度和最大.最小高度. ...

  3. 第三章—Windows程序

    这一章我都不知道该如何写了,呵呵~~ 毕竟,Win32是一个非常深奥的系统,目前还容不得我这种 小辈在这儿说三道四,不过,我既然是要写给那些入门阶段的朋友们看的,又不是写给那些搞程序设计老鸟看的,所以 ...

  4. 如何从软硬件层面提升 Android 动画性能?

    若是有人问如何解决动画性能不佳的问题,Dan Lew Codes 总会反问:你是否使用了硬件层? 动画放映过程中每帧画面可能都要重绘.如果使用视图层,,渲染过的视图可以存入离屏缓存以待将来重用,而无需 ...

  5. Design Tutorial: Learn from Life

    Codeforces Round #270 B:http://codeforces.com/contest/472/problem/B 题意:n个人在1楼,想要做电梯上楼,只有1个电梯,每次只能运k个 ...

  6. POJ 1275 Cashier Employment(差分约束)

    http://poj.org/problem?id=1275 题意 : 一家24小时营业的超市,要雇出纳员,需要求出超市每天不同时段需要的出纳员数,午夜只需一小批,下午需要多些,希望雇最少的人,给出每 ...

  7. android 自定义按钮的外边框

    <?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http: ...

  8. [Gauss]POJ1222 EXTENDED LIGHTS OUT

    题意:给一个5*6的矩阵 1代表该位置的灯亮着, 0代表该位置的灯没亮 按某个位置的开关,可以同时改变 该位置 以及 该位置上方.下方.左方.右方, 共五个位置的灯的开.关(1->0, 0-&g ...

  9. c/c++ 重载 数组 操作符[] operator[ is ambiguous, as 0 also mean a null pointer of const char* type.

    // Note: //int x = a[0].GetInt(); // Error: operator[ is ambiguous, as 0 also mean a null pointer of ...

  10. 146. LRU Cache

    题目: Design and implement a data structure for Least Recently Used (LRU) cache. It should support the ...