How to run a simple ruby script in any web server (Apache or Mongrel or any thing else)

前端 未结 7 865
半阙折子戏
半阙折子戏 2021-02-05 06:20

It seems very funny to me that when I search something related ruby, all ruby on rails related results popped up. So nobody using raw ruby anymore?

However, I am new to

相关标签:
7条回答
  • 2021-02-05 06:51

    The more commonly used way of running a ruby website is passenger: http://www.modrails.com/ It is not really hard to install and you use, here is he doc for apache: http://www.modrails.com/documentation/Users%20guide%20Apache.html#_deploying_a_ruby_on_rails_application

    Your application must be a valid rack application, here is a minimal hello world (let's say /app is your application's root folder):

    /app/config.ru

    require 'rack'
    require 'app'
    run(app)
    

    /app/app.rb

    app = proc do |env|
      [
        # http status code
        200,
        # headers
        {'Content-Type' => 'text/html'},
        # html body
        ["<head><title>Test Page</title></head><body>Hello World !</body>"]
      ]
    end
    

    Save the files above and create a subfolder /app/public (required by passenger to detect a ruby/rails/sinatra application) and use /app/public as DocumentRoot in your apache config.

    This may look scary but this is for production deployment, in development your really don't want to mess with a real server.

    All you need to run the config.ru file I gave above is:

    $ gem install rack
    $ rackup config.ru
    

    Or if you want to be closer to your production system:

    $ gem install passenger
    $ cd /app
    $ passenger start
    

    which will install you an nginx server with passenger and run your application.

    In most case you will never use rack directly but instead use ruby on rails, sinatra or another framework to generate the html for you (they all use rack below now to provide a common api with the webservers).

    0 讨论(0)
  • 2021-02-05 06:55

    I've heard mod_ruby is good. Unlike, #!/path/to/your/ruby, mod_ruby won't spawn a new ruby interpreter.

    https://github.com/shugo/mod_ruby

    0 讨论(0)
  • 2021-02-05 06:57

    You could configure Apache (for example) to run .rb files as CGI scripts, and then add a shebang line (#!/path/to/your/ruby or maybe #!/usr/bin/env ruby) at the top of the script. It's not optimal, though, as it'd start a new interpreter for each request.

    0 讨论(0)
  • 2021-02-05 07:00

    Ruby 1.9.2+ simple command.

    ruby -run -e httpd . -p 5000
    

    from this article http://til.justincampbell.me/start-an-http-server-with-ruby-run/ other article https://gist.github.com/willurd/5720255

    0 讨论(0)
  • 2021-02-05 07:01

    Run this from your app root.

    ruby -r webrick -e "s = WEBrick::HTTPServer.new(:Port => 8000, :DocumentRoot => Dir.pwd); trap('INT') { s.shutdown }; s.start"
    
    0 讨论(0)
  • 2021-02-05 07:02
    #!/usr/bin/env ruby  //shebang line to indicate path to ruby.
    require 'cgi'       //cgi file to create a simple cgi object.
    cgi = CGI.new      //instantiating a cgi object. 
    puts cgi.header   //thats telling the server about the type(html).
    puts "hello"      // thats the output on the browser.
    
    0 讨论(0)
提交回复
热议问题