Nginx安装笔记(rails via passenger, php via fastcgi, python via fastcgi)

Wednesday, July 21, 2010

download and install REE(Ruby Enterprise Edition)

root@eric-desktop:/usr/local/src# wget http://rubyforge.org/frs/download.php/71100/ruby-enterprise_1.8.7-2010.02_i386_ubuntu10.04.deb
root@eric-desktop:/usr/local/src# dpkg -i ruby-enterprise_1.8.7-2010.02_i386_ubuntu10.04.deb

install build-essential

root@eric-desktop:/usr/local/src# apt-get install build-essential

install php

root@eric-desktop:/usr/local/src# apt-get install php5-cli php5-cgi

install dependancies for nginx

root@eric-desktop:/usr/local/src# apt-get install libpcre3 libpcre3-dev libpcrecpp0 libssl-dev zlib1g-dev

download prce

root@eric-desktop:/usr/local/src# wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.10.tar.gz
root@eric-desktop:/usr/local/src# tar zxvf pcre-8.10.tar.gz

download and install nginx

root@eric-desktop:/usr/local/src# wget http://sysoev.ru/nginx/nginx-0.8.46.tar.gz
root@eric-desktop:/usr/local/src# tar zxvf nginx-0.8.46.tar.gz
root@eric-desktop:/usr/local/src# cd nginx-0.8.46
root@eric-desktop:/usr/local/src/nginx-0.8.46# ./configure --sbin-path=/usr/local/sbin --with-http_ssl_module --with-http_stub_status_module --with-http_gzip_static_module --with-pcre=/usr/local/src/pcre-8.10 --add-module=/usr/local/lib/ruby/gems/1.8/gems/passenger-2.2.15/ext/nginx/
root@eric-desktop:/usr/local/src/nginx-0.8.46# make
root@eric-desktop:/usr/local/src/nginx-0.8.46# make install
root@eric-desktop:/usr/local/src/nginx-0.8.46# nginx
[alert]: Phusion Passenger is disabled because the 'passenger_root' option is not set. Please set this option if you want to enable Phusion Passenger.
打开浏览器看看
root@eric-desktop:/usr/local/src/nginx-0.8.46# firefox http://localhost
会看到“Welcome to nginx!”,就表示启动成功了,好吧, 配置一下passenger

1, rails项目 nginx+passenger

