简体   繁体   中英

Running a Ruby program through PHP on iPage

Recently I have been doing a lot of development in Ruby and would like to implement some ruby code I have to my iPage web-server. It seems that the only way for me to do this is to upload my ruby files to the server and them run them from a PHP script by calling:

<?php

system("ruby web-test.rb");

?>

My question is should I even bother doing this, and if so how does the performance play into everything? For example let's say I wanted to use a Ruby program to parse text in a file for a specific string? Would I be better off just doing this all in PHP?

Generally you're better doing this completely in PHP unless you have a significant amount of code in Ruby that necessitates breaking it out into a separate program or where translating your Ruby code to PHP is too time-consuming to be practical.

Even then, don't forget you can have a mixed environment with a Rails or Sinatra app running in parallel with your PHP application. You can interface between them at the server level with mapping certain URLs to one or the other, or by making the PHP app interface with your Ruby app over an HTTP API.

You should evaluate if PHP or Ruby is the best call for your overall application and try and steer towards using one predominantly if not exclusively. They both have their various strengths and weaknesses, so it's hard to say which is best for you without knowing more about your requirements.

Here is a picture of the basic command line I made with PHP, HTML, CSS & jQuery which seems to be able to run Ruby files. Seems a little but much, but I'm having for so whatever!

Basic Command Line

Below is the basic terminal application which consists of two parts:

1) cmd.php - a simple php file to process system commands

2) index.php - a user interface to interact with it.

Its a pretty basic command line interface, but a good place to start!

<?php

// Be sure to set your password from 1234567 to something more secure!!!
define("MY_PASSWORD", "1234567");  //  <--- Important cange to your password

// Check if there is a command and password submitted before executing
if (isset($_POST['cmd']) == true &&  $_POST['key'] == MY_PASSWORD) { 

    system($_POST['cmd'],$output);  // execute request

      // Return error if output is not equal to 0
     if ($output == 127) { err("invalid command..."); } 
else if ($output == 1) { err("operation no permitted..."); } 
else if ($output == 2) { err("no such file / directory..."); } 
else if ($output == 3) { err("no such process..."); } 
else if ($output == 4) { err("interrupted system call!"); }
else if ($output == 5) { err("i/o error..."); }
else if ($output != 0) { err("error ($output)..."); }
} else {
    echo(-1); // Login Failed!
}
function err($msg) {
    echo("<span class='errs'>$msg</span>"); // error message
}

?>

And here is the user interface, be sure to save them both in same directory and update the password from 1234567 in the file above to something else...

<html>
<head>
<title>iCMD</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js></script>
<style>
body, html { background-color:#000000; color:#1AD6FD; outline:0; padding:0; outline:0; margin:0; border:0; }
input { display:inline-block; background-color:transparent; font-family:monospace; font-size:14px; padding:10px; outline:none; border:none; min-height:60px; height:10%; width:100%; color:#FFF; }
#cmd-frame { background-color:#000000; position:fixed; bottom:0px; right:0px; left:0px; }
#frame { background-color:#111111; margin-top:-50px; }
#sys { overflow:auto; padding:10px; height:90%; }
.cmdo { color:#FFFFFF; margin-left:10px; margin-top:5px; margin-bottom:5px; }
.code { color:#FFFFFF; }
.cmdi { color:#1AD6FD; }
.cmdc { color:#39FF14; }
.errs { color:#FF2A68; }
</style>
</head>
<body>
<div id="frame">
<pre><code>
    <div id="sys"></div>
<code><pre>
</div>
<div id="cmd-frame">
    <table width='100%' padding="0px"><tr>
        <td width='150px'><span style='padding-left:8px;'> [command-line: ~ $</span></td><td><input type="txt" id="cmd" name="cmd" placeholder=""></td>
    </tr></table>
</div>
<script>
inp("Welcome to the command line terminal! (V. 1.0)");
inp("Please enter your password to continue...");

mem = ["Hello, world"];
key = "";
idx = 0;

shiftlock = false;
$(document).on('keyup', function(e) { shiftlock = e.shiftKey });
$(document).on('keydown', function(e) {
    shiftlock = e.shiftKey
    scrolling = false; 
    if (e.which == 38) {
        scrolling = true;
        idx -= 1;
    } else if (e.which == 40) {
       scrolling = true;
       idx += 1;  
    }
    if (idx < 0) {
        idx = 0;
    }
    if (idx > mem.length) {
        idx = mem.length;
    }
    if (scrolling == true) {
        mem.push($('#cmd').val());
        $('#cmd').val(mem[idx]);
    }
});
$('#cmd').keypress(function(e) {
    if (e.which == 13 && shiftlock == false) {
        command = $("#cmd").val();
        mem.push(command);
        idx = mem.length;
        if (key) {
           inp(command);
           exe(command);
        } else {
           inp("******");
           usr(command);
        }
    } 
});
function usr(tmp) {
    $.post("cmd.php",{ 
        cmd: "echo success!",
        key: tmp
    },
    function(data, status) {
        if (parseInt(data) != -1) {
            key = tmp;
            out(data);
        } else {
            out("<span class='errs'>login failed...</span>");
        }
    }); 
}
function exe(cmd) {
    cmd = cmd.replace("read ", "ruby read.rb ");
    cmd = cmd.replace("save ", "ruby save.rb ");

    // Important to change cmd.php to where your php component is (same directory)!
    $.post("cmd.php",{ 
        cmd: cmd,
        key: key
    },
    function(data, status) {
        out(data)
    }); 
}
function inp(cmd) {
    $('#cmd').val("");
    $("#sys").append("<span class='cmdi'>[command-line: ~ $ <span class='code'>" + cmd + "</span></span>\n");
    dwn();
}
function out(msg) {
    if (msg) { $("#sys").append("<p class='cmdo'>"+msg+"</p>"); }
    dwn();
}
function err(msg) {
    $("#sys").append("<p class='errs'>"+msg+"</p>");
    dwn();
}
function dwn() {
    var e = document.getElementById("sys");
    e.scrollTop = e.scrollHeight + 30;
}
</script>
</body>
</html> 

Basically it can process only 1 request at a time, and keeps no track of memory in it self so is limited in what it can do. However, it works rather quickly and is quite powerful, so don't go typing in rm -rf ;)

It is possible to use ruby with cgi-interface. Some hosters allow this. I fear ipages does not allow this, they only mention php, perl and python: http://www.ipage.com/knowledgebase/beta/article.bml?ArticleID=187 You can ask the help desk of the ipage if they allow you to run ruby scripts.

A basic ruby cgi script can look like this:

#!/usr/bin/ruby

puts "HTTP/1.0 200 OK"
puts "Content-type: text/html\n\n"
puts "<html><body>This is a test</body></html>"

Try to put in in a file 'example.rb' or 'example.cgi' to try to call it with an url.

( Unfortunately many ruby users use a big framework called ruby on rails and have forgotten that there are also simplier options. You will not get much help from them. If you search on the web, look for ruby and "cgi".)

In my opinion, it is much nicer to parse text in ruby than in php.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM