Ruby on Apache with mod_ruby - ruby

I really want to run some ruby code on Apache server. I've got libapache2-mod-ruby and libapache-ruby1.8 installed (complete list of what is installed is here). What do I do now to make it run (here`s an example)?

Here's a quick howto to get mod_ruby up and running. A short summary of the steps I used on Ubuntu 12.04 is replicated here:
Install mod_ruby for apache:
sudo apt-get install libapache2-mod-ruby
To enable mod_ruby for all files under /var/www, edit your apache config file, i.e. /etc/apache2/sites-enabled/000-default
I had to add +ExecCGI to Options, and add the section pertaining to Ruby:
<Directory /var/www/>
Options Indexes +ExecCGI
# Ruby with mod_ruby
RubyRequire apache/ruby-run
<Files *.rb>
SetHandler ruby-object
RubyHandler Apache::RubyRun.instance
AddType text/html rb
</Files>
</Directory>
The main drawback that I see is that I can't seem to find a way to set the mime/content type from within the script. You have to set it globally for all ruby scripts with the AddType text/html rb directive.
Finally, you need a test script in /var/www and it must be executable. For example, I have /var/www/test.rb:
#!/usr/bin/ruby
puts "Hello World!<br><pre>"
ENV.each { |k,v|
puts "#{k}=#{v}"
}
puts "</pre>"
Don't forget to make it executable:
chmod a+x /var/www/test.rb
And this outputs:
Hello World!
HTTP_HOST=10.0.1.3
HTTP_CONNECTION=keep-alive
HTTP_CACHE_CONTROL=max-age=0
HTTP_ACCEPT=text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
HTTP_USER_AGENT=Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/36.0.1985.125 Safari/537.36
HTTP_ACCEPT_ENCODING=gzip,deflate,sdch
HTTP_ACCEPT_LANGUAGE=en-US,en;q=0.8
HTTP_COOKIE=__test=1;
PATH=/bin:/usr/bin:/usr/ucb:/usr/bsd:/usr/local/bin
SERVER_SIGNATURE=
Apache/2.2.22 (Ubuntu) Server at 10.0.1.3 Port 80
SERVER_SOFTWARE=Apache/2.2.22 (Ubuntu)
SERVER_NAME=10.0.1.3
SERVER_ADDR=10.0.1.3
SERVER_PORT=80
REMOTE_ADDR=10.0.1.3
DOCUMENT_ROOT=/var/www
SERVER_ADMIN=webmaster#localhost
SCRIPT_FILENAME=/var/www/test.rb
REMOTE_PORT=38188
SERVER_PROTOCOL=HTTP/1.1
REQUEST_METHOD=GET
REQUEST_URI=/test.rb
SCRIPT_NAME=/test.rb
MOD_RUBY=mod_ruby/1.2.6
GATEWAY_INTERFACE=CGI-Ruby/1.1
I've run an Apache benchmark on mod_ruby vs. simple Ruby as CGI (which spins up the Ruby interpreter every request) and mod_ruby was about 8X faster and on par with PHP performance.

mod-ruby is not really the preferred way to go in the ruby community.
The easiest way for you to start it to install passenger (trough gem) and configure apache to use it.
It is really easy to do,
you can follow the official tutorial here: http://www.modrails.com/install.html

Related

Cannot load modules/mod_proxy_wstunnel.so into server

