RSS
 

Archive for August, 2010

[DEPRECATED]Ragios: Ruby based System and Network Monitoring Tool

31 Aug

This post has been deprecated. See the new version of Ragios here: http://github.com/obi-a/Ragios

While reading the book “Nagios: system and Network Monitoring” by Wolfgang Barth, I thought it will be a good idea to write a Ruby based Object Oriented System and Network Monitoring tool similar to Nagios.

So I started writing such a tool. Since it was inspired by Nagios, I will call it Ragios (Ruby Agios) aka Saint Ruby (St. Ruby) since Agios is Saint in Greek.

This is all just for fun and educational purposes.

Ragios could be used to monitor any type of system including servers, workstations, switches, routers, system services and applications, locally or over a network. The system admin can define the tests he wants to run on the system or re-use tests written by other developers. The tests run periodically on the system. When a test fails the admin receives an email or SMS alert from Ragios. Everything is written in ruby, the configuration settings are all in ruby syntax and Ragios itself is a ruby script.

Ragios is Object Oriented so everything is an object. The Base object is the System_monitor defined by the System_monitor class. This class defines the behavior of all system monitors.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
class System_monitor
#base class that defines the behavior of all system monitors
 
   attr_reader :test_description   # a short description of the test performed by this system monitor
   attr_reader :hostname #hostname of the system being monitored
   attr_reader :address   #ip address of the system being monitored
   attr_reader :contact    #email address of admin to contact when a test fails
   attr_reader :test_result #results of a test
 
   def initialize
   end
 
   def test_command
   #defines the tests to run on a system
   end
 
   def failed
   #defines the action to take when a test fails
   #- May send email or SMS to the admin under contact
   #- May also take action to fix the issue like restart a process/service
   end
end

The method test_command defines the test that will be performed on the system periodically by the system monitor. If the test is successful test_command will return TRUE, if it fails the method will return FALSE. The test_command method can run any type of test, it just needs to return values TRUE for a PASSED test and FALSE for a FAILED test.

The failed method defines the actions to be performed when a test fails. These actions may include sending email or SMS to alert the system admin about a problem. It may also include actions to fix the problem.

After every test any results from the test like an error message for a failed test is saved in the test_result variable. This should be implemented in the test_command method.

Ragios monitors 2 types of systems, Hosts and Services. Hosts are devices that run on a network like computers, routers, switches and servers. Services are applications and processes like a web server, database, web apps, and system processes.

Hosts and Services both extend the System_monitor class. They both inherit all the characteristics of a System_monitor.

The Host class

1
2
3
4
5
6
7
class Host < System_monitor
#defines how computers,servers,network devices will be monitored
 
    def initialize
     super
    end
end

The Service class

1
2
3
4
5
6
class Service < System_monitor
#defines how services, processes and applications will be monitored
    def initialize
     super
    end
end

Now we have the basic framework in place. With Ragios the system admin implements his own system monitors or reuses system monitors already implemented by other developers.

Lets implement a simple system monitor that monitors a webpage and alerts the owner when the site is down. This is simply done by sending http requests to the site’s home page and alerting the site owner if it fails to get a HTTP 200 OK status response from the server.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
class Test_webpage_for_http_200 < Service
#monitors a webpage to check if the site is loading
#PASSED if it gets a HTTP 200 Response status code from the website
 
   attr_reader :test_url
 
   def initialize
        super
   end
 
   def test_command
     #returns true when http request to test_url returns a 200 OK Response
       response = Net::HTTP.get_response(URI.parse(test_url))
        @test_result = response.code
       if response.code != "200"
            return FALSE
       else
	    return TRUE
       end
   end
 
   def failed
         #send email notification to site owner
         #not yet implemented
   end
end

Test_webpage_for_http_200 extends the Service class because it is testing http which is a service. it also has all the characteristics of the base class System_monitor. We just needed to override the test_command method to implement our specific test.

def test_command
     #returns true when http request to test_url returns a 200 OK Response
       response = Net::HTTP.get_response(URI.parse(test_url))
        @test_result = response.code
       if response.code != "200"
            return FALSE
       else
	    return TRUE
       end
   end

test_command sends http requests to the test_url and returns TRUE when it gets a 200 OK Response, returns FALSE when it gets a different response code. The response code is saved in @test_result as the result of this test. This value gives a system admin insight on what has gone wrong.

The failed method should define the actions to take when a test fails. In this case it sends an email to notify the site owner. The email feature is not yet implemented.

Test_webpage_for_http_200 is a generic system monitor, it doesn’t test any particular site, we still need to write system monitors that test specific sites for us. These monitors will be subclasses of Test_webpage_for_http_200. Lets write some now

1
2
3
4
5
6
7
8
class Test_my_blog < Test_webpage_for_http_200
#monitors my blog, to check if the blog's home page is loading
   def initialize
      @test_description  = "Testing My Blog"
      @test_url = "http://obi-akubue.homeunix.org/"
      super
   end
end

Running

Test_my_blog.new.test_command

will send a http request to my blog’s home page http://obi-akubue.homeunix.org/ and return TRUE if it gets a 200 Ok Response and FALSE if it gets anything else.

Lets write another test, this one monitors my website http://whisperservers.com/blog.

1
2
3
4
5
6
7
class Test_my_site < Test_webpage_for_http_200
   def initialize
      @test_description  = "Testing my website site"
      @test_url = "http://www.whisperservers.com/blog/"
      super
   end
end

Running

Test_my_site.new.test_command

will send a http request to my site’s home page http://www.whisperservers.com/blog/ and return TRUE if it gets a 200 Ok Response and FALSE if it gets anything else.

Ragios will run all our tests and alert us when a test fails.

1
2
3
4
5
6
7
8
9
10
11
jobs = [Test_my_blog.new, Test_my_site.new]
 
jobs.each do |job|
 puts job.test_description
 if job.test_command
        puts "   [PASSED]"
     else
         puts "   [FAILED]"
         job.failed
     end
end

We just need to add our tests to the jobs Array.

jobs = [Test_my_blog.new, Test_my_site.new]

Ragios will alert us when a test fails by calling the failed method.

Complete source code for Ragios

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#!/usr/bin/ruby
#:Title   :Ragios (aka St. Ruby)
#:Date    :8/26/2010
#:Author  :Obi Akubue
#:Version :0.1
#description: This is the framework for a Ruby Based System and Network Monitoring Tool
 
 
require 'net/http'
 
class System_monitor
#base class that defines the behavior of all system monitors
 
   attr_reader :test_description   # a short description of the test performed by this system monitor
   attr_reader :hostname #hostname of the system being monitored
   attr_reader :address   #ip address of the system being monitored
   attr_reader :contact    #email address of admin to contact when a test fails
   attr_reader :test_result #results of a test
 
   def initialize
   end
 
   def test_command
   #defines the tests to run on a system
   end
 
   def failed
   #defines the action to take when a test fails
   #- May send email or SMS to the admin under contact
   #- May also take action to fix the issue like restart a process/service
   end
end
 
 
class Host < System_monitor
#defines how computers,servers,network devices will be monitored
 
    def initialize
     super
    end
 
end
 
class Service < System_monitor
#defines how services will be monitored
 
    def initialize
     super
    end
end
 
class Test_webpage_for_http_200 < Service
#monitors a webpage to check if the site is loading
#PASSED if it gets a HTTP 200 Response status code from the website
 
   attr_reader :test_url
 
   def initialize
        @contact = "Obi@whisperservers.com"
        super
   end
 
   def test_command
     #returns true when http request to test_url returns a 200 OK Response
       response = Net::HTTP.get_response(URI.parse(test_url))
        @test_result = response.code
       if response.code != "200"
            return FALSE
       else
	    return TRUE
       end
   end
 
   def failed
         #send email notification to site owner @contact
         #not yet implemented
   end
end
 
class Test_my_site < Test_webpage_for_http_200
   def initialize
      @test_description  = "Testing my website site"
      @test_url = "http://www.whisperservers.com/blog/"
      super
   end
end
 
class Test_my_blog < Test_webpage_for_http_200
#tests my blog, to check if the blog is loading
 
   def initialize
      @test_description  = "Testing My Blog"
      @test_url = "http://obi-akubue.homeunix.org/"
      super
   end
end
 
 
jobs = [ Test_my_site.new, Test_my_blog.new]
 
jobs.each do |job|
 puts job.test_description
 if job.test_command
        puts "   [PASSED]"
     else
        puts "   [FAILED]"
        job.failed
     end
end

In its current form Ragios is just a simple ruby script, the system admin has to edit this script to add tests and configuration settings, or reuse tests already available.

Any setting that applies to more than one test should be added to the initialize method of the super class of those tests. In the example above we added

@contact = "Obi@whisperservers.com"

to the initialize method of Test_webpage_for_http_200. This allows our tests (Test_my_blog and Test_my_site) to use the same contact = "obi@whisperservers.com" by simply calling the super method in their constructor.

Ragios is still incomplete, for now we may set it to run hourly as a cron job or in the future we may make it into a daemon. This is just the basic framework to extend and play with.

[UPDATE 10-1-2010] View the complete Ragios source with latest revisions
http://github.com/obi-a/Ragios.

 

User Authentication for SysInfo

21 Aug

Since SysInfo, the CGI app we created in the last blog post displays server information on a webpage, it only makes sense to add some basic authentication to protect access to it. This will make it ask for a username and password, before users get access to the App.

This is done by adding the following Directory block to the Apache configuration file.

<directory /usr/lib/cgi-bin/sysinfo/ >
AuthName "Sys-info Login"
Authtype Basic
AuthUserFile /usr/lib/cgi-bin/sysinfo/htpasswd
require valid-user
</directory>

/usr/lib/cgi-bin/sysinfo/ is the location of the Sysinfo App, its the folder we want to protect, putting it in the above Directory block tells Apache to require authentication for access to it.

AuthName is the message displayed by the browser when the server requests authentication.

Authtype Basic means we are using basic Authentication, the password is transmitted without encryption.

AuthUserFile /usr/lib/cgi-bin/sysinfo/htpasswd refers to the file that will store the username and passwords i.e the password file.

require valid-user Tells apache to all only allow access to users found in the password file.

Now we are done with the Apache Configuration file, next step is to create the password file and add a user. This is done with the htpasswd program that comes with Apache.

type:
cd /usr/lib/cgi-bin/sysinfo/
htpasswd -c htpasswd admin

This will create a new password file called htpasswd and a new user called admin. It will prompt you to enter a password for this user. The password file is /usr/lib/cgi-bin/sysinfo/htpasswd. The passwords stored in the file will always be in encrypted form. With the -c option htpasswd will create a new password file.

To add more users;
htpasswd htpasswd another-user
Just omit the -c option since the password file already exists.

Final step, lets make the web user www-data the only one that can read or write the password file.
sudo chown www-data htpasswd
sudo chmod 600 htpasswd

Now restart Apache and try the App, it will require authentication.

 
No Comments

Posted in Linux

 

Checking Up On Your Linux Server from a Webpage

21 Aug

I was playing with CGI. I wrote a simple cgi web app that allows you to see what is going on with your Linux server from a web page. This is just a simple app for educational purposes. Its not Nagios by any means:)

Linux Commands
Fun linux commands used by the app to get server information.

hostname
This will display the hostname of your system on the terminal

uptime
Displays how long the system has been running and the number of users currently logged in.

w
Lists all the users logged into the system, what programs they are running, when they logged in and how active or idle they have been.

free -m
Displays the amount of free memory on the system.

df -h
Displays information about the hard drive, displays the size, used and available space.

