Xen Virtualization: Work with MySQL Server, Ruby on Rails, and Subversion

Exclusive offer: get 50% off this eBook here
Xen Virtualization

Xen Virtualization — Save 50%

A fast and practical guide to supporting multiple operating systems with the Xen hypervisor

$23.99    $12.00
by Prabhakar Chaganti | October 2007 | Architecture & Analysis Linux Servers Open Source

In this article, Prabhakar Chaganti will show us to create virtual appliances that perform one specific function. These appliances can be web servers, database servers or anything else that you can think of which is useful for your business.

The key advantage with these appliances is the fact that they are mostly maintenance free, and can be started up and then used without going through any lengthy installation process. You can use these as needed for your testing and then dispose them off. You can also use them for staging environments or production environments, and you can even share them across the enterprise.

Base Appliance Image

We will use an Ubuntu Feisty domain image as the base image for creating these appliances. This image should be made as sparse and small as possible, and free of any cruft. A completely stripped down version of Linux with only the bare necessities would be a great start. In this case, we will not need any graphical desktop environments, so we can completely eliminate software packages like the X11 and any window manager like Gnome or KDE. Once we have a base image, we can back it up and then start using it for creating Xen appliances. In this article we will use an Ubuntu Feisty domain as the base image. Once this domain image is ready we are going to update it and clean it up a little bit so it can be our base.

  1. Edit the sources list for apt and add in other repositories that we will need to get software packages we will need when creating these appliances.
    Xen Virtualization: Work with MySQL Server, Ruby on Rails, and Subversion
  1. Update your list of software. This will connect to the apt repositories and get the latest list of packages. We will do the actual update in the next step.
    Xen Virtualization: Work with MySQL Server, Ruby on Rails, and Subversion

  1. Upgrade the distribution to ensure that you have the latest versions of all the packages.
    Xen Virtualization: Work with MySQL Server, Ruby on Rails, and Subversion

  1. Automatically clean the image so all unused packages are removed. This will ensure that the image stays free of cruft.
 Xen Virtualization: Work with MySQL Server, Ruby on Rails, and Subversion

Now we have the base appliance image ready, we will use it to create some Xen appliances. You can make a backup of the original base image and every time you create an appliance you can use a copy as the starting point or template. The images are nothing but domU images, which are customized for running only specific applications. You start them up and run them like ay other Xen guest domains.

MySQL Database Server

MySQL is one of the most popular open-source databases in the world. It is a key component of the LAMP architecture – (Linux Apache MySQL and PHP). It is also very easy to get started with MySQL and is one of the key factors driving its adoption across the enterprise. In this section we will create a Xen appliance that will run a MySQL database server and also provide the ability to automatically backup the database on a given schedule.

Time for Action – Create our first Xen appliance

We will use our base Ubuntu Feisty domain image, and add MySQL and other needed software to it. Please ensure that you have updated your base image to the latest versions of the repositories and software packages before creating this appliance.

  1. Install mysql-server using apt.
    Xen Virtualization: Work with MySQL Server, Ruby on Rails, and Subversion

  1. Once it is installed, Ubuntu will automatically start the database server. So before we make our other changes, stop MySQL.
    Xen Virtualization: Work with MySQL Server, Ruby on Rails, and Subversion
  1. Edit the /etc/mysql/my.cnf and comment out the line for the bind-address parameter. This will ensure that MySQL will accept connections from external machines and not just the localhost.
    Xen Virtualization: Work with MySQL Server, Ruby on Rails, and Subversion
  1. Start a mysql console session to test that everything is installed and working correctly.
    Xen Virtualization: Work with MySQL Server, Ruby on Rails, and Subversion

  1. Next we will install the utility for doing the automated backups. In order to do that we will first need to install the wget utility for transferring files. This is not a part of the base Ubuntu Feisty installation.
    Xen Virtualization: Work with MySQL Server, Ruby on Rails, and Subversion

  1. Download the automysqlbackup script from the website.
    Xen Virtualization: Work with MySQL Server, Ruby on Rails, and Subversion

  1. Copy this script to wherever you like, maybe /opt. Create a link to this location so it’s easy to do future updates.
    # cp automysqlbackup.sh.2.5 /opt
    # ln -s automysqlbackup.sh.2.5 automysqlbackup.sh
  1. Edit the script and modify the parameters at the top of the script to match your environment. Here are the changes to be made in our case.

    # Username to access the MySQL server e.g. dbuser
    USERNAME=pchaganti
    # Username to access the MySQL server e.g. password
    PASSWORD=password
    # Host name (or IP address) of MySQL server e.g localhost
    DBHOST=localhost
    # List of DBNAMES for Daily/Weekly Backup e.g. "DB1 DB2 DB3"
    DBNAMES="all"
    # Backup directory location e.g /backups
    BACKUPDIR="/var/backup/mysql"
    # Mail setup
    MAILCONTENT="quiet"

  1. Schedule this backup script to be run daily by creating a crontab entry for it, in the following format.
    45 5 * * * root  /opt/automysqlbackup.sh >/dev/null 2>&1

Now we have a MySQL database server with automatic daily backups as a nice reusable Xen appliance.

What just happened?

We created our first Xen appliance! It is running the open-source MySQL database server along with an automated backup of the database as per the given schedule. This image is essentially a domU image and it can be uploaded along with its configuration file to a repository somewhere, and can be used by anyone in the enterprise or elsewhere with their Xen server. You can either start up the domain manually as and when you need it or set it up to boot automatically when your xend server starts.

Ruby on Rails Appliance

Ruby on Rails is one of the hottest web development frameworks around. It is simple to use and you can use all the expressive power of the Ruby language. It provides a great feature set and has really put the Ruby language on the map. Ruby on Rails is gaining rapid adoption across the IT landscape and for a wide variety of web applications. In this section, we are going to create a Rails appliance that contains Ruby, Rails, and the Mongrel cluster for serving the Rails application and nginx web server for the static content. This appliance gives you a great starting point for your explorations into the world of Ruby on Rails and can be an excellent learning resource.

Time for Action – Rails on Xen

We will use our base Ubuntu Feisty domain image and add Rails and other needed software to it. Please ensure that you have updated your base image to the latest versions of the repositories and software packages before creating this appliance.

  1. Install the packages required for compiling software on an Ubuntu system. This is required as we will be compiling some native extensions. Once the image is done, you can always remove this package if you want to save space.
    Xen Virtualization: Work with MySQL Server, Ruby on Rails, and Subversion

  1. Install Ruby and other packages that are needed for it.
    Xen Virtualization: Work with MySQL Server, Ruby on Rails, and Subversion

  1. Download the RubyGems package from RubyForge. We will use this to install any Ruby libraries or packages that we will need, including Rails.
    Xen Virtualization: Work with MySQL Server, Ruby on Rails, and Subversion

  1. Now install Rails. The first time when you run this command on a clean Ubuntu Feisty system, you will get the following error. Ignore this error and just run the command once again and it will work fine.
    Xen Virtualization: Work with MySQL Server, Ruby on Rails, and Subversion

  1. This will install Rails and all of its dependencies.
    Xen Virtualization: Work with MySQL Server, Ruby on Rails, and Subversion

  1. Create a new Rails application. This will create everything needed in a directory named xenbook.

$ rails xenbook 
  1. Change into the directory of the application that we created in the previous step and start the server up. This will start Ruby’s built-in web server, webrick by default.
    Xen Virtualization: Work with MySQL Server, Ruby on Rails, and Subversion

  1. Launch a web browser and navigate to the web page for our xenbook application.
    Xen Virtualization: Work with MySQL Server, Ruby on Rails, and Subversion

We have everything working for a simple Rails install. However, we are using webrick, which is a bit slow. So let’s install the Mongrel server and use it with Rails. We will actually install mongrel_cluster that will let us use a cluster of Mongrel processes for serving up our Rails application.

Xen Virtualization: Work with MySQL Server, Ruby on Rails, and Subversion

Xen Virtualization A fast and practical guide to supporting multiple operating systems with the Xen hypervisor
Published: December 2007
eBook Price: $23.99
Book Price: $39.99
See more
Select your format and quantity:
  1. Startup the server again. You will see that this time Rails starts up using the Mongrel server and not webrick. Rails is smart enough to recognize that you have Mongrel installed on the machine and automatically starts using it.
    Xen Virtualization: Work with MySQL Server, Ruby on Rails, and Subversion
  1. We have everything in place for serving up dynamic Rails pages. However, it advised to use a web server such as Apache for serving up the static parts of Rails. We will use tiny and super fast web server called nginx for this purpose. Let’s first install nginx from the Ubuntu repositories using apt. Ubuntu will automatically startup nginx once it is installed, using a default configuration. We will be modifying this configuration to match our environment later on in this section.
    Xen Virtualization: Work with MySQL Server, Ruby on Rails, and Subversion

  1. Copy the mongrel_cluster service file to Ubuntu’s startup services directory.
    #/usr/lib/ruby/gems/1.8/gems/mongrel_cluster-1.0.2/resources/mongrel_
    cluster /etc/init.d/mongrel_cluster
  1. Edit the above file to change the user and group information, and to add the PATH. Save the file after you make the changes.

    PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/local:/usr/local/sbin:/usr/local/bin
    USER=www-data

  1. Change the permissions on it so it is executable. Also add mongrel_cluster to the Ubuntu startup services list so it starts up by default when the server starts up.
    Xen Virtualization: Work with MySQL Server, Ruby on Rails, and Subversion

  1. You can move the directory of the Rails application that we created earlier to the more standard location of /var/www. Change the permissions on the directory to be readable by the www-data user and group.