配置passenger_root和passenger_ruby
root@eric-desktop:/usr/local/src/nginx-0.8.46# vim /usr/local/nginx/conf/nginx.conf
在http块中插入passenger_root和passenger_ruby
http {
    #...
    #--------------------passenger------------------------------------
    passenger_root /usr/local/lib/ruby/gems/1.8/gems/passenger-2.2.15;
    passenger_ruby /usr/local/bin/ruby;
    #-----------------------------------------------------------------
    #....
}
新建一个vhosts文件夹来放配置信息
root@eric-desktop:/usr/local/src/nginx-0.8.46# mkdir /usr/local/nginx/conf/vhosts
在nginx.conf底部inclue这个目录 include /usr/local/nginx/conf/vhosts/*;
测试一下
建一个rails项目的配置
root@eric-desktop:/usr/local/src/nginx-0.8.46# vim /usr/local/nginx/conf/vhosts/rails.lvh.me
server {
        listen       80;
        server_name  rails.lvh.me;
        root /home/eric/workspace/NetBeansProjects/blog/public;
        passenger_enabled on;
        rails_env development;
       }
root@eric-desktop:/usr/local/src/nginx-0.8.46# killall -9 nginx
root@eric-desktop:/usr/local/src/nginx-0.8.46# nginx
打开浏览器看看
root@eric-desktop:/usr/local/src/nginx-0.8.46# firefox http://rails.lvh.me
rails项目正常运行

2, php项目 nginx+fcgi

# PHP and FCGI
# check out spawn-fcgi
root@eric-desktop:/usr/local/src# svn co svn://svn.lighttpd.net/spawn-fcgi/trunk spawn-fcgi
root@eric-desktop:/usr/local/src# cd spawn-fcgi/
root@eric-desktop:/usr/local/src/spawn-fcgi# ls
AUTHORS     CMakeLists.txt  COPYING  Makefile.am  README        src
autogen.sh  configure.ac    doc      NEWS         spawn-fcgi.1
# 需要安装automake
root@eric-desktop:/usr/local/src/spawn-fcgi# apt-get install automake
# 安装spawn-fcgi
root@eric-desktop:/usr/local/src/spawn-fcgi# ./autogen.sh
root@eric-desktop:/usr/local/src/spawn-fcgi# ./configure
root@eric-desktop:/usr/local/src/spawn-fcgi# make
root@eric-desktop:/usr/local/src/spawn-fcgi# make install

root@eric-desktop:/usr/local/src/spawn-fcgi# mkdir -p /home/eric/workspace/phpapp/php_test
root@eric-desktop:/usr/local/src/spawn-fcgi# vim /home/eric/workspace/phpapp/php_test/index.php


<?php echo("hello php world!")?>




root@eric-desktop:/usr/local/src/spawn-fcgi# vim /usr/local/nginx/conf/vhosts/php.lvh.me
server {
       listen 80;
       server_name php.lvh.me;
       if (!-e $request_filename) {
          rewrite ^([_0-9a-zA-Z-]+)?(/wp-.*) $2 last;
          rewrite ^([_0-9a-zA-Z-]+)?(/.*\.php)$ $2 last;
          rewrite ^ /index.php last;
          }
        location / {
        root /home/eric/workspace/phpapp/php_test;
        index index.html index.php index.htm;
        }
        location ~ \.php$ {
        fastcgi_pass 127.0.0.1:9000;
        fastcgi_index index.php;
        fastcgi_param QUERY_STRING $query_string;
        fastcgi_param REQUEST_METHOD $request_method;
        fastcgi_param CONTENT_TYPE $content_type;
        fastcgi_param CONTENT_LENGTH $content_length;
        fastcgi_param SCRIPT_FILENAME /home/eric/workspace/phpapp/php_test/$fastcgi_script_name;
        fastcgi_param REQUEST_URI $request_uri;
        fastcgi_param DOCUMENT_URI $document_uri;
        fastcgi_param DOCUMENT_ROOT $document_root;
        fastcgi_param SERVER_PROTOCOL $server_protocol;
        fastcgi_param GATEWAY_INTERFACE CGI/1.1;
        fastcgi_param SERVER_SOFTWARE nginx;
        fastcgi_param REMOTE_ADDR $remote_addr;
        fastcgi_param REMOTE_PORT $remote_port;
        fastcgi_param SERVER_ADDR $server_addr;
        fastcgi_param SERVER_PORT $server_port;
        fastcgi_param SERVER_NAME $server_name;
        }
}
启动fastcgi
root@eric-desktop:/usr/local/src/spawn-fcgi# spawn-fcgi -a 127.0.0.1 -p9000 -u www-data -f /usr/bin/php5-cgi
root@eric-desktop:/usr/local/src/nginx-0.8.46# killall -9 nginx
root@eric-desktop:/usr/local/src/nginx-0.8.46# nginx
root@eric-desktop:/usr/local/src/nginx-0.8.46# firefox http://php.lvh.me
# 看到hello php world!就对了

3, python项目 nginx+fcgi

root@eric-desktop:/usr/local/src/spawn-fcgi# easy_install flup
root@eric-desktop:/usr/local/src/spawn-fcgi# mkdir -p /home/eric/workspace/python_app/flask_test/
root@eric-desktop:/usr/local/src/spawn-fcgi# vim /home/eric/workspace/python_app/flask_test/app.py



#!/usr/bin/env python
# encoding: utf-8

from flask import Flask
app = Flask(__name__)

@app.route("/")
def hello():
        return "Hello Flask World!"


root@eric-desktop:/usr/local/src/spawn-fcgi# vim /home/eric/workspace/python_app/flask_test/fcgi.py


#!/usr/bin/env python
# encoding: utf-8

from app import app
from flup.server.fcgi import WSGIServer
WSGIServer(app,bindAddress='/tmp/flask_test_app.sock').run()


root@eric-desktop:/usr/local/src/spawn-fcgi# vim /usr/local/nginx/conf/vhosts/python.lvh.me
server{
         listen 80;
         server_name python.lvh.me;
         location / {
             include fastcgi_params;
             fastcgi_param SCRIPT_FILENAME "";
             fastcgi_param PATH_INFO $fastcgi_script_name;
             fastcgi_pass unix:/tmp/flask_test_app.sock;
                 }
     }

root@eric-desktop:/usr/local/src/spawn-fcgi\# python /home/eric/workspace/python\_app/flask\_test/fcgi.py method=prefork/threaded minspare=50 maxspare=50 maxchildren=1000 &
root@eric-desktop:/usr/local/src/spawn-fcgi\# chmod 777 /tmp/flask\_test\_app.sock
root@eric-desktop:/usr/local/src/nginx-0.8.46\# killall -9 nginx
root@eric-desktop:/usr/local/src/nginx-0.8.46\# nginx
root@eric-desktop:/usr/local/src/nginx-0.8.46\# firefox http://python.lvh.me
看到"Hello Flask World!"就对了

This entry was tagged Linux, Nginx, Python, fcgi, passenger, Flask and php

comments powered by Disqus

© 2009-2013 lxneng.com. All rights reserved. Powered by Pyramid

go to Top