环境: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. python 下载安装及setuptools应用

    1.首先下载python安装程序,下载地址:https://www.python.org/download/releases/2.7.8/ 如下图: 因为我的机器是32位的就选择了Windows x8 ...

  2. KVC vs KVO(内容为转载记录,整合大家的总结为我所用)

    KVC即key-value coding的缩写, KVO即key-value observing的缩写 假如需要掌握Key-Value Observing机制,那么需要阅读本文应该有帮助.本文提供了K ...

  3. 异常: http://www.ly.com/news/visa.html: java.io.IOException: unzipBestEffort returned null

    nutch 运行时异常: http://www.ly.com/news/visa.html: java.io.IOException: unzipBestEffort returned null 参考 ...

  4. hdu 3481 3482

    Good Serial Inc.比较简单: #include<cstdio> #include<cstring> #include<algorithm> #defi ...

  5. HDU4527+BFS

    模拟BFS搜索 对于一个将要爆炸的点,可能同时由多个点引起. /* 模拟搜索过程 */ #include<stdio.h> #include<stdlib.h> #includ ...

  6. PHP unlink() 函数

    定义和用法 unlink() 函数删除文件. 若成功,则返回 true,失败则返回 false. 语法 unlink(filename,context) 参数 描述 filename 必需.规定要删除 ...

  7. js设置radio选中

    在页面数据绑定时,经常会遇到给radio设置选中,以下是我写的js方法,经测试可以使用.欢迎拍砖 <html> <head> <script type="tex ...

  8. SQLite入门与分析(八)---存储模型(3)

    写在前面:接上一节,本节主要讨论索引页面格式,以及索引与查询优化的关系. (1)索引页面格式sqlite> select * from sqlite_master;table|episodes| ...

  9. SPRING IN ACTION 第4版笔记-第五章Building Spring web applications-001-SpringMVC介绍

    一. 二.用Java文件配置web application 1. package spittr.config; import org.springframework.web.servlet.suppo ...

  10. js中replace用法

    js中replace的用法 replace方法的语法是:stringObj.replace(rgExp, replaceText) 其中stringObj是字符串(string),reExp可以是正则 ...