环境: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 animate的使用方法

    js: $('#colspan').click(function () { if ($('#colspan').hasClass('glyphicon-chevron-up')) { $('#cols ...

  2. [转载]MongoDB C# 驱动教程

    本教程基于C#驱动 v1.6.x . Api 文档见此处: http://api.mongodb.org/csharp/current/. 简介 本教程介绍由10gen支持的,用于MongoDB的C# ...

  3. ***PHP多线程pthreads 实现QQ号码爬虫

    通过空间历史浏览,爬出查看你空间的人(一般限制20人,除非开通黄钻),然后在爬出这20人的浏览记录,依次向下爬,你可以控制爬行深度.这里仅仅给出怕中代码片段,你可以进一步优化,将QQ分类存储.通过QQ ...

  4. ZOJ 2110 Tempter of the Bone(DFS)

    点我看题目 题意 : 一个N×M的迷宫,D是门的位置,门会在第T秒开启,而开启时间小于1秒,问能否在T秒的时候到达门的位置,如果能输出YES,否则NO. 思路 :DFS一下就可以,不过要注意下一终止条 ...

  5. HDU4525+公式

    一开始TLE了... /* 模拟 */ #include<stdio.h> #include<math.h> ; typedef __int64 int64; int64 a[ ...

  6. MySql 命令积累

    一. 修改表的自增起点 ; 二.获取自增键值 1. select max(id) from tablename 2.SELECT LAST_INSERT_ID() 函数 LAST_INSERT_ID ...

  7. C++的表驱动法

    目的:使用表驱动法,替换复杂的if/else和switch/case语句. 说明:JS 等其他语言也都支持的. 表驱动发示例:http://blog.csdn.net/zhouyulu/article ...

  8. [unity菜鸟] 笔记1 —— 函数篇

    SendMessage() 调用其他物体中的指令,先在脚本中编写一个自定义的函数,然后使用SendMessage()命令来调用那个物体上的命令 //①将以下函数附给target对象 void Rena ...

  9. 弱安全协议探测工具-sslciphercheck

    SSL(Secure Sockets Layer 安全套接层),及其继任者传输层安全(Transport Layer Security,TLS)是为 网络通信提供安全及数据完整性的一种安全协议.TLS ...

  10. vi编辑器的三种模式

    1.命令模式(command mode)—执行命令 在该模式中,可以输入命令来执行许多种功能.控制屏幕光标的移动,字符.字或行的删除,移动复制某区段及进入Insert mode下,或者到 last l ...