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.

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

目录
相关文章
|
24天前
|
API Android开发 开发者
NavigableListDetailPaneScaffold
【9月更文挑战第12天】
15 5
|
2月前
|
XML Java 数据处理
深入了解 XPath
【8月更文挑战第22天】
40 0
|
2月前
|
SQL Java 数据库
什么是 PagingAndSortingRepository?
【8月更文挑战第21天】
34 0
|
XML Java 数据库连接
parameterType是必须写的吗?
xml中没有配置parameterType,但是这是正确的,因为mybatis能自动识别,但返回值类型不能不写,因为mybatis需要将获得结果封装到相应的类中,查询的字段与类的属性需要一致。
355 0
parameterType是必须写的吗?
|
供应链 机器人
什么是RPA?
什么是RPA?
361 0
|
算法
PAT条条大路通罗马
Indeed there are many different tourist routes from our city to Rome. You are supposed to find your clients the route with the least cost while gaining the most happiness.
118 0
PAT有几个pat
字符串APPAPT中包含了两个单词“PAT”,其中第一个PAT是第2位§,第4位(A),第6位(T);第二个PAT是第3位§,第4位(A),第6位(T)。 现给定字符串,问一共可以形成多少个PAT?
118 0
|
存储 安全 Java
PalDB 介绍
开篇  PalDB在我的工作中被大面积使用,场景我就不描述了,这里我只想直白的说一句,这个系列的PalDB博文绝对是国内最详细的,如果有兴趣非常建议收藏了好好看看。
1062 0