Article:
Dùng Pound làm frontend cho Rails
1300
jishin.myopenid.com 18Updated 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.shBâ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.
$ ./mongrel_cluster.sh start # start cluster
$ ./mongrel_cluster.sh stop # stop cluster
Tham khảo
[còn tiếp]
18
Updated over 3 years ago