ps aux
ps command displays the processes running on the system. The fun thing about the ps command is that you can format the results and choose which parameters to include in the results. You can also sort the results using –sort. An interesting variation of the ps command is

ps -eo start,pid,user,args --sort user
This will display all the processes running on the system, arranged by Time STARTED, PID , USER of that process and COMMAND they are running/location of the executable all ordered by Process USER. More details about the ps command here.

pstree
Displays a tree diagram of all processes running on the system. It shows both the parent and their child (spawn) processes, so it gives you a good idea of what is going on in the system.

System Files
Some system files contain important information about the server. The information from these files are displayed on the terminal using the cat command.

cat /etc/issue
Displays the System’s Linux Distro and the version number.

cat /proc/version
Displays the systems kernel version and build information.

cat /proc/cpuinfo
Displays the system’s CPU information

Displaying Results of a Shell Script on a Webpage
We do this using CGI. CGI is an interface for executing scripts, receiving and processing requests from clients on a web server. Most popular web servers like Apache and lighthttpd support CGI. CGI scripts can be written in any language. To get the webserver to execute a cgi script simply put that script in the cgi-bin directory.
Debian/Ubuntu has its cgi-bin directory at /usr/lib/cgi-bin and CentOS/Fedora/RedHat has its cgi-bin directory at /var/www/cgi-bin. You must have Apache,lighthttpd or a web server that supports CGI running on the system.

Nixcraft already wrote an excellent tutorial on displaying results from shell scripts on a webpage, read it first to see how to do it. Click here for the tutorial. The app I wrote extends the cgi script in that tutorial. I added menu options and more features to it.

SysInfo: The Server Check Up CGI Web App
The APP consists of 4 CGI scripts.

index.cgi – Displays the basic information about about the system.

cpu-info.cgi -Displays the systems CPU details (vendor,model,speed,core etc).

ps.cgi -Displays the Processes running on the system by TIME STARTED, PROCESS ID, USER,COMMAND.

pstree.cgi -Displays the running processes in a tree diagram showing the parent and their child processes.

All the scripts assume they are running from the directory cgi-bin/sysinfo/. To test the App manually, create a folder in your cgi-bin directory called sysinfo.

For Ubuntu/Debian use:
mkdir /usr/lib/cgi-bin/sysinfo

For CentOS/Fedora:
mkdir /var/www/cgi-bin/sysinfo

Now create the scripts with your favorite text editor and save them in the cgi-bin/sysinfo/ folder. You can copy and paste the source code of all the scripts from here:http://whisperservers.com/sysinfo/. you only need to create index.cgi,cpu-info.cgi,ps.cgi and pstree.cgi.

Next step is to make all the scripts executable:
sudo chmod +x index.cgi cpu-info.cgi ps.cgi pstree.cgi

Now you are done, to view the App, open your web browser and type on the address bar http://your-ip/cgi-bin/sysinfo/index.cgi or http://localhost/cgi-bin/sysinfo/index.cgi

To make the app easy to install and test, I wrote a script that will install it for you, so you dont have to do any of the steps described above, you simply run the installer script and open your web browser to use the app.

Using the installer script to Install the App;
Step 1. Download the installer script with

wget http://whisperservers.com/sysinfo/install-sysinfo

Step 2. Make the installer script executable

chmod +x install-sysinfo

step 3. Run the installer

./install-sysinfo

Step 4. Now test the app, open your web browser and type on the address bar http://your-ip/cgi-bin/sysinfo/index.cgi or http://localhost/cgi-bin/sysinfo/index.cgi

The source code for the installer script, install-sysinfo;

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#!/bin/bash
#:Title   :sysinfo - Installer
#:Date    :8/15/2010
#:Author  :Obi Akubue
#:Version :0.1
#description: Installs the Sysinfo cgi scripts
 