Recently I need integrate a websocket server with apache2,and I find that apache2.4 has supported websocket with the module : mod_proxy_wstunnel.
http://httpd.apache.org/docs/2.4/mod/mod_proxy_wstunnel.html
I compiled them by myself, but encountered a problem :
httpd: Syntax error on line 122 of /usr/local/apache2/conf/httpd.conf: Cannot load modules/mod_proxy_wstunnel.so into server: /usr/local/apache2/modules/mod_proxy_wstunnel.so: undefined symbol: ap_proxy_release_connection
environment info:
OS:centos 6.8
php:php-7.0.10
apache:httpd-2.4.23
My steps are:
Download source code.
php7 github.com/php/php-src/releases/tag/php-7.0.10
apache2.4 github.com/apache/httpd/releases/tag/2.4.23
Copy to my workspace and tar,say /home
shell> tar -xjf php-7.0.10.tar.bz2
shell> tar -xjf httpd-2.4.23.tar.bz2
Install gcc : yum install gcc
Compile apache
down apr:apr-1.5.2.tar.gz apr-util-1.5.4.tar.gz
copy to /httpd-2.4.23/srclib
tar -zvf apr-1.5.2.tar.gz tar -zvf apr-util-1.5.4.tar.gz
Modify the files name: apr-1.5.2 as apr apr-util-1.5.4 as apr-util
Install pcre
yum install pcre-devel
./configure --enable-so --enable-http --enable-proxy --enable-proxy-http --with-included-apr --enable-proxy-wstunnel
make && make install
set apache server ip:
shell> vi /usr/local/apache2/conf/httpd.conf
ServerName 127.0.0.1
Compile php
libxml2:yum install libxml2-devel
mkdir /usr/local/php7
./configure --prefix=/usr/local/php7 --with-mysqli --enable-mysqlnd --with-mysql-sock=/var/lib/mysql/mysql.sock --with-apxs2=/usr/local/apache2/bin/apxs
make && make install
cp -v ./php.ini-production /usr/local/php7/lib/php.ini
configure httpd.conf
shell> vi /usr/local/apache2/conf/httpd.conf
<FilesMatch \.php$>
SetHandler application/x-httpd-php
</FilesMatch>
enalbe mod_proxy_wstunnel
shell> vi /usr/local/apache2/conf/httpd.conf
LoadModule proxy_wstunnel_module modules/mod_proxy_wstunnel.so
Above is my steps. Then I try to start my apache2 :
/usr/local/apache2/bin/apachectl start
Got the following errors:
httpd: Syntax error on line 122 of /usr/local/apache2/conf/httpd.conf: Cannot load modules/mod_proxy_wstunnel.so into server: /usr/local/apache2/modules/mod_proxy_wstunnel.so: undefined symbol: ap_proxy_release_connection
I have google it , but can't find some helpful info.
You need to load mod_proxy as well as mod_proxy_wstunnel. If you load an underlying proxy module (http/fcgi/ajp/wstunnel) without mod_proxy you will get this error.
So check your configuration and make sure you have a LoadModule directive for mod_proxy as well as mod_proxy_wstunnel.
You can also use the output of apachectl -M to check the modules Apache is loading.

How Can I use nginx X-Accel-Redirect in a ruby cgi script