# mv ~pchaganti/xenbook /var/www
# chown -R www-data:www-data /var/www/xenbook
  1. Generate the configuration for a cluster of three Mongrels running on port 8000. The user and group in our case, needs to be www-data. If you change your mind about the user and group, you can always change it after the file has been generated by editing the file.
    Xen Virtualization: Work with MySQL Server, Ruby on Rails, and Subversion

  1. Create a symbolic link for the configuration file that we just generated in our xenbook application directory to the /etc location.
# mkdir /etc/mongrel_cluster
# cd /etc/mongrel_cluster
# ln –s /var/www/xenbook/config/mongrel_cluster.yml
  1. Start up the cluster of Mongrels.
    Xen Virtualization: Work with MySQL Server, Ruby on Rails, and Subversion

  1. Stop the nginx server.
    # /etc/init.d/nginx stop
  1. Edit the configuration for nginx to use the mongrel_cluster.

user www-data www-data;
worker_processes 3;

error_log /var/log/nginx/error.log;

pid /var/run/nginx.pid;

events {
worker_connections 1024;
}

http {
include /etc/nginx/mime.types;
default_type application/octet-stream;

log_format main '$remote_addr - $remote_user [$time_local] $status '
'"$request" $body_bytes_sent "$http_referer" '
'"$http_user_agent" "http_x_forwarded_for"';

sendfile on;
tcp_nopush on;
tcp_nodelay on;
gzip on;

upstream mongrel {
server localhost.packtpub.com:8000;
server localhost.packtpub.com:8001;
server localhost.packtpub.com:8002;
}

server {
listen 80;
root /var/www/xenbook/public;
index index.html index.htm;
server_name www.xenbook.net xenbook.net;
client_max_body_size 50M;

access_log /var/log/nginx/localhost.access.log;

location / {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded_for $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect false;
proxy_max_temp_file_size 0;

if (-f $request_filename) {
break;
}
if (-f $request_filename/index.html) {
rewrite (.*) $1/index.html break;
}
if (-f $request_filename.html) {
rewrite (.*) $1.html break;
}
if (!-f $request_filename) {
proxy_pass http://mongrel;
break;
}

}
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /500.html;
location = /500.html {
root /var/www/xenbook/public;
}
}
}
  1. Restart nginx to use this new configuration.
    # /etc/init.d/nginx start
  1. You can now see that everything is up and running by listing the processes on the system using the ps command. You will see a cluster of three Mongrels and three master nginx processes running.
    Xen Virtualization: Work with MySQL Server, Ruby on Rails, and Subversion

  1. Now you should be able to access your application on port 80 of your server.
    Xen Virtualization: Work with MySQL Server, Ruby on Rails, and Subversion

We have successfully setup our Rails application serving up the Rails pages using a cluster of Mongrels for dynamic data and nginx for super fast static web serving.

What just happened?

We installed everything needed to run a simple Rails application on our appliance image. We installed Ruby along with its development libraries. These libraries are needed when compiling Ruby extensions. We also installed the compilers and associated packages needed for doing the actual compilations on our image. We installed the RubyGems package. RubyGems are somewhat analogous to the jar files in the Java world and the egg packaging format used in the Python world. They are a simple way to package Ruby extensions and libraries into one downloadable archive. This archive contains metadata describing how to actually lay it down on a target system and how to compile any native extensions if needed. Most of the software libraries and applications in the Ruby world are distributed using this mechanism these days. We installed Rails, Mongrel and mongrel_cluster using their gems. Mongrel is the replacement web server for webrick which is tightly integrated with Rails and is able to serve up Rails application in a much more performant manner. The recommended and scalable way of using Mongrel is to cluster it and use a pack of running Mongrel processes for serving a Rails application. We then finally installed nginx. It is a super fast web server and reverse proxy. It has a very small footprint and is able to serve static items much faster than either Apache or lighttpd. It is fast becoming the favored way to deploy a Rails application with a cluster of Mongrels. We configured everything to work together and voila! We have a Ruby on Rails Xen appliance ready for serving up your Rails applications in a Xen environment!

Subversion Appliance

Subversion is an open-source revision control system that was designed to be a simple and efficient alternative to CVS. It is widely used in the open-source community and a lot of high profile projects such as KDE have switched over to it recently. In this section we are going to create a Xen appliance that contains Subversion and Apache. This appliance can be used by your development team or you can use it for playing with and experimenting with Subversion itself.

