2010-06-13

如何用apache+mongrel部署Rails应用

  1. 安装mongrel和mongrel_cluster
    sudo gem install mongrel mongrel_cluster
    由于用gem安装后mongrel_rails命令被放在 /var/lib/gems/1.8/bin/mongrel_rails,使用不方便,所以建议建立符号链接
    sudo ln -s /var/lib/gems/1.8/bin/mongrel_rails /usr/bin/mongrel_rails
  2. 配置并启动 mongrel_cluster 在Rails项目的根目录下执行以下命令,生成config/mongrel_cluster.yml 文件,供启用mongrel集群使用
    sudo mongrel_rails cluster::configure -e production -p 8000 -a 127.0.0.1 -N 3
    有关cluster::configure 更多参数使用可借助帮助命令查看
    mongrel_rails cluster::configure -h
    生成的文件内容如下:
    ---
    address: 127.0.0.1
    log_file: log/mongrel.log
    port: "8000"
    environment: production
    pid_file: tmp/pids/mongrel.pid
    servers: 3
    
    启用mongrel_cluster
    wangsheng@pc01:/opt/redmine/web$ sudo mongrel_rails cluster::start
    starting port 8000
    starting port 8001
    starting port 8002
    
  3. 添加虚拟主机 在/etc/apache2/sites-available 目录下 test 文件,配置Rails程序的虚拟主机。 文件的内容如下
    <VirtualHost *:80>
        # Server name
        ServerName test.example.com
    
        # Proxy ACL
        <Proxy *>
            Order allow,deny
            Allow from all
        </Proxy>
    
        # config mongrel cluster proxy
        <Proxy balancer://mongrel>
            BalancerMember http://127.0.0.1:8000
            BalancerMember http://127.0.0.1:8001
            BalancerMember http://127.0.0.1:8002
        </Proxy>
    
        # Proxy directives
        ProxyPass / balancer://mongrel/
        ProxyPassReverse / balancer://mongrel/
        ProxyPreserveHost on
    
        # Logfiles
        ErrorLog  /var/log/apache2/test.example.com.error.log
        CustomLog /var/log/apache2/test.example.com.access.log combined
    
    </VirtualHost>
    注意:前提是你需要开启apache代理以及代理均衡模块(proxy和proxy_balancer)。
  4. 测试配置是否成功 重启apache,在浏览器输入 test.example.com,按回车键。如果正常显示应用程序的主页,则证明配置成功。
blog comments powered by Disqus