I'm using nginx to run a ruby script. I've already set up the nginx config correctly as it works with php-fpm
In php I set
header('X-Accel-Redirect, file_path)
Is there an equivalent for it in ruby.
I have tried it with
cgi = CGI.new
cgi.out( "X-Accel-Redirect" => new_file)
But it doesn't seem to work...
Is any any gem that I could include.
I'm new to ruby. any help would be highly appreciated.
Assuming you are using a Rack application (Rails, Sinatra, Padrino etc.) the same way you used your php-fpm apps (i.e. behind Nginx as a reverse proxy), you should be able to do the same thing you did in PHP:
response['X-Accel-Redirect'] = file_path

How to setup mod_lua in Apache to access third party Lua modules?

I'm attempting to set up mod_lua module for Apache, but have encountered difficulty regarding accessing third party Lua modules. Say I have a hello_world.lua in Apache's htdocs folder that has something like this:
require "apache2"
function handle(r)
r.content_type = "text/html"
r:write "Hello World from <strong>mod_lua</strong>."
return apache2.OK
end
And I go to "http://localhost/hello_world.lua", that will function as expected. But if I try to add a line such as:
require "socket"
Or
require "cgilua"
I get the following output:
Error!
attempt to call a nil value
However, some modules do work, such as:
require "base"
That functions as expected.
If I navigate to base.lua in the filesystem (c:\program files\lua\5.1\lua\base.lua) and remove this file, then attempt to run my script I get the same error as stated above. So this must be the directory that mod_lua is checking for modules. Modules dlls are not in this folder, instead they are in c:\program files\lua\5.1\clibs\, which I set up the environment variable LUA_CPATH to point to.
Luasocket and cgilua are both present in this folder, yet they cause an error when I try to require them in my script.
From what I can gather, it works fine with any pure lua modules, but anything that has cmodules as well (socket, etc) causes problems.
Additional info:
OS: Windows 7 Home Premium
LUA_PATH = c:\program files\lua\5.1\lua\
LUA_CPATH = c:\program files\lua\5.1\clibs\
Apache version: 2.2.22
mod_lua version: http://www.corsix.org/content/mod-lua-win32#comment-3214
What needs to be done to be able to require modules in scripts run by mod_lua?
It looks like you need to add LuaPackageCPath and/or LuaPackagePath directives to your site configuration (in the global configuration file, or .htaccess, ...).
In your case, I'd assume that
LuaPackagePath c:\program files\lua\5.1\lua\
LuaPackageCPath c:\program files\lua\5.1\clibs\
should do the trick.

Apache2 doesn't execute .rb files (mod_ruby)

I want to make apache2 run ruby scripts. I managed to do this by using CGI, but now I want to go one step further and do it with mod_ruby. I installed mod_ruby through apt-get so it's supposed to be enabled, but when I visit http://localhost/test.rb my browser downloads the file instead of displaying it's output.
If I open /etc/apache2/mods-enabled/ruby.load there is a line containing the path do this mod.
I also added the following lined to my apache2.conf:
<IfModule mod_ruby.c>
<Directory /var/www/>
Options +ExecCGI
</Directory>
RubyRequire apache/ruby-run
#RubySafeLevel 0
<Files *.rb>
SetHandler ruby-object
RubyHandler Apache::RubyRun.instance
</Files>
<Files *.rbx>
SetHandler ruby-object
RubyHandler Apache::RubyRun.instance
</Files>
</IfModule>
Can you please help me?
PS. Please don't suggest using ROR. I'm familiar with that. Now I just want to use ruby for educational reasons. Also please keep things simple I'm a total newbie to apache.
See this article - it has some instructions. Basically you need to tell Apache to pass your Ruby files through the handler.
Also, consider trying Sinatra - it's a micro web framework that's as simple as plain ruby files. And it runs with Passenger which is the well-documented Ruby module for Apache.
The article on HowtoForge covers almost the whole story, but I needed to add one line inside the <Directory /var/www> directive to prevent browsers from downloading the file:
AddType text/html .rb .rbx
Here are a couple more sites that I found helpful:
https://defuse.ca/blog/2012/07/how-to-install-mod_ruby-on-debian-squeeze/
http://modruby.net/en/doc/
Debian Stretch 9.
I tried installing mod_ruby with apt-get install libapache2-mod-ruby but there is no package by that name. In the end, all I needed to do was add this line to the apache-config to get the server to run my scripts.
AddHandler cgi-script .rb

Running Sinatra on port 80

I installed Sinatra and it works but it uses port 4567 by default. I want it to run on port 80.
In an effort to get it to work on port 80, I tried this:
require 'rubygems'
require 'rack/handler/webrick'
require 'sinatra'
Sinatra::Application.default_options.merge!(
:run => false,
:env => :production,
:port => 80
)
get '/' do
"Hello World"
end
But I get this error:
$ ruby -rubygems index.rb
index.rb:5:in `<main>': undefined method `default_options' for Sinatra::Application:Class (NoMethodError)
Any idea what's going on?
Can't you just use (http://www.sinatrarb.com/configuration.html):
set :port, 80
Note that in order to bind a socket to port 80, you'll need to have superuser privileges.
And, by the way,
Using Sinatra.default_options to set base configuration items is obsolete
From: http://www.sinatrarb.com/one-oh-faq
An alternate way to accepted answer
rvmsudo rackup -p 80
In case one is using RVM to manage Ruby versions, you may not be able to use sudo that easily (or else would need to setup ruby in path).
Any port below 1024 is for privileged processes only. You'd have to run as root to run the sinatra app directly on 80. You could reverse proxy - http://sinatra-book.gittr.com/#deployment.
Yes, running anything other than Apache, Nginx, Varnish or HAProxy or port 80 is in my opiniona dangerous game. Those tools are very good at what they do. A reverse proxy setup is the way to go.

Resources