Time for Action – A revision control appliance

We will use our base Ubuntu Feisty domain image and add Subversion and other needed software to it. Please ensure that you have updated your base image to the latest versions of the repositories and software packages before creating this appliance.

  1. Install Subversion first using apt.
    Xen Virtualization: Work with MySQL Server, Ruby on Rails, and Subversion

  1. Install Apache2 if you don’t already have it installed.
    Xen Virtualization: Work with MySQL Server, Ruby on Rails, and Subversion

  1. Add basic authentication to the SVN server.
    Xen Virtualization: Work with MySQL Server, Ruby on Rails, and Subversion

  1. Create a directory for storing the Subversion repository. Create the actual repository itself and set its permissions to be writable by the www-data user and group.
    Xen Virtualization: Work with MySQL Server, Ruby on Rails, and Subversion

  1. Configure DAV and SVN by editing the file - /etc/apache2/mods-available/dav_svn.conf. Here are the contents of that file with all the comments removed to save space.

DAV svn

SVNPath /var/svn/repos

AuthType Basic

AuthName "Subversion Repository"

AuthUserFile /etc/apache2/dav_svn.passwd

Require valid-user


  1. Restart apache.
    # /etc/init.d/apache2 restart
  1. Import any file into the repository for testing to making sure your SVN installation is working properly.
  1. Try to access the SVN repository in your browser and you should be presented with a username and password prompt.
    Xen Virtualization: Work with MySQL Server, Ruby on Rails, and Subversion

  1. Put in your username and password and you should be able to access your spanking new SVN repository.
    Xen Virtualization: Work with MySQL Server, Ruby on Rails, and Subversion

What just happened?

We created a Xen appliance that provides a Subversion repository that is served by Apache2 using a basic authentication scheme. We strove for simplicity in this appliance and did not try to add and use all of the myriad things that are possible using SVN and Apache2. The idea is to show you how simple it is to get started with using Subversion. You can experiment with Subversion and add functionality that you need and extend this appliance as you go.

Summary

In this article, we explored the use of Xen appliances to provide targeted pieces of functionality in small ready to use packages. They are a simple way to share and reuse your domU images that are customized for providing a certain use. In this article, we created these appliances:

  • MySQL Database – An appliance that provides the populate MySQL server along with a way to automatically backup the database on a specified schedule.
  • Ruby on Rails appliance – An appliance that provides a way to host a Rails application that runs using a cluster of Mongrels fronted by nginx web server processes.
  • Subversion - An appliance that provides a Subversion repository served up by using Apache2.
Xen Virtualization A fast and practical guide to supporting multiple operating systems with the Xen hypervisor
Published: December 2007
eBook Price: $23.99
Book Price: $39.99
See more
Select your format and quantity:

About the Author :


Prabhakar Chaganti

Prabhakar Chaganti is the founder and CTO of Ylastic, a start-up that is building a single unified interface to architect, manage, and monitor a user's entire AWS Cloud computing environment: EC2, S3, RDS, AutoScaling, ELB, Cloudwatch, SQS, and SimpleDB. He is the author of Xen Virtualization and GWT Java AJAX Programming, and is also the winner of the community choice award for the most innovative virtual appliance in the VMware Global Virtual Appliance Challenge. He hangs out on Twitter as @pchaganti.

Read about his tips on time management...

Books From Packt

SOA Governance
SOA Governance

trixbox CE 2.6
trixbox CE 2.6

MODx Web Development
MODx Web Development

RESTful PHP Web Services
RESTful PHP Web Services

Oracle SOA Suite Developer's Guide
Oracle SOA Suite Developer's Guide

Choosing an Open Source CMS: Beginner's Guide
Choosing an Open Source CMS: Beginner's Guide

JBoss Portal Server Development
JBoss Portal Server Development

Asterisk Gateway Interface 1.4 and 1.6 Programming
Asterisk Gateway Interface 1.4 and 1.6 Programming

 


 

 

No votes yet

Post new comment

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
A
8
5
8
3
r
Enter the code without spaces and pay attention to upper/lower case.
Code Download and Errata
Packt Anytime, Anywhere
Register Books
Print Upgrades
eBook Downloads
Video Support
Contact Us
Awards Voting Nominations Previous Winners
Judges Open Source CMS Hall Of Fame CMS Most Promising Open Source Project Open Source E-Commerce Applications Open Source JavaScript Library Open Source Graphics Software
Resources
Open Source CMS Hall Of Fame CMS Most Promising Open Source Project Open Source E-Commerce Applications Open Source JavaScript Library Open Source Graphics Software