Vinova tuyển lập trình viên Mobile & Web ở Hà Nội, lương $300-1000

Article: Dùng Pound làm frontend cho Rails 1300

jishin.myopenid.com 18
Updated over 3 years ago

Khi chạy chương trình Rails trên server thực sự trong producton environment, cần chạy vài instance của chương trình một lúc để đảm bảo cùng lúc trang web phục vụ được nhiều request.

Tuy nhiên Rails không thread-safe. Do đó phải chạy nhiều process của chương trình cùng lúc. Giải pháp chạy vài process Mongrel làm backend để chạy Rails theo kiểu cluster, và server gì đó như Apache, Lighttpd, Nginx làm frontend là best practice phổ biến.

Bài viết này xin trình giới thiệu cách dùng Pound làm frontend. Nó nhỏ gọn dễ cài đặt và config, có đầy đủ tính năng virtual host, SSL, nên có lẽ thỏa mãn nhu cầu của hầu hết project Rails.

Dưới đây là hướng dẫn dành cho Debian/Ubuntu. Các *nix khác tương tự.

Cài đặt Pound

apt-get install pound

Config Pound

Giả sử ta sẽ chạy 3 Mongrel servers trên port từ 3001 đến 3003. Sửa /etc/pound/pound.cfg thành như sau:

ListenHTTP
Address 0.0.0.0
Port 80
Service
BackEnd
Address 127.0.0.1
Port 3001
End
BackEnd
Address 127.0.0.1
Port 3002
End
BackEnd
Address 127.0.0.1
Port 3003
End
End
End

Sửa file /etc/default/pound, đổi startup=0 thành startup=1.

Pound còn nhiều tính năng khác, xin tham khảo tài liệu để biết cách config.

Cài đặt Mongrel

gem install mongrel --include-dependencies
gem install mongrel_cluster --include-dependencies

Chú ý cần mongrel_cluster.

Config Mongrel

Tạo file config cho mongrel cluster cho ứng dụng. Ví dụ tạo ở /path/to/your/app/config/yourapp_mongrel.yml với nội dung như sau:

port: "3001"
cwd: /path/to/your/app/root
environment: production
address: 127.0.0.1
pid_file: log/mongrel.pid
servers: 3

Tiếp theo đó ta phải tạo startup script cho mongrel cluster. Tạm thời gọi là mongrel_cluster.sh , đặt ở /path/to/your/app/script/ . Bạn có thể dùng script này để start toàn bộ mongrel server lên, tuy nhiên cần chú ý đến thư mục đặt yourapp_mongrel.yml . Nếu bạn chạy nhiều ứng dụng Rails hãy chọn cách bỏ chung tất cả yourapp???_mongrel.yml vào một chỗ để tiện cho việc khởi động (vd: /opt/app/mongrel/).

#!/bin/bash
#
# Copyright (c) 2006 Bradley Taylor, bradley@railsmachine.com
#
# mongrel_cluster Startup script for Mongrel clusters.
#
# chkconfig: - 85 15
# description: mongrel_cluster manages multiple Mongrel processes for use \
# behind a load balancer.
#

# CONF_DIR points to the folder contains yourapp_mongrel.yml
CONF_DIR="/path/to/your/app/config"  
RETVAL=0

case "$1" in
start)
mongrel_cluster_ctl start -c $CONF_DIR
RETVAL=$?
;;
stop)
mongrel_cluster_ctl stop -c $CONF_DIR
RETVAL=$?
;;
restart)
mongrel_cluster_ctl restart -c $CONF_DIR
RETVAL=$?
;;
*)
echo "Usage: mongrel_cluster {start|stop|restart}"
exit 1
;;
esac

exit $RETVAL

Chạy

Chạy mongrel_cluster.sh vừa tạo.

$ chmod +x mongrel_cluster.sh
$ ./mongrel_cluster.sh start              # start cluster
$ ./mongrel_cluster.sh stop               # stop cluster
Bây giờ thử access vào localhost, bạn sẽ thấy ứng dụng chạy tức là quá trình deploy đã thành công. Xin chúc mừng.

Tham khảo

[còn tiếp]

1 2 3 

Editors
jishin.myopenid.com 18
ngocdaothanh.myopenid.com 172

Comments

ngocdaothanh.myopenid.com 172
Updated over 3 years ago

Pen và Balance còn đơn giản nhanh gọn hơn nữa. Ví dụ thời gian install đến lúc chạy được Pen chỉ mất chừng 10 giây trên Ubuntu.

Tuy nhiên mấy cái như Pound, Pen, Balance chỉ là... balancer, không phải là HTTP (web) server nên không serve được tập tin tĩnh, nếu tập tin tĩnh to cỡ chục MB trở lên thì tốc độ chương trình Rails bị ảnh hưởng rất nhiều.

Nếu dùng Apache với mod_proxy_balancer, để serve tập tin tĩnh cần thêm

RewriteCond %{DOCUMENT_ROOT}/%{REQUEST_FILENAME} !-f

trước

RewriteRule ^/(.*)$ balancer://mongrel_cluster%{REQUEST_URI} [P,QSA,L]

Nếu dùng với mod_proxy, cần thêm

ProxyPass /images !
ProxyPass /javascripts !
ProxyPass /stylesheets !

Alias /images /path/to/public/images
Alias /javascripts /path/to/public/javascripts
Alias /stylesheets /path/to/public/stylesheets

trước

ProxyPass / http://localhost:3001/
ProxyPassReverse / http://localhost:3001/
ProxyPreserveHost on

You must login to be able to comment

Uploaded files

No file uploaded yet

You must login to be able to upload

Nhà tài trợ:

Mọi người đều tự do viết bài, sửa bài của người khác, và bình luận ở trang web này. Bạn muốn chủ động tạo bài mới để chia sẻ kinh nghiệm với mọi người? Xin click link ở dưới.

Create new content