red='\e[0;31m'
RED='\e[1;31m'
blue='\e[0;34m'
BLUE='\e[1;34m' #light blue
cyan='\e[0;36m'
CYAN='\e[1;36m'
GREEN='\e[1;32m'
YELLOW='\e[1;33m'
WHITE='\e[1;37m'
NC='\e[0m' # No Color
 
#begining  messages
 if [ "$1" = "-u" ]; then
    #option to install on Ubuntu/Debian
        echo  -e "${GREEN} Welcome to Sysinfo Installer"
        echo " Installing on Ubuntu/Debian Linux ..."
        echo -e "${WHITE} Press ENTER to continue or Ctrl-C to Quit"
 
        echo -e "${NC}"
 
    elif [ "$1" = "-c" ]; then
        #option to install on CentOS/Fedora/Redhat
        echo  -e "${GREEN} Welcome to Sysinfo Installer"
        echo " Installing on CentOS/Fedora/Redhat Linux ..."
        echo -e "${WHITE} Press ENTER to continue or Ctrl-C to Quit"
        echo -e "${NC}"
    else
        echo  -e "${GREEN} Welcome to Sysinfo Installer"
        echo -e "${WHITE} "
        echo "To install on Ubuntu/Debian Abort and type ./sysinfo-installer -u"
        echo "To install on CentOS/Fedora/Redhat Abort and type ./sysinfo-installer -c"
        echo "Press Ctrl-C to Abort"
        echo -e "${NC}"
        read key
        exit
 
   fi
 
 read key
 
#
if [ "$1" = "-u" ]; then
#is Ubuntu/Debian OS
sudo mkdir /usr/lib/cgi-bin/sysinfo
cd /usr/lib/cgi-bin/sysinfo
 
elif [ "$1" = "-c" ]; then
#is CentOS/Fedora/Redhat
sudo mkdir /var/www/cgi-bin/sysinfo
cd /var/www/cgi-bin/sysinfo
fi
 
sudo wget http://whisperservers.com/sysinfo/index
sudo wget http://whisperservers.com/sysinfo/ps
sudo wget http://whisperservers.com/sysinfo/pstree
sudo wget http://whisperservers.com/sysinfo/cpu-info
 
sudo mv index index.cgi
sudo mv ps ps.cgi
sudo mv pstree pstree.cgi
sudo mv cpu-info cpu-info.cgi
 
sudo chmod +x index.cgi cpu-info.cgi ps.cgi pstree.cgi
 
echo  -e "${GREEN} SysInfo has been successfully Installed"
echo "To test it on your browser open"
echo -e "${YELLOW} http://your-ip/cgi-bin/sysinfo/index.cgi or http://your-hostname/cgi-bin/sysinfo/index.cgi"
 echo -e "${NC}"

Source Code for the App: SysInfo
Source for index.cgi.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#!/bin/bash
#:Title   :sysinfo
#:Date    :8/15/2010
#:Author  :Obi Akubue
#:Version :0.1
#description: Displays General System Information on a webpage
echo "Content-type: text/html"
echo ""
echo "<html><head><title>SysInfo"
echo "</title></head><body>"
 
echo "<h1>General system information</h1>"
echo ""
 
echo "Home |"
echo "<a href="/cgi-bin/sysinfo/cpu-info.cgi">CPU Info</a> |"
echo "<a href="/cgi-bin/sysinfo/ps.cgi">Running Pocesses</a> |"
echo "<a href="/cgi-bin/sysinfo/pstree.cgi">Process Tree</a> |"
echo "<a href="">Refresh</a> "
echo ""
echo ""
echo ""
 
echo "<p><b>Hostname: </b>$(hostname -s)</p>"
echo "<p><b>OS: </b>$(cat /etc/issue)</p>"
echo "<p><b>Uptime: </b>$(uptime)</p>"
 
echo "<h1>Logged in users</h1>"
echo "<pre> $(w) </pre>"
 
echo "<h1>Memory Info</h1>"
echo "<pre> $(free -m) </pre>"
 
