Setting Up a Ruby on Rails & MySQL Based Server

Write tutorials here
There are more tutorials here
Forum rules
Please don't add support questions to tutorials,start your own thread in the appropriate sub-forum instead. Before you post please read this
Level 1
Level 1
Posts: 5
Joined: Tue Jan 13, 2009 4:09 pm

Setting Up a Ruby on Rails & MySQL Based Server

Postby neutrix » Tue Jan 13, 2009 4:19 pm

This tutorial was tested on an Ubuntu Server 8.10 system, which at the time of writing I would highly recommend as a platform for running a web server on. It will take you through setting up the server and creating your first live rails application.

The first step is to install the ruby language.

Code: Select all

Sudo apt-get install ruby-full build-essential

Apache 2 web server
Now we install Apache2 and all of the relevant extensions for running rails. This is possible with the command:

Code: Select all

sudo apt-get install apache2 apache2-mpm-prefork apache2-prefork-dev apache2-utils libapache2-mod-fcgid

MySQL setup
Next we will install the mysql server and client. This is achieved using:

Code: Select all

sudo apt-get install mysql-server mysql-client libmysql-ruby libmysqlclient-dev

Now we may install rubygems from source. First we get the source tarball and then install with the included setup files as follows:

Code: Select all

tar xzvf rubygems-1.2.0.tgz
cd rubygems-1.2.0
sudo ruby setup.rb
sudo ln -s /usr/bin/gem1.8 /usr/bin/gem
sudo gem update --system

Now we may install rails using rubygems. In the terminal type:

Code: Select all

sudo gem install rails

Setting up Apache
Rails applications will reside in “/var/rails”, and will be linked to from “/var/www”, which is apache2's default web directory. Create the rails directory and give ownership to the apache user and allow members of the apache group access using:

Code: Select all

sudo mkdir /var/rails
sudo chown www-data:www-data /var/rails
sudo chmod 775 /var/rails

Now we will create a virtual host for our rails applications. This will setup the base directory for all of our rails applications as well as other settings required for the applications to work correctly. Edit “/etc/apache2/httpd.conf” to read:

Code: Select all

<VirtualHost *>
       ServerName servername
       DocumentRoot /var/www/
       ErrorLog /var/rails/apache.log

       <Directory /var/www>
               Options ExecCGI FollowSymLinks
               AddHandler fcgid-script .fcgi
               AllowOverride all
               Order allow,deny
               Allow from all

You may now restart apache for the changes to take effect with :

Code: Select all

sudo /etc/init.d/apache2 restart

Building a Test Site
We will now create and configure a test rails application to be used on our new server. At this point it is worth adding your default user to the www-data group or you will probably not have permissions to create and edit the files in the following sections. Now let us go and create our rails application:

Code: Select all

cd /var/rails
rails testapplication

Next we will create a symbolic link to our applications public directory using the following command:

Code: Select all

sudo ln -s /var/rails/testapplication/public /var/www/testapplication

Now we must configure the .htaccess file for our rails application. Go to your applications public directory, in this case “/var/rails/testapplication/public”. If there is a .htaccess file there already, then replace it with the following, or if there is not one there, just create one and fill it with the following:

Code: Select all

# General Apache options

#AddHandler fastcgi-script .fcgi
#AddHandler cgi-script .cgi
AddHandler fcgid-script .fcgi
Options +FollowSymLinks +ExecCGI

# If you don't want Rails to look in certain directories,
# use the following rewrite rules so that Apache won't rewrite certain requests

# Example:
#   RewriteCond %{REQUEST_URI} ^/notrails.*
#   RewriteRule .* - [L]

# Redirect all requests not available on the filesystem to Rails
# By default the cgi dispatcher is used which is very slow
# For better performance replace the dispatcher with the fastcgi one
# Example:
#   RewriteRule ^(.*)$ dispatch.fcgi [QSA,L]
RewriteEngine On

# If your Rails application is accessed via an Alias directive,
# then you MUST also set the RewriteBase in this htaccess file.
# Example:
#   Alias /myrailsapp /path/to/myrailsapp/public
#   RewriteBase /myrailsapp

RewriteRule ^$ index.html [QSA]
RewriteRule ^([^.]+)$ $1.html [QSA]
RewriteCond %{REQUEST_FILENAME} !-f
#RewriteRule ^(.*)$ dispatch.cgi [QSA,L]
RewriteRule ^(.*)$ dispatch.fcgi [QSA,L]

# In case Rails experiences terminal errors
# Instead of displaying this message you can supply a file here which will be rendered instead
# Example:
#   ErrorDocument 500 /500.html

ErrorDocument 500 "<h2>Application error</h2>Rails application failed to start properly"

Finally, open up the environment.rb file in the “/var/rails/testapplication/config” directory and add at the end of the “ do |config|
” block add:

Code: Select all

 config.action_controller.relative_url_root = '/testapplication'

This should be set to the name of the symlink that you created at the beginning of this section. It effectively offsets the root of the rails application to this subdirectory, and allows you to set up many different rails apps on one server in different directories.

You may now go ahead and create any controllers or models that you would like. Don't forget to set the /var/rails/testapplication/config/database.yml file to include the details for your rails app database. If you get the error “Rails requires RubyGems >= 1.3.1 (you have 1.2.0). Please `gem update --system` and try again.”, you can fix it by running:

Code: Select all

sudo gem install rubygems-update

Finally make sure that the whole application belongs to the apache user and restart the server:

Code: Select all

sudo chown -R www-data /var/rails/testapplication
sudo /etc/init.d/apache2 restart

Hopefully now you should be riding with rails!

Return to “Tutorials”

Who is online

Users browsing this forum: No registered users and 7 guests