最近想用rails写个小网站,发现要把rails工程发布成production还是要花很多功夫的.之前服务器上装的是nginx+php-fpm来支持php,为了使nginx来服务rails,就必须重新编译nginx,使之支持passenger.用nginx来服务rails大都是直接通过’passenger-install-nginx-module’实现的,但是由于之前已经装了nginx,用这个方法就不方便.本文通过原生编译nginx来使之支持passenger.
个人原创,版权所有,转载请注明出处,并保留原文链接:
http://www.embbnux.com/2014/11/19/compile_nginx_for_php-fpm_passenger_rails/
参考: phusion passenger users guide, nginx version nginx 官方文档
一 php和rails passenger环境
我的电脑是ubuntu 14.04
安装php-fpm:
sudo apt-get install php5-fpm
安装rails passenger
curl -L https://get.rvm.io | bash -s stable source ~/.rvm/scripts/rvm rvm install 2.0.0 rvm 2.0.0 --default gem install rails gem install passenger
查看passenger安装目录,后面会用到:
passenger-config --nginx-addon-dir #类似/home/user/.rvm/gems/ruby-2.0.0-p481/gems/passenger-4.0.48/ext/nginx passenger-config --root #类似/home/user/.rvm/gems/ruby-2.0.0-p481/gems/passenger-4.0.48
二 下载编译nginx
安装编译环境包:
sudo apt-get install build-essential libpcre3-dev libssl-dev zlib1g
下载官方的nginx包,编译安装
wget http://nginx.org/download/nginx-1.7.7.tar.gz tar zxvf nginx-1.7.7.tar.gz cd nginx-1.7.7 ./configure \ --prefix=/usr \ --sbin-path=/usr/sbin/nginx \ --conf-path=/etc/nginx/nginx.conf \ --error-log-path=/var/log/nginx/error.log \ --pid-path=/var/run/nginx.pid \ --lock-path=/var/lock/nginx.lock \ --user=www-data \ --group=www-data \ --with-http_ssl_module \ --with-http_flv_module \ --with-http_gzip_static_module \ --add-module=/home/user/.rvm/gems/ruby-2.0.0-p481/gems/passenger-4.0.48/ext/nginx \ --http-log-path=/var/log/nginx/access.log \ --http-client-body-temp-path=/var/tmp/nginx/client/ \ --http-proxy-temp-path=/var/tmp/nginx/proxy/ \ --http-fastcgi-temp-path=/var/tmp/nginx/fcgi/ make sudo make install
这样就编译安装好了nginx,同时支持php-fpm和passenger,nginx配置目录在/etc/nginx/下
三 nginx配置php-fpm以及passenager
现在配置nginx使其中本地上的两个网站,一个基于php,一个基于ruby on rails
配置文件:/etc/nginx/nginx.conf
user www-data www-data; worker_processes 1; pid /var/run/nginx.pid; events { worker_connections 1024; } http { passenger_root /home/user/.rvm/gems/ruby-2.0.0-p481/gems/passenger-4.0.48; passenger_ruby /home/user/.rvm/wrappers/default/ruby; passenger_max_pool_size 10; include mime.types; default_type application/octet-stream; keepalive_timeout 10; types_hash_bucket_size 64; server_names_hash_bucket_size 128; client_max_body_size 2m; index index.php index.html index.htm; upstream php { #this should match value of "listen" directive in php-fpm pool server unix:/var/run/php5-fpm.sock; # # server 127.0.0.1:9000; # # } #php server server { server_name 127.0.0.1; root /var/www/webphp; index index.php; location ~ /\. { deny all; } location ~* /(?:uploads|files)/.*\.php$ { deny all; } location / { try_files $uri $uri/ /index.php?$args; } location ~ [^/]\.php(/|$) { fastcgi_split_path_info ^(.+?\.php)(/.*)$; if (!-f $document_root$fastcgi_script_name) { return 404; } #works with "cgi.fix_pathinfo = 1" in /etc/php.ini (default) include fastcgi.conf; fastcgi_index index.php; fastcgi_pass php; } } #rails server server { server_name localhost; root /home/user/railsapp/public; location / { #root html; root /home/user/railsapp/public; index index.html index.htm; passenger_enabled on; rails_env production; } error_page 500 502 503 504 /50x.html; location = /50x.html { } } }
然后启动nginx就可以了: sudo nginx