echo "<h1>Disk Info:</h1>"
echo "<pre> $(df -h) </pre>"
 
echo "<center><b>Running: </b> $(cat /proc/version)</center><p></p>"
echo "<center>Information generated on $(date)</center>"
echo "</body></html>"

Source for cpu-info.cgi

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#!/bin/bash
#:Title   :sysinfo ->cpu info
#:Date    :8/15/2010
#:Author  :Obi Akubue
#:Version :0.1
#description: Displays General System Information on a webpage
echo "Content-type: text/html"
echo ""
echo "<html><head><title>CPU Info"
echo "</title></head><body>"
 
echo "<h1>General system information</h1>"
echo ""
 
echo "<a href="/cgi-bin/sysinfo/index.cgi">Home</a> |"
echo "CPU Info |"
echo "<a href="/cgi-bin/sysinfo/ps.cgi">Running Pocesses</a> |"
echo "<a href="/cgi-bin/sysinfo/pstree.cgi">Process Tree</a> |"
echo "<a href="">Refresh</a> "
echo ""
echo ""
echo ""
echo "<p><b>Hostname: </b>$(hostname -s)</p>"
echo "<p><b>OS: </b>$(cat /etc/issue)</p>"
echo "<p><b>Uptime: </b>$(uptime)</p>"
 
echo "<h1>CPU Info</h1>"
echo "<pre> $(cat /proc/cpuinfo) </pre>"
 
echo "<center><b>Running: </b> $(cat /proc/version)</center><p></p>"
echo "<center>Information generated on $(date)</center>"
echo "</body></html>"

Source for ps.cgi

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#!/bin/bash
#:Title   :sysinfo ->Running Processes
#:Date    :8/15/2010
#:Author  :Obi Akubue
#:Version :0.1
#description: Displays General System Information on a webpage, works on linux/unix/bsd
 
echo "Content-type: text/html"
echo ""
echo "<html><head><title>CPU Info"
echo "</title></head><body>"
 
echo "<h1>General system information</h1>"
echo ""
 
echo "<a href="/cgi-bin/sysinfo/index.cgi">Home</a> |"
echo "<a href="/cgi-bin/sysinfo/cpu-info.cgi">CPU Info</a> |"
echo "Running Pocesses |"
echo "<a href="/cgi-bin/sysinfo/pstree.cgi">Process Tree</a> |"
echo "<a href="">Refresh</a> "
echo ""
echo ""
echo ""
echo "<p><b>Hostname: </b>$(hostname -s)</p>"
echo "<p><b>OS: </b>$(cat /etc/issue)</p>"
echo "<p><b>Uptime: </b>$(uptime)</p>"
 
echo "<h1>Running Processes</h1>"
echo "<pre> $( ps -eo start,pid,user,args --sort user) </pre>"
 
echo "<center><b>Running: </b> $(cat /proc/version)</center><p></p>"
echo "<center>Information generated on $(date)</center>"
echo "</body></html>"

Source for pstree.cgi

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#!/bin/bash
#:Title   :sysinfo ->Process Tree
#:Date    :8/15/2010
#:Author  :Obi Akubue
#:Version :0.1
#description: Displays General System Information on a webpage, works on linux/unix/bsd
 
echo "Content-type: text/html"
echo ""
echo "<html><head><title>CPU Info"
echo "</title></head><body>"
 
echo "<h1>General system information</h1>"
echo ""
 
echo "<a href="/cgi-bin/sysinfo/index.cgi">Home</a> |"
echo "<a href="/cgi-bin/sysinfo/cpu-info.cgi">CPU Info</a> |"
echo "<a href="/cgi-bin/sysinfo/ps.cgi">Running Pocesses</a> |"
echo "Process Tree |"
echo "<a href="">Refresh</a> "
echo ""
echo ""
echo ""
echo "<p><b>Hostname: </b>$(hostname -s)</p>"
echo "<p><b>OS: </b>$(cat /etc/issue)</p>"
echo "<p><b>Uptime: </b>$(uptime)</p>"
 
