出处:http://www.javaeye.com/topic/105869
最近网上有很多人在推荐使用nginx+mongerl来搭建了rails的生产环境,今天研究了一下。
在ubuntu上安装nginx比较简单 sudo apt-get install nginx 就可以了,如果要安装最新的版本可以去nginx的官方网站上去下载。
ubuntu上搭建rails的已经存在ubuntu的源库中,只要使用apt-get install rails 但是安装的不是最新的的版本。在gem install 分别安装 mongel 和Mongrel_cluster。
假如你的rails工程放在/var/www/myapp/下
代码
1. adduser mongrel
2. cd /var/www/myapp
3. rails test
4. cd test
5. mongrel_rails start
6. chown -R mongrel:mongrel /var/www/myapp/test
以上命令多是root用户
访问一下http://localhost:3000 看一下你的mongrel是否能启动 能启动,则继续搭建mongerl的集群,
代码
1. sudo mongrel_rails cluster::configure -e production
2. \-p 8000 -N 3 -c /var/www/myapp/test -a 127.0.0.1
3. \ --user mongrel --group mongrel
这样会在test工程下生成一个config/mongrel_cluster.yml 这样一个配置文件。记录了相关的配置信息。
代码
1. mongrel_rails cluster::start
就可以把起来了三个mongrel进程。
你也可以把mongrel_rails 作为系统服务
代码
1. $ sudo mkdir /etc/mongrel_cluster
2. $ sudo cp /var/www/myapp/test/config/mongrel_cluster.yml \
3. /etc/mongrel_cluster/testapp.yml
4. $ sudo cp \
5. /path/to/mongrel_cluster_gem/resources/mongrel_cluster/mongrel_rails \
6. /etc/init.d/
7. $ sudo chmod +x /etc/init.d/mongrel_cluster
你就可以在/etc/init.d/mongrel_cluster start 来启动这个mongrel的集群
接下来配置nginx 他的配置文件比较简单/etc/nginx/nginx.conf
下面可以帖一个简单的配置文件
代码
1. user mongrel;
2. worker_processes 1;
3.
4. error_log /var/log/nginx/error.log;
5. pid /var/run/nginx.pid;
6.
7. events {
8. worker_connections 1024;
9. }
10.
11. http {
12. include /etc/nginx/mime.types;
13. default_type application/octet-stream;
14.
15. access_log /var/log/nginx/access.log;
16.
17. sendfile on;
18. #tcp_nopush on;
19.
20. #keepalive_timeout 0;
21. keepalive_timeout 65;
22. tcp_nodelay on;
23.
24. upstream mongrel {
25. server 127.0.0.1:8000;
26. server 127.0.0.1:8001;
27. server 127.0.0.1:8002;
28. }
29.
30. gzip on;
31.
32. server {
33. listen 80;
34. server_name localhost;
35.
36. access_log /var/log/nginx/localhost.access.log;
37.
38. location / {
39. root /var/www/myapp/test/public;
40. index index.html index.htm;
41. }
42. location / {
43. proxy_pass http://mongrel;
44. proxy_redirect off;
45. proxy_set_header Host $host;
46. proxy_set_header X-Real-IP $remote_addr;
47. proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
48. }
49. 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)$ {
50. root /var/www/myapp/test/public;
51. }
52.
53. }
54.
55. }
在nginx重启一下 访问一下http://localhost 一个简单的rails的生产环境就搭建完成了
最新回复
还要配置呀...晕呢...
代码
1. $ sudo mkdir /etc/mongrel_cluster
2. $ sudo cp /var/www/myapp/test/config/mongrel_cluster.yml \
3. /etc/mongrel_cluster/testapp.yml
4. $ sudo cp \
5. /path/to/mongrel_cluster_gem/resources/mongrel_cluster/mongrel_rails \
6. /etc/init.d/
7. $ sudo chmod +x /etc/init.d/mongrel_cluster
搞不明白第 5 行命令所指的目录!
请哪位给解释一下?谢谢!
你在linux的console下试试看就直到了,shell命令后面增加"\"表示换行继续写命令而不是马上执行
"\"忽略"\"后面紧跟的回车操作
QUOTE:
谢谢 lgn21st !我知道4,5,6行是一条连续的语句,但就是不明白单第 5 行这行所指代的目录,别的都能配置成功了,就差不明白这行,
所以不能在
/etc/init.d/下直接启动 mongrel_cluster 集群
请你再解释一下好吗?谢谢了!
[ 本帖最后由 yjgz 于 2007-10-18 08:23 编辑 ]
是这样的,其实sudo gem install mongrel_cluster安装mongrel_cluster这个gem后
你已经可以启动,停止,重启配置好的mongrel集群了
在Linux的/usr/bin下面可以找到命令文件"mongrel_cluster_ctl",用这个命令
mongrel_cluster_ctl start|stop|restart 就已经可以手动控制mongrel集群了
但是你想让服务器重新启动的时候顺便把mongrel集群一起给启动了,配置etc/init.d
在我的电脑上,mongrel_cluster的位置在
/usr/lib/ruby/gems/1.8/gems/mongrel_cluster-1.0.2/resources
就是你安装gem的位置,或者下载一个mongrel_cluster的压缩包,解压缩后也可以找到
QUOTE:
谢谢!非常多谢 lgn21st 的指点!原来如此,终得明白了!
呵呵……
QUOTE:
You can give R Bi to the person who helped you, right? Then others will help you again.http://www.ubuntu.com/getubuntu/download
QUOTE:
只有一个服务器, 条件不够, 只能上apache, 哈.性能差别主要在于通用web server和专用proxy web server的区别吧.