ror的生产环境的搭建过程当中可以有多种web server和app server的选择和搭配,其中nginx + mongrel的组合是众多选择当中性能突出的一种,因此,手动搭建一个nginx + mongrel的ROR环境让自己的rails程序流畅的运行便成了事在必行的工作。
建议在搭建ROR的生产过程之前,能够对javaeye老大robbin的关于ROR服务器的相关文章进行阅读,以加深自己对ROR运行环境的认识,这样在大家搭建任何一种主流ROR生产环境的时候,都能明白为什么会这样选择和搭配,充分了解其内部原理,更能够把技术运用自如。
本篇文章主要是介绍在linux环境下搭建nginx + mongrel的集群环境,nginx作为反向代理服务器,里面有很多详细的参数配置没有涉及到,对web server的性能调优望大家用google自行解决一些实际遇到的性能瓶颈问题
我使用的linux环境是redhat5,下载的ruby版本为1.8.7
1、安装ruby解析器
在linux上选择一个目录安装需要的软件,我选择的是/data
mkdir ruby
tar xzvf ruby-1.8.7-p72.tar.gz
cd ruby-1.8.7-p72
./configure -prefix=/data/ruby
make
make install
安装之后用ruby -v来查看是否安装成功,如果安装成功会看到相应的ruby版本号,这里是1.8.7
安装成功之后设置环境变量
vim /etc/profile
在文件最后添加:
export PATH=/data/ruby/bin:$PATH
2、安装gem
gem是ruby的一个融打包,下载,管理等为一体的辅助工具,ruby需要依靠gem来安装其他一些工具包或者
库,
rails框架通常也是通过gem来安装的,下载rubygems-1.3.7.tgz
tar xzvf rubygems-1.3.7.tgz
cd rubygems-1.3.7
ruby setup.rb
安装之后用gem -v查看,如果出现版本号,则证明成功
3、安装rails
在先前的基础上我们通过rubygems直接在线安装rails,这里要指定一个版本号,我用的rails的版本是是2.2.2
gem install rails -v=2.2.2
要等待一会,安装结束后用rails -v来查看,出现版本号说明安装成功
4、安装ruby的mysql数据库适配器
robbin说过在生产环境下要用C编译的mysql数据库适配器,这样才能更好的发挥性能,也可以使用rails自带的数据库适配器,如果没有或者版本不对,可以用gem install mysql来进行安装,我在一开始的时候使用了自带的ruby-mysql适配器,但是启动rails的时候总是出现问题,后来安装了c编译的数据库适配器之后就好了,如果大家用自带的ruby-mysql适配器没有出错的话,也可以使用这个适配器进行工作,不过生产环境下最好采用纯C版本的数据库适配器为好
下载mysql-ruby-2.8.2.tar.gz
tar xzvf mysql-ruby-2.8.2.tar.gz
cd mysql-ruby-2.8.2
ruby extconf.rb --with-mysql-dir=/data/mysql/
make
make install
这里执行安装过程需要指向现已安装好的mysql数据库的根目录,这一步必须先安装mysql数据库,通常linux会自带mysql,并且目录是在/opt/mysql下,可以直接使用,如果不想使用也可以自己用rpm包进行安装,也非常方便
5、安装pcre库
安装nginx必须要有pcre的支持,如果有的话就不必安装了,如果没有请务必安装pcre,下载pcre-8.10.tar.gz
mkdir pcre
tar xzvf pcre-8.10.tar.gz
cd pcre-8.10
./configure -prefix=/data/pcre
make && make install
安装成功之后一定要设置pcre的环境变量:
export PATH=/data/pcre/bin:$PATH
export LD_LIBRARY_PATH=/data/pcre:$LD_LIBRARY_PATH
6、安装nginx
下载nginx-0.8.53.tar.gz
mkdir nginx
tar xzvf nginx-0.8.53.tar.gz
cd nginx-0.8.53
./configure -prefix=/data/nginx
make
make install
如果在./configure这个Makefile的过程当中出现需要依赖pcre的错误的话,则修改为:
./configure -prefix=/data/nginx --with-pcre=/data/pcre-8.10 ---这里指向pcre的解压后的文件夹
成功安装了nginx之后,设置环境变量
export PATH=/data/nginx:$PATH
7、安装mongrel
mongrel的安装十分简单,通过gem命令就可以搞定
使用集群需要安装cluster
gem install mongrel
gem install mongrel_cluster
8、整合nginx + mongrel
随便选定一个目录,然后生成一个rails工程
rails myProject
修改config下的database.yml文件,以满足当前工程和数据库进行匹配
修改好之后进入rails工程根目录,通过命令生成mongrel_cluster的配置文件
mongrel_rails cluster::configure -e production -p 3000 -N 1 -c /data/myProject -a
192.168.1.100
建议写成本机IP地址而不用localhost或者127.0.0.1
完成之后会在config目录产生一篇文件mongrel_cluster.yml,文件内容如下:
address: 192.168.1.100
log_file: log/mongrel.log
port: "3000"
cwd: /data/myProject
environment: production
pid_file: tmp/pids/mongrel.pid
servers: 1
如果启动这里会打开1个mongrel进程,servers这个属性指定需要启动多少个mongrel进程。mongrel的配置暂时停到此处,让我们来看看nginx
修改/data/nginx/conf/nginx.conf文件:
#user nobody;
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
#log_format main '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"';
#access_log logs/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
upstream mongrel{
server 192.168.1.100:3000;
}
server {
listen 80;
server_name localhost;
index index.html index.htm;
charset utf-8;
#access_log logs/host.access.log main;
location / {
root /data/myProject/public;
index index.html index.htm;
proxy_pass http://mongrel;
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
#location ~* ^.+\.(jpg|jpeg|gif|png|ico|css|zip|tgz|gz|rar|bz2|doc|xls|exe|pdf|ppt|txt|tar|mid|midi|wav|bmp|rtf|js|mov)${
# root /data/rails_code/test/public;
#}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias;
# location / {
# root html;
# index index.html index.htm;
# }
#}
# HTTPS server
#
#server {
# listen 443;
# server_name localhost;
# ssl on;
# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;
# ssl_session_timeout 5m;
# ssl_protocols SSLv2 SSLv3 TLSv1;
# ssl_ciphers ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv2:+EXP;
# ssl_prefer_server_ciphers on;
# location / {
# root html;
# index index.html index.htm;
# }
#}
}
修改后的配置文件如上所示,主要添加upstream和local /处的配置,其他的地方略微进行调整即可,使得nginx通过http_proxy来连接mongrel,配置好之后然后再转向rails的工程根目录
使用集群启动:
mongrel_rails cluster::start
启动之后cd到nginx的sbin目录,
nginx ---启动nginx服务器
nginx的端口号是80,则打开浏览器输入http://ip_address/yourController/yourAction来进行访问,如果成功访问到,说明你的生产环境已经搭建成功,但是在真正系统运营当中,需要对nginx的各项参数进行调整以适合客户的访问情况,nginx跟lighttpd有个明显不同的地方在于nginx具有接收缓冲区,而lighttpd则没有,所以根据情况可以对nginx的接收缓冲区的大小进行设置,可以设定为64kb,具体设置方式请google
mongrel_rails cluster::stop是停止mongrel进程
nginx -s stop 是停止nginx服务器 -----最好是cd到nginx/sbin目录下进行command
以上是mongrel+nginx的ROR生产环境搭建,如果有些朋友不喜欢用linux做服务器操作系统,上述操作同样适用于windows,只需对相应的地方做微调即可。