echo "<h1>Running Processes</h1>"
echo "<pre> $(pstree) </pre>"
 
echo "<center><b>Running: </b> $(cat /proc/version)</center><p></p>"
echo "<center>Information generated on $(date)</center>"
echo "</body></html>"
 
1 Comment

Posted in Linux

 

Installing Rails + Apache + Passenger on Ubuntu 10.04 with Speedtrain

08 Aug

[Update 3/7/2011] Check the updated version of the speedtrain script for Ruby 1.9.2 and Rails 3 here: http://obi-akubue.org/?p=922 and https://github.com/obi-a/speedtrain. This version doesn’t install Apache, Passenger yet.

I have written a new version of the Speedtrain script. This version now includes an option to install Rails with Apache and Phusion Passenger ready for production. By default the script will install Ruby, Rubygems, Rails, Sqlite and a Sample app. But by using the advanced option, the script will also install apache and phusion passenger with directions to deploy the Sample app on passenger.

Usage:
Step 1. Download the script

wget http://whisperservers.com/speedtrain

Step 2. Make the script executable

chmod +x speedtrain

Step 3. Option 1
For a basic rails setup for development, which install Rails (Ruby,Rubygems) and Sqlite. simply run the script

./speedtrain

Option 2. For a production type Rails setup, which install Rails (Ruby,Rubygems), Sqlite, Apache and Phusion Passenger run the script with a -p switch.

./speedtrain -p

After the installation the script will create a folder in your $HOME directory called rails_apps. This is the folder where you can create and store your rails apps. The folder is at $HOME/rails_apps. The script will also create a sample Rails app at $HOME/rails_apps/test_app. You can run the sample app by typing

cd $HOME/rails_apps/testapp
ruby script/server

Then open your browser and type http://localhost:3000/

If you used the option to install Apache and Passenger, you will be guided interactively through the Apache + Passenger installation. After the installation, follow the simple steps displayed on the screen to deploy Rails apps on passenger. Very easy steps:)

This script is ready to run on most Ubuntu 10.04 installations. Works great and easy on ubuntu as host OS on a machine. Using virtual box Apache gets some permission issues but should be easy to fix if one digs deeper. It works perfectly on Amazon EC2 Ubuntu 10.04 instances.

To install Rails with Passenger on an EC2 instance with this script you can use this Ubuntu AMI image# ami-2d4aa444 or choose one from here

Using these AMIs the username is literally “ubuntu” as in
ssh -i yourkeyname.pem ubuntu@ec2-123-45-67-89.compute-1.amazonaws.com. I found this in the comments after some digging.

Have fun.

The source code for the new speedtrain script.

#!/bin/bash
#:Title   :Speedtrain on Rails
#:Date    :
#:Author  :Obi Akubue
#:Version :0.1
#:Description: This script installs Ruby on Rails on Ubuntu 10.04 with the option to install Apache and Phusion Passenger. (It creates a test rails app)
 
#Usage Option 1: To Install Rails (Ruby + Rubygems) + sqlite. Simply run the script by typing ./speedtrain
#Usage Option 2: To Install Rails (Ruby + Rubygems) + Sqlite + Apache + Passenger. Run script by typing ./speedtrain -p
 
red='\e[0;31m'
RED='\e[1;31m'
blue='\e[0;34m'
BLUE='\e[1;34m' #light blue
cyan='\e[0;36m'
CYAN='\e[1;36m'
GREEN='\e[1;32m'
YELLOW='\e[1;33m'
WHITE='\e[1;37m'
NC='\e[0m' # No Color
 
