Running Ruby app on Apache

2019-02-26 03:38发布

问题:

I have been learning Ruby lately, and I want to upload a test web application to my server. But I can't figure out how to get it to run on my shared hosting.

My Hosting Details

  • Shared Hosting with JustHost (see here for list of features)
  • OS: Linux
  • Apache: 2.2.11
  • cPanel: 11.25.0-STABLE
  • No SSH access.
  • Can install Ruby Gems.
  • Can't install Apache modules.
  • Can "Manage Ruby on Rails Applications" through cPanel.
  • Mongrel gem is installed.

I built the following simple HelloWorld Ruby Rack app using Sinatra:

#!/usr/bin/ruby ruby
require 'rubygems'
require 'sinatra'
get '/hi' do
  "Hello World!"
end

I just can can't figure out how to "start" the application. Do I need to tell Mongrel (or maybe Apache) that the application exists somehow? How do I start this app running? I am happy to provide more info if needed.

回答1:

firstly you have to start your application manualy or by script when the server is starting. Just do something like ruby hi.rb (as described on sinatra webpage it runs appication on the port 4567). Then you have two options. 1) You can access this application directly as: http://yourserver:4567/ or 2) you can use apache as a proxy.

If you want use apache as a proxy you have to use virtualhost servers. for example:

NameVirtualHost hi.server:80
<VirtualHost hi.server:80>
    Servername hi.server
    RewriteEngine On
    <Proxy balancer://hi>
        BalancerMember http://127.0.0.1:4567
    </Proxy>
    ProxyPass / balancer://hi/
    ProxyPassReverse / balancer://hi/
</VirtualHost>

And if you have ie multiple cores you can run hi.rb more then once (each time on diferent port) and you just add new BalancerMember. You can also switch on apache cache using directive: CacheEnable mem /