Setting Up a Ruby on Rails and MySQL Based Server

Write tutorials here
There are more tutorials here http://community.linuxmint.com/tutorial/welcome
Forum rules
Do not start a support topic here please. Before you post please read this

Setting Up a Ruby on Rails and MySQL Based Server

Postby neutrix on 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.

Ruby
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


RubyGems
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
wget http://rubyforge.org/frs/download.php/38646/rubygems-1.2.0.tgz
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


Rails
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
       </Directory>
</VirtualHost>


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 “Rails::Initializer.run 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! You can view your site with your servers address followed by /testapplication. For example, on my internal network it would be given by: http://192.168.0.5/testapplication .
neutrix
Level 1
Level 1
 
Posts: 4
Joined: Tue Jan 13, 2009 4:09 pm

Linux Mint is funded by ads and donations.
 

Re: Setting Up a Ruby on Rails and MySQL Based Server

Postby hex on Sun Apr 12, 2009 6:47 pm

Hey, very nice tutorial. Thanks!
Only one problem, I recieve
Code: Select all
Internal Server Error

The server encountered an internal error or misconfiguration and was unable to complete your request.

Please contact the server administrator, [no address given] and inform them of the time the error occurred, and anything you might have done that may have caused the error.

More information about this error may be available in the server error log.
Apache/2.2.9 (Ubuntu) Phusion_Passenger/2.1.3 Server at 192.168.1.103 Port 80

but if I delete the contents of the .htaccess file it works. what seems to be the problem? Thanks.
hex
Level 1
Level 1
 
Posts: 3
Joined: Sun Apr 12, 2009 6:33 pm

Re: Setting Up a Ruby on Rails and MySQL Based Server

Postby neutrix on Fri Apr 17, 2009 7:13 pm

Hi, glad you liked the tutorial.

At first glance I'm not sure what could have caused that problem, but if it works without a .htaccess file, then great, you may as well use that. Another potential problem is you might be using a different version of apache or rails, which might cause problems. If it auto-generates a .htaccess file then use the default one with a couple of changes. The important points of the .htaccess file is that it sets rails up to use the fcgid-script handler, and uses dispatch.fcgi rather than dispatch.cgi. The important lines to include are: "RewriteRule ^(.*)$ dispatch.fcgi [QSA,L]" in place of "RewriteRule ^(.*)$ dispatch.cgi [QSA,L]" and "AddHandler fcgid-script .fcgi" in place of "AddHandler cgi-script .cgi".

I hope this helps in some way.
neutrix
Level 1
Level 1
 
Posts: 4
Joined: Tue Jan 13, 2009 4:09 pm

Re: Setting Up a Ruby on Rails and MySQL Based Server

Postby ballpointpenguin on Thu Jun 04, 2009 5:25 am

I had the same problem as hex and also solved it by deleting the .htaccess file

Server version: Apache/2.2.9 (Ubuntu)
Rails 2.3.2
Seattle, WA
ballpointpenguin
Level 1
Level 1
 
Posts: 1
Joined: Thu Jun 04, 2009 5:21 am

Re: Setting Up a Ruby on Rails and MySQL Based Server

Postby OutOfTouch on Thu Sep 17, 2009 5:15 pm

Hi,

I am noob with linux and Ruby but not to programming, nice tutorial by the way.

If I already have MySQL installed prior to installing ruby can I just run the following to get the api for Ruby to talk to MySQL:
Code: Select all
sudo apt-get install  libmysql-ruby libmysqlclient-dev


Do I need the last part the libmysqlclient-dev ?

What is the difference between this command:
Sudo apt-get install ruby-full build-essential
and the one shown on the ruby site for install?
% sudo apt-get install ruby irb rdoc

Also on the ruby site is says that that irb and rdoc require the universal repository to be enabled.
As a total noob to linux I have no idea what that is or if it is enabled. <--- any help or links to explain this would be much appreciated.

Sorry for all the noob questions as I am coming from the MS programming world.

Also when I installed ruby I was at the root with this command: sudo su
Is that incorrect? If I did that incorrectly how do I un-install it at the root and install it at the correct level?

Thanks!
OutOfTouch
Level 1
Level 1
 
Posts: 8
Joined: Wed Sep 16, 2009 6:41 pm

Re: Setting Up a Ruby on Rails and MySQL Based Server

Postby OutOfTouch on Tue Sep 22, 2009 8:49 pm

I could really use some help answering some of newbie questions:
If I install ruby from source instead of the package is rdoc and irb installed? I am assuming it would be.

Do I need to rake to MySQL?

In the instructions detailed here they mention that the MySQL socket file is not in the default location:
http://wiki.rubyonrails.org/getting-sta ... tion/linux
Do I need to change the socket location? If yes to what? this? --> socket: '/var/run/mysqld/mysqld.sock'

Any and all help is appreciated.

Thanks!
OutOfTouch
Level 1
Level 1
 
Posts: 8
Joined: Wed Sep 16, 2009 6:41 pm

Re: Setting Up a Ruby on Rails and MySQL Based Server

Postby OutOfTouch on Fri Sep 25, 2009 3:04 am

Does anybody read these threads?
OutOfTouch
Level 1
Level 1
 
Posts: 8
Joined: Wed Sep 16, 2009 6:41 pm

Re: Setting Up a Ruby on Rails and MySQL Based Server

Postby firelinemx on Wed Oct 28, 2009 12:18 pm

here is another link that solve some newbies problems and errors:

http://www.elemenex.com/index.php?optio ... &Itemid=30
firelinemx
Level 1
Level 1
 
Posts: 1
Joined: Wed Oct 28, 2009 12:14 pm

Linux Mint is funded by ads and donations.
 

Return to Tutorials

Who is online

Users browsing this forum: No registered users and 10 guests