#begining  messages
 if [ "$1" = "-p" ]; then
    #option to install Apache + Passenger
        echo  -e "${GREEN} Welcome to Speedtrain on Rails"
        echo " Installing Rails (Ruby + Rubygems) + Apache + sqlite + passenger ..."
        echo " This will take a few minutes to complete.."
        echo -e "${WHITE} Press ENTER to continue or Ctrl-C to Quit"
 
        echo -e "${NC}"
 
    else
        echo  -e "${GREEN} Welcome to Speedtrain on Rails"
        echo " Installing Rails..."
        echo " This will take a few minutes to complete.."
        echo -e "${WHITE} Press ENTER to continue or Ctrl-C to Quit"
        echo -e "${NC}"
   fi
 
 read key
 
echo -e "${RED} updating system..."
echo -e "${CYAN}"
sudo aptitude update
 
echo -e "${RED} Installing build essential..."
echo -e "${CYAN}"
sudo apt-get install build-essential
 
echo -e "${RED} installing ruby..."
echo -e "${GREEN}"
sudo apt-get install irb libopenssl-ruby libreadline-ruby rdoc ri ruby ruby-dev
 
echo -e "${RED} installing rubygems..."
echo -e "${CYAN}"
cd /usr/local/src
sudo wget http://production.cf.rubygems.org/rubygems/rubygems-1.3.6.tgz
sudo tar xzvf rubygems-1.3.6.tgz
cd rubygems-1.3.6
sudo ruby setup.rb
 
echo -e "${RED} updating rubygems..."
echo -e "${GREEN}"
sudo update-alternatives --install /usr/bin/gem gem /usr/bin/gem1.8 1
sudo gem update --system
 
echo -e "${RED} installing rails..."
echo -e "${CYAN}"
sudo gem install rails
 
echo -e "${RED} installing sqlite..."
echo -e "${GREEN}"
sudo apt-get install libsqlite3-dev sqlite3 sqlite3-doc
sudo gem install sqlite3-ruby
 
echo -e "${RED} creating dev directory..."
echo -e "${CYAN}"
mkdir ~/rails_apps/
 
cd ~/rails_apps/
echo -e "${RED} creating dev test app..."
echo -e "${GREEN}"
rails testapp
 
if [ "$1" = "-p" ]; then
#if the passenger switch is set
echo -e "${RED} installing Apache..."
echo -e "${CYAN}"
sudo apt-get install apache2
 
echo -e "${RED} installing passenger..."
echo -e "${GREEN}"
sudo gem install passenger
 
#install apache development headers
#required by passenger
#Apache 2 development headers --
sudo apt-get install apache2-prefork-dev
 
#Apache portable Runime (APR) development headers --
sudo apt-get install libapr1-dev
#install passenger
sudo passenger-install-apache2-module
fi
 
#ending message
if [ "$1" = "-p" ]; then
#if the passenger switch is set
echo -e "${YELLOW}"
printf "Your Apache Configuration file is /etc/apache2/httpd.conf\n"
echo -e "${NC}"
fi
 
echo -e "${CYAN}"
echo "Rails has been successfully installed"
echo -e "${GREEN}"
echo "Your Apps directory is $HOME/rails_apps"
echo "We created a Test Rails App for you at $HOME/rails_apps/testapp/"
echo "To run the Test App type"
echo -e "${YELLOW} cd $HOME/rails_apps/testapp"
echo " ruby script/server"
echo -e "${NC}"
 
if [ "$1" = "-p" ]; then
#if the passenger switch is set
echo -e "${GREEN}"
echo "To deploy the Test Rails app on passenger "
echo "Edit the Apache Configuration file and add the following virtualhost"
echo -e "${YELLOW}\n"
 
echo ""
echo "  ServerName www.yourhost.com"
echo "  DocumentRoot $HOME/rails_apps/testapp/public"
echo " "
echo "     AllowOverride all"
echo "     Options -MultiViews"
echo " "
echo ""
echo -e "${NC}"
echo -e "${GREEN}"
echo "then Restart Apache: sudo /etc/init.d/apache2 restart"
echo "and Open on your browser: http://yourdormain.com or http://your-ip.com"
echo -e "${NC}"
fi
 
 
 
Premium Wordpress Plugin