个人工具

“UbuntuHelp:RubyOnRails”的版本间的差异

来自Ubuntu中文

跳转至: 导航, 搜索
第2行: 第2行:
 
{{Languages|UbuntuHelp:RubyOnRails}}
 
{{Languages|UbuntuHelp:RubyOnRails}}
 
== RubyOnRails ==
 
== RubyOnRails ==
 
 
Rails is a Model-View-Control web development framework implemented in Ruby for developing web database applications.
 
Rails is a Model-View-Control web development framework implemented in Ruby for developing web database applications.
 
 
=== Getting started.  What is needed: ===
 
=== Getting started.  What is needed: ===
 
 
*  <code><nowiki>Ruby</nowiki></code> - Ruby 1.8.5 is recommended version for use with Rails.
 
*  <code><nowiki>Ruby</nowiki></code> - Ruby 1.8.5 is recommended version for use with Rails.
 
 
*  <code><nowiki>RubyGems</nowiki></code> - RubyGems is the standard Ruby package manager.  Similar to apt-get, but is used for installing Ruby libraries and applications. Gems are sorta like .debs (Preferred way of installing Rails and it's dependencies)
 
*  <code><nowiki>RubyGems</nowiki></code> - RubyGems is the standard Ruby package manager.  Similar to apt-get, but is used for installing Ruby libraries and applications. Gems are sorta like .debs (Preferred way of installing Rails and it's dependencies)
 
 
*  <code><nowiki>Rails</nowiki></code> - With <code><nowiki>RubyGems</nowiki></code> loaded, you can install all of Rails and its dependencies.
 
*  <code><nowiki>Rails</nowiki></code> - With <code><nowiki>RubyGems</nowiki></code> loaded, you can install all of Rails and its dependencies.
 
 
 
 
=== Installation ===
 
=== Installation ===
 
 
Note:  Ensure that you have the Universe repository enabled in your /etc/sources.list. See https://wiki.ubuntu.com/AddingRepositoriesHowto
 
Note:  Ensure that you have the Universe repository enabled in your /etc/sources.list. See https://wiki.ubuntu.com/AddingRepositoriesHowto
 
 
In Ubuntu 6.06LTS or newer, you can install the "rails" package then skip to step 4. But this is also known to cause some annoying issues with ubuntu's package management (apt-get) and the rails gem manager.  
 
In Ubuntu 6.06LTS or newer, you can install the "rails" package then skip to step 4. But this is also known to cause some annoying issues with ubuntu's package management (apt-get) and the rails gem manager.  
 
 
''
 
''
 
<ReinH> Let gems manage your rails package otherwise gem and apt-get will start fighting like Spock and Captain Kirk in Amock Time. If you haven't seen that episode, trust me: you don't want that.
 
<ReinH> Let gems manage your rails package otherwise gem and apt-get will start fighting like Spock and Captain Kirk in Amock Time. If you haven't seen that episode, trust me: you don't want that.
 
'';
 
'';
 
 
1) Install Ruby
 
1) Install Ruby
 
 
<pre><nowiki>sudo apt-get install ruby rdoc irb libyaml-ruby libzlib-ruby ri
 
<pre><nowiki>sudo apt-get install ruby rdoc irb libyaml-ruby libzlib-ruby ri
 
</nowiki></pre>
 
</nowiki></pre>
 
 
2) Install RubyGems via source (recommended):
 
2) Install RubyGems via source (recommended):
 
 
<pre><nowiki>wget http://rubyforge.org/frs/download.php/17190/rubygems-0.9.2.tgz
 
<pre><nowiki>wget http://rubyforge.org/frs/download.php/17190/rubygems-0.9.2.tgz
 
tar xzvf rubygems-0.9.2.tgz
 
tar xzvf rubygems-0.9.2.tgz
第38行: 第23行:
 
sudo gem update --system
 
sudo gem update --system
 
</nowiki></pre>
 
</nowiki></pre>
 
 
2)(alternate) Install RubyGems from ubuntu repository and update it (not recommended because apt-get will complain about files changed by gem update --system):
 
2)(alternate) Install RubyGems from ubuntu repository and update it (not recommended because apt-get will complain about files changed by gem update --system):
 
 
<pre><nowiki>sudo apt-get install rubygems
 
<pre><nowiki>sudo apt-get install rubygems
 
sudo gem update --system
 
sudo gem update --system
 
</nowiki></pre>
 
</nowiki></pre>
 
 
3) Install Rails and it's dependencies (with RubyGems)
 
3) Install Rails and it's dependencies (with RubyGems)
 
 
<pre><nowiki>sudo gem install rails -y
 
<pre><nowiki>sudo gem install rails -y
 
</nowiki></pre>
 
</nowiki></pre>
 
 
Here is a good time to install any gems you may need.
 
Here is a good time to install any gems you may need.
 
 
4) Create your first Rails app, as the current user (i.e., no <code><nowiki>sudo</nowiki></code>):
 
4) Create your first Rails app, as the current user (i.e., no <code><nowiki>sudo</nowiki></code>):
 
<pre><nowiki>$ rails /path/to/new/railsapp
 
<pre><nowiki>$ rails /path/to/new/railsapp
 
</nowiki></pre>
 
</nowiki></pre>
 
Of course, replace <code><nowiki>/path/to/new/railsapp</nowiki></code> with the path to the location where you'd like the source code for your new Rails application to exist. This can be /home/myhome/rails/myapp.
 
Of course, replace <code><nowiki>/path/to/new/railsapp</nowiki></code> with the path to the location where you'd like the source code for your new Rails application to exist. This can be /home/myhome/rails/myapp.
 
 
=== Install through rubygems as normal user (non root) ===
 
=== Install through rubygems as normal user (non root) ===
 
 
It is possible to install rubygems in your home directory, which doesn't require root rights and is possibly safer (doesn't install or modify files outside your home directory).
 
It is possible to install rubygems in your home directory, which doesn't require root rights and is possibly safer (doesn't install or modify files outside your home directory).
 
 
1) Install Ruby
 
1) Install Ruby
 
 
<pre><nowiki>sudo apt-get install ruby rdoc irb libyaml-ruby libzlib-ruby ri
 
<pre><nowiki>sudo apt-get install ruby rdoc irb libyaml-ruby libzlib-ruby ri
 
</nowiki></pre>
 
</nowiki></pre>
 
 
2) Install RubyGems
 
2) Install RubyGems
 
 
<pre><nowiki>wget http://rubyforge.org/frs/download.php/17190/rubygems-0.9.2.tgz
 
<pre><nowiki>wget http://rubyforge.org/frs/download.php/17190/rubygems-0.9.2.tgz
 
tar xzvf rubygems-0.9.2.tgz
 
tar xzvf rubygems-0.9.2.tgz
第76行: 第49行:
 
ruby setup.rb all --prefix=$PREFIX
 
ruby setup.rb all --prefix=$PREFIX
 
</nowiki></pre>
 
</nowiki></pre>
 
 
You should then add GEM_HOME and RUBYLIB into your ~/.profile file to automatically load on login (otherwise some scripts will complain of not finding rubygems).
 
You should then add GEM_HOME and RUBYLIB into your ~/.profile file to automatically load on login (otherwise some scripts will complain of not finding rubygems).
 
 
(instructions based on [http://docs.rubygems.org/read/chapter/15#page101 rubygems FAQ] with a fix for the missing "local").
 
(instructions based on [http://docs.rubygems.org/read/chapter/15#page101 rubygems FAQ] with a fix for the missing "local").
 
 
3) Install Rails and it's dependencies (with RubyGems)
 
3) Install Rails and it's dependencies (with RubyGems)
 
 
<pre><nowiki>~/bin/gem install rails -y
 
<pre><nowiki>~/bin/gem install rails -y
 
</nowiki></pre>
 
</nowiki></pre>
 
 
Here is a good time to install any gems you may need.
 
Here is a good time to install any gems you may need.
 
 
4) Create your first Rails app, as the current user (i.e., no <code><nowiki>sudo</nowiki></code>):
 
4) Create your first Rails app, as the current user (i.e., no <code><nowiki>sudo</nowiki></code>):
 
<pre><nowiki>$ ~/lib/ruby/gems/1.8/bin/rails /path/to/new/railsapp
 
<pre><nowiki>$ ~/lib/ruby/gems/1.8/bin/rails /path/to/new/railsapp
 
</nowiki></pre>
 
</nowiki></pre>
 
Of course, replace <code><nowiki>/path/to/new/railsapp</nowiki></code> with the path to the location where you'd like the source code for your new Rails application to exist. This can be /home/myhome/rails/myapp.
 
Of course, replace <code><nowiki>/path/to/new/railsapp</nowiki></code> with the path to the location where you'd like the source code for your new Rails application to exist. This can be /home/myhome/rails/myapp.
 
 
=== Other ===
 
=== Other ===
 
To install [http://mongrel.rubyforge.org/ mongrel], you can do the following:
 
To install [http://mongrel.rubyforge.org/ mongrel], you can do the following:
第99行: 第65行:
 
sudo gem install mongrel
 
sudo gem install mongrel
 
</nowiki></pre>
 
</nowiki></pre>
 
 
 
If you would like to install the mysql gem,
 
If you would like to install the mysql gem,
 
<pre><nowiki>sudo apt-get install libmysqlclient15-dev
 
<pre><nowiki>sudo apt-get install libmysqlclient15-dev
第107行: 第71行:
 
<pre><nowiki>sudo gem install mysql
 
<pre><nowiki>sudo gem install mysql
 
</nowiki></pre>
 
</nowiki></pre>
 
 
=== Advanced Apache + FCGI setup ===
 
=== Advanced Apache + FCGI setup ===
 
It is easiest just to test ruby on rails with a the built in webrick or mongrel http server. But for those who want to integrate ruby on rails into your apache configuration, here are the instructions.
 
It is easiest just to test ruby on rails with a the built in webrick or mongrel http server. But for those who want to integrate ruby on rails into your apache configuration, here are the instructions.
 
 
First, you must have Ruby and Rails installed, and your application should be working under WEBrick (<code><nowiki><appdir>/script/server</nowiki></code>) server. This means you'll have all the necessary db-libraries etc. Read RubyOnRails or search the web if you haven't done this already. You also need to have Apache up and running. See [[UbuntuHelp:ApacheMySQLPHP]]. apt-get install apache2 mysql-server  would be a good thing to do here.
 
First, you must have Ruby and Rails installed, and your application should be working under WEBrick (<code><nowiki><appdir>/script/server</nowiki></code>) server. This means you'll have all the necessary db-libraries etc. Read RubyOnRails or search the web if you haven't done this already. You also need to have Apache up and running. See [[UbuntuHelp:ApacheMySQLPHP]]. apt-get install apache2 mysql-server  would be a good thing to do here.
 
 
Enable mod_rewrite:
 
Enable mod_rewrite:
 
<pre><nowiki>sudo a2enmod rewrite
 
<pre><nowiki>sudo a2enmod rewrite
 
</nowiki></pre>
 
</nowiki></pre>
 
 
Install FCGI for Apache and Ruby:
 
Install FCGI for Apache and Ruby:
 
<pre><nowiki>sudo apt-get install libapache2-mod-fcgid  
 
<pre><nowiki>sudo apt-get install libapache2-mod-fcgid  
 
</nowiki></pre>
 
</nowiki></pre>
 
 
After install mod_fcgid should be enabled, so you dont need to do  sudo a2enmod fcgid
 
After install mod_fcgid should be enabled, so you dont need to do  sudo a2enmod fcgid
 
 
fcgid has a nasty default timeout value which is too low for rails. So edit..  
 
fcgid has a nasty default timeout value which is too low for rails. So edit..  
 
 
/etc/apache2/mods-available/fcgid.conf
 
/etc/apache2/mods-available/fcgid.conf
 
 
<pre><nowiki>
 
<pre><nowiki>
 
<IfModule mod_fcgid.c>
 
<IfModule mod_fcgid.c>
第139行: 第95行:
 
</IfModule>
 
</IfModule>
 
</nowiki></pre>
 
</nowiki></pre>
 
 
If you didnt add that, you probably wouldnt notice it until you were processing substantial amounts of data.
 
If you didnt add that, you probably wouldnt notice it until you were processing substantial amounts of data.
 
That should take care of that. I found that fix here: http://brian.pontarelli.com/2006/08/06/fcgid-timeout-causing-rails-500-errors/
 
That should take care of that. I found that fix here: http://brian.pontarelli.com/2006/08/06/fcgid-timeout-causing-rails-500-errors/
 
 
Moving on.
 
Moving on.
 
 
You can directly install the <code><nowiki>libfcgi-ruby1.8</nowiki></code> package with apt-get, but the way that works best is to install it using ruby gems as follows.
 
You can directly install the <code><nowiki>libfcgi-ruby1.8</nowiki></code> package with apt-get, but the way that works best is to install it using ruby gems as follows.
 
 
<pre><nowiki>sudo apt-get install build-essential ruby1.8-dev libfcgi-dev
 
<pre><nowiki>sudo apt-get install build-essential ruby1.8-dev libfcgi-dev
 
sudo gem install fcgi
 
sudo gem install fcgi
 
</nowiki></pre>
 
</nowiki></pre>
 
 
I didnt have to do this, but others have said you may have to modify the dispatch.fcgi file. That must have been for an older version, but maybe would be helpful for troubleshooting.  
 
I didnt have to do this, but others have said you may have to modify the dispatch.fcgi file. That must have been for an older version, but maybe would be helpful for troubleshooting.  
 
 
Although it might not be necessary, you should check that the server has read and write access to the tmp and log directories in your Rails directory.  Also, Ruby files and and CGI files must be executable.
 
Although it might not be necessary, you should check that the server has read and write access to the tmp and log directories in your Rails directory.  Also, Ruby files and and CGI files must be executable.
 
 
Set file permissions:
 
Set file permissions:
 
<pre><nowiki>chgrp www-data -R /path/to/rails/app/log
 
<pre><nowiki>chgrp www-data -R /path/to/rails/app/log
第163行: 第112行:
 
find /path/to/rails/app -name *.*cgi | xargs chmod 755
 
find /path/to/rails/app -name *.*cgi | xargs chmod 755
 
</nowiki></pre>
 
</nowiki></pre>
 
 
You can skip this too. You should check that the first line of this file points to a valid ruby interpreter. <code><nowiki>#!/usr/bin/ruby</nowiki></code> or <code><nowiki>#!/usr/bin/ruby1.8</nowiki></code> should work. The former is just a symlink installed by <code><nowiki>ruby</nowiki></code> package which depends on the <code><nowiki>ruby1.8</nowiki></code> package. If you have both 1.6 and 1.8 installed and you are switching between them (using alternatives or just rewriting the link yourself), make sure Rails is using the 1.8 version.
 
You can skip this too. You should check that the first line of this file points to a valid ruby interpreter. <code><nowiki>#!/usr/bin/ruby</nowiki></code> or <code><nowiki>#!/usr/bin/ruby1.8</nowiki></code> should work. The former is just a symlink installed by <code><nowiki>ruby</nowiki></code> package which depends on the <code><nowiki>ruby1.8</nowiki></code> package. If you have both 1.6 and 1.8 installed and you are switching between them (using alternatives or just rewriting the link yourself), make sure Rails is using the 1.8 version.
 
 
Next, you should decide whether you would rather have the application running in root of new virtual server or integrated into structure of your already running webserver.
 
Next, you should decide whether you would rather have the application running in root of new virtual server or integrated into structure of your already running webserver.
 
 
==== Virtual Server Scenario ====
 
==== Virtual Server Scenario ====
 
 
Create and edit new file:
 
Create and edit new file:
 
<pre><nowiki>sudo nano /etc/apache2/sites-available/<servername>
 
<pre><nowiki>sudo nano /etc/apache2/sites-available/<servername>
 
</nowiki></pre>
 
</nowiki></pre>
 
 
Contents of the new file should be:<br>
 
Contents of the new file should be:<br>
 
<pre><nowiki>
 
<pre><nowiki>
第198行: 第142行:
 
LogLevel warn
 
LogLevel warn
 
</VirtualHost></nowiki></pre>
 
</VirtualHost></nowiki></pre>
 
 
Of course replace those values with your own website, error log name, and local path to your rails project. Make sure to link to the 'public' directory.
 
Of course replace those values with your own website, error log name, and local path to your rails project. Make sure to link to the 'public' directory.
 
 
So `/var/www-rails/app` would be `/var/www-rails/app/public`.
 
So `/var/www-rails/app` would be `/var/www-rails/app/public`.
 
 
For the error and access logs, feel free to make a subdir for them, or place them elsewhere.
 
For the error and access logs, feel free to make a subdir for them, or place them elsewhere.
 
 
As everything is set, you don't need the `.htaccess` file in your app's `public` dir anymore. But I didnt have to do anything with them, and just left them alone.
 
As everything is set, you don't need the `.htaccess` file in your app's `public` dir anymore. But I didnt have to do anything with them, and just left them alone.
 
 
If this is just a local setup, and you don't plan to have an DNS entry for your new virtual server just yet, you can add a new entry (ie. <servername>) to the line beginning with `127.0.0.1` in your `/etc/hosts` file (separate it from the rest of records either with space or tab).
 
If this is just a local setup, and you don't plan to have an DNS entry for your new virtual server just yet, you can add a new entry (ie. <servername>) to the line beginning with `127.0.0.1` in your `/etc/hosts` file (separate it from the rest of records either with space or tab).
 
 
Now lets enable the site:
 
Now lets enable the site:
 
<pre><nowiki>sudo a2ensite <servername>
 
<pre><nowiki>sudo a2ensite <servername>
 
</nowiki></pre>
 
</nowiki></pre>
 
 
Afterwards:
 
Afterwards:
 
<pre><nowiki>sudo /etc/init.d/apache2 restart
 
<pre><nowiki>sudo /etc/init.d/apache2 restart
 
</nowiki></pre>
 
</nowiki></pre>
 
 
Everything should work just fine. If you just added the <servername> into the `/etc/hosts` file, don't forget that this is just local change, and you have to make it on every other computer that will use the application (`/etc/hosts` on linux/unix and `<windows_dir>\system32\drivers\etc\hosts` on windows). On other computers the name of the virtual server must be prepended with your servers real IP address, not 127.0.0.1 though.
 
Everything should work just fine. If you just added the <servername> into the `/etc/hosts` file, don't forget that this is just local change, and you have to make it on every other computer that will use the application (`/etc/hosts` on linux/unix and `<windows_dir>\system32\drivers\etc\hosts` on windows). On other computers the name of the virtual server must be prepended with your servers real IP address, not 127.0.0.1 though.
 
 
==== Troubleshooting ====
 
==== Troubleshooting ====
 
Click the home page's "About your application's environment" button. Refer to output and errors below.
 
Click the home page's "About your application's environment" button. Refer to output and errors below.
 
 
===== Lots of text, beginning with <code><nowiki>#!/usr/bin/ruby1.8</nowiki></code>... =====
 
===== Lots of text, beginning with <code><nowiki>#!/usr/bin/ruby1.8</nowiki></code>... =====
 
The CGI/FCGI handler isn't configured properly. If the <code><nowiki>VirtualHost</nowiki></code> configuration in Apache has "<code><nowiki>AllowOverride none</nowiki></code>" in it, then the .htaccess handler rules cannot take effect. Either move the rules to the <code><nowiki>VirtualHost</nowiki></code> directory configuration or remove "<code><nowiki>AllowOverride none</nowiki></code>".
 
The CGI/FCGI handler isn't configured properly. If the <code><nowiki>VirtualHost</nowiki></code> configuration in Apache has "<code><nowiki>AllowOverride none</nowiki></code>" in it, then the .htaccess handler rules cannot take effect. Either move the rules to the <code><nowiki>VirtualHost</nowiki></code> directory configuration or remove "<code><nowiki>AllowOverride none</nowiki></code>".
 
 
===== 404 =====
 
===== 404 =====
 
Rewrite rules are not taking effect. If the <code><nowiki>VirtualHost</nowiki></code> configuration in Apache has "<code><nowiki>AllowOverride none</nowiki></code>" in it, then the .htaccess rewrite rules cannot take effect. Either move the rules to the <code><nowiki>VirtualHost</nowiki></code> directory configuration or remove "<code><nowiki>AllowOverride none</nowiki></code>".
 
Rewrite rules are not taking effect. If the <code><nowiki>VirtualHost</nowiki></code> configuration in Apache has "<code><nowiki>AllowOverride none</nowiki></code>" in it, then the .htaccess rewrite rules cannot take effect. Either move the rules to the <code><nowiki>VirtualHost</nowiki></code> directory configuration or remove "<code><nowiki>AllowOverride none</nowiki></code>".
 
 
===== Application Error =====
 
===== Application Error =====
 
Apache doesn't have the necessary permissions to run the application.  The log and tmp directories must be writable by the server.  The best way to do this is to give ownership of these two directories to the apache user (i.e. www-data).  The permissions should allow read/write by the user and read-only for everyone else (i.e. 744)
 
Apache doesn't have the necessary permissions to run the application.  The log and tmp directories must be writable by the server.  The best way to do this is to give ownership of these two directories to the apache user (i.e. www-data).  The permissions should allow read/write by the user and read-only for everyone else (i.e. 744)
 
 
<pre><nowiki>chgrp www-data -R /path/to/rails/app/log
 
<pre><nowiki>chgrp www-data -R /path/to/rails/app/log
 
chgrp www-data -R /path/to/rails/app/tmp
 
chgrp www-data -R /path/to/rails/app/tmp
第236行: 第167行:
 
chmod 744 -R /path/to/rails/app/log
 
chmod 744 -R /path/to/rails/app/log
 
</nowiki></pre>
 
</nowiki></pre>
 
 
 
==== Aliased directory under server's webroot ====
 
==== Aliased directory under server's webroot ====
 
 
Modify the existing site file under `/etc/apache2/sites-available/`. Insert following just before the `</VirtualHost>` tag in the file :<br>
 
Modify the existing site file under `/etc/apache2/sites-available/`. Insert following just before the `</VirtualHost>` tag in the file :<br>
 
<pre><nowiki>
 
<pre><nowiki>
第260行: 第188行:
 
* <errorlog> and <accesslog> are names of the apache error and access logs, and they typically resides in `/var/log/apache2/` directory. Feel free to make a subdir for them, or place them elsewhere (change the path respectively).
 
* <errorlog> and <accesslog> are names of the apache error and access logs, and they typically resides in `/var/log/apache2/` directory. Feel free to make a subdir for them, or place them elsewhere (change the path respectively).
 
* Remember to include rule of "<code><nowiki>SetEnv RAILS_ENV development</nowiki></code>" in the <code><nowiki>VirtualHost</nowiki></code> setting
 
* Remember to include rule of "<code><nowiki>SetEnv RAILS_ENV development</nowiki></code>" in the <code><nowiki>VirtualHost</nowiki></code> setting
 
 
Because everything is set, you don't need the `.htaccess` file in your app's `public` dir anymore.
 
Because everything is set, you don't need the `.htaccess` file in your app's `public` dir anymore.
 
 
Don't forget that your applications expects directories (namely <code><nowiki>images</nowiki></code>,<code><nowiki>stylesheets</nowiki></code> etc.} found in <path_to_your_applications_public_dir> in the webservers root, so either move them or change the application accordingly.
 
Don't forget that your applications expects directories (namely <code><nowiki>images</nowiki></code>,<code><nowiki>stylesheets</nowiki></code> etc.} found in <path_to_your_applications_public_dir> in the webservers root, so either move them or change the application accordingly.
 
 
No new site needs to be enabled, no new hosts/dns entrys are needed so just do (as root): <code><nowiki>/etc/init.d/apache2 restart</nowiki></code> and try if your application works.
 
No new site needs to be enabled, no new hosts/dns entrys are needed so just do (as root): <code><nowiki>/etc/init.d/apache2 restart</nowiki></code> and try if your application works.
 
 
 
----
 
----
 
[[category:CategoryDocumentation]]
 
[[category:CategoryDocumentation]]
  
 
[[category:UbuntuHelp]]
 
[[category:UbuntuHelp]]

2007年11月30日 (五) 21:22的版本

RubyOnRails

Rails is a Model-View-Control web development framework implemented in Ruby for developing web database applications.

Getting started. What is needed:

  • Ruby - Ruby 1.8.5 is recommended version for use with Rails.
  • RubyGems - RubyGems is the standard Ruby package manager. Similar to apt-get, but is used for installing Ruby libraries and applications. Gems are sorta like .debs (Preferred way of installing Rails and it's dependencies)
  • Rails - With RubyGems loaded, you can install all of Rails and its dependencies.

Installation

Note: Ensure that you have the Universe repository enabled in your /etc/sources.list. See https://wiki.ubuntu.com/AddingRepositoriesHowto In Ubuntu 6.06LTS or newer, you can install the "rails" package then skip to step 4. But this is also known to cause some annoying issues with ubuntu's package management (apt-get) and the rails gem manager. <ReinH> Let gems manage your rails package otherwise gem and apt-get will start fighting like Spock and Captain Kirk in Amock Time. If you haven't seen that episode, trust me: you don't want that. ; 1) Install Ruby

sudo apt-get install ruby rdoc irb libyaml-ruby libzlib-ruby ri

2) Install RubyGems via source (recommended):

wget http://rubyforge.org/frs/download.php/17190/rubygems-0.9.2.tgz
tar xzvf rubygems-0.9.2.tgz
cd rubygems-0.9.2
sudo ruby setup.rb
sudo gem update --system

2)(alternate) Install RubyGems from ubuntu repository and update it (not recommended because apt-get will complain about files changed by gem update --system):

sudo apt-get install rubygems
sudo gem update --system

3) Install Rails and it's dependencies (with RubyGems)

sudo gem install rails -y

Here is a good time to install any gems you may need. 4) Create your first Rails app, as the current user (i.e., no sudo):

$ rails /path/to/new/railsapp

Of course, replace /path/to/new/railsapp with the path to the location where you'd like the source code for your new Rails application to exist. This can be /home/myhome/rails/myapp.

Install through rubygems as normal user (non root)

It is possible to install rubygems in your home directory, which doesn't require root rights and is possibly safer (doesn't install or modify files outside your home directory). 1) Install Ruby

sudo apt-get install ruby rdoc irb libyaml-ruby libzlib-ruby ri

2) Install RubyGems

wget http://rubyforge.org/frs/download.php/17190/rubygems-0.9.2.tgz
tar xzvf rubygems-0.9.2.tgz
cd rubygems-0.9.2
PREFIX=$HOME
export GEM_HOME=$PREFIX/lib/ruby/gems/1.8 
export RUBYLIB=$PREFIX/local/lib/ruby:$PREFIX/local/lib/site_ruby/1.8
ruby setup.rb all --prefix=$PREFIX

You should then add GEM_HOME and RUBYLIB into your ~/.profile file to automatically load on login (otherwise some scripts will complain of not finding rubygems). (instructions based on rubygems FAQ with a fix for the missing "local"). 3) Install Rails and it's dependencies (with RubyGems)

~/bin/gem install rails -y

Here is a good time to install any gems you may need. 4) Create your first Rails app, as the current user (i.e., no sudo):

$ ~/lib/ruby/gems/1.8/bin/rails /path/to/new/railsapp

Of course, replace /path/to/new/railsapp with the path to the location where you'd like the source code for your new Rails application to exist. This can be /home/myhome/rails/myapp.

Other

To install mongrel, you can do the following:

sudo apt-get install build-essential
sudo apt-get install ruby1.8-dev
sudo gem install mongrel

If you would like to install the mysql gem,

sudo apt-get install libmysqlclient15-dev

then run

sudo gem install mysql

Advanced Apache + FCGI setup

It is easiest just to test ruby on rails with a the built in webrick or mongrel http server. But for those who want to integrate ruby on rails into your apache configuration, here are the instructions. First, you must have Ruby and Rails installed, and your application should be working under WEBrick (<appdir>/script/server) server. This means you'll have all the necessary db-libraries etc. Read RubyOnRails or search the web if you haven't done this already. You also need to have Apache up and running. See UbuntuHelp:ApacheMySQLPHP. apt-get install apache2 mysql-server would be a good thing to do here. Enable mod_rewrite:

sudo a2enmod rewrite

Install FCGI for Apache and Ruby:

sudo apt-get install libapache2-mod-fcgid 

After install mod_fcgid should be enabled, so you dont need to do sudo a2enmod fcgid fcgid has a nasty default timeout value which is too low for rails. So edit.. /etc/apache2/mods-available/fcgid.conf

<IfModule mod_fcgid.c>
AddHandler fcgid-script .fcgi
SocketPath /var/lib/apache2/fcgid/sock
SharememPath /var/lib/apache2/fcgid/shm
IdleTimeout 600
ProcessLifeTime 3600
MaxProcessCount 8
IPCConnectTimeout 8
IPCCommTimeout 48
</IfModule>

If you didnt add that, you probably wouldnt notice it until you were processing substantial amounts of data. That should take care of that. I found that fix here: http://brian.pontarelli.com/2006/08/06/fcgid-timeout-causing-rails-500-errors/ Moving on. You can directly install the libfcgi-ruby1.8 package with apt-get, but the way that works best is to install it using ruby gems as follows.

sudo apt-get install build-essential ruby1.8-dev libfcgi-dev
sudo gem install fcgi

I didnt have to do this, but others have said you may have to modify the dispatch.fcgi file. That must have been for an older version, but maybe would be helpful for troubleshooting. Although it might not be necessary, you should check that the server has read and write access to the tmp and log directories in your Rails directory. Also, Ruby files and and CGI files must be executable. Set file permissions:

chgrp www-data -R /path/to/rails/app/log
chgrp www-data -R /path/to/rails/app/tmp
chmod 744 -R /path/to/rails/app/tmp
chmod 744 -R /path/to/rails/app/log
find /path/to/rails/app -name *.rb | xargs chmod 755
find /path/to/rails/app -name *.*cgi | xargs chmod 755

You can skip this too. You should check that the first line of this file points to a valid ruby interpreter. #!/usr/bin/ruby or #!/usr/bin/ruby1.8 should work. The former is just a symlink installed by ruby package which depends on the ruby1.8 package. If you have both 1.6 and 1.8 installed and you are switching between them (using alternatives or just rewriting the link yourself), make sure Rails is using the 1.8 version. Next, you should decide whether you would rather have the application running in root of new virtual server or integrated into structure of your already running webserver.

Virtual Server Scenario

Create and edit new file:

sudo nano /etc/apache2/sites-available/<servername>

Contents of the new file should be:

<VirtualHost *>
SetEnv RAILS_ENV development
ServerName www.mysite.com
DocumentRoot /home/myuser/www/myrailsproject/public
<Directory /home/myuser/www/myrailsproject/public/>
Options ExecCGI FollowSymLinks
AddHandler fcgid-script .fcgi
Order allow,deny
Allow from all
RewriteEngine On
RewriteRule ^$ index.html [QSA]
RewriteRule <sup>([</sup>.]+)$ $1.html [QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /dispatch.fcgi?$1 [QSA,L]
AllowOverride None
</Directory>
ServerSignature On
#       ErrorDocument 500 /500.html
ErrorLog /var/log/apache2/www.mysite.com.error.log
CustomLog /var/log/apache2/www.mysite.com.access.log combined
LogLevel warn
</VirtualHost>

Of course replace those values with your own website, error log name, and local path to your rails project. Make sure to link to the 'public' directory. So `/var/www-rails/app` would be `/var/www-rails/app/public`. For the error and access logs, feel free to make a subdir for them, or place them elsewhere. As everything is set, you don't need the `.htaccess` file in your app's `public` dir anymore. But I didnt have to do anything with them, and just left them alone. If this is just a local setup, and you don't plan to have an DNS entry for your new virtual server just yet, you can add a new entry (ie. <servername>) to the line beginning with `127.0.0.1` in your `/etc/hosts` file (separate it from the rest of records either with space or tab). Now lets enable the site:

sudo a2ensite <servername>

Afterwards:

sudo /etc/init.d/apache2 restart

Everything should work just fine. If you just added the <servername> into the `/etc/hosts` file, don't forget that this is just local change, and you have to make it on every other computer that will use the application (`/etc/hosts` on linux/unix and `<windows_dir>\system32\drivers\etc\hosts` on windows). On other computers the name of the virtual server must be prepended with your servers real IP address, not 127.0.0.1 though.

Troubleshooting

Click the home page's "About your application's environment" button. Refer to output and errors below.

Lots of text, beginning with #!/usr/bin/ruby1.8...

The CGI/FCGI handler isn't configured properly. If the VirtualHost configuration in Apache has "AllowOverride none" in it, then the .htaccess handler rules cannot take effect. Either move the rules to the VirtualHost directory configuration or remove "AllowOverride none".

404

Rewrite rules are not taking effect. If the VirtualHost configuration in Apache has "AllowOverride none" in it, then the .htaccess rewrite rules cannot take effect. Either move the rules to the VirtualHost directory configuration or remove "AllowOverride none".

Application Error

Apache doesn't have the necessary permissions to run the application. The log and tmp directories must be writable by the server. The best way to do this is to give ownership of these two directories to the apache user (i.e. www-data). The permissions should allow read/write by the user and read-only for everyone else (i.e. 744)

chgrp www-data -R /path/to/rails/app/log
chgrp www-data -R /path/to/rails/app/tmp
chmod 744 -R /path/to/rails/app/tmp
chmod 744 -R /path/to/rails/app/log

Aliased directory under server's webroot

Modify the existing site file under `/etc/apache2/sites-available/`. Insert following just before the `</VirtualHost>` tag in the file :

Alias /<alias> "<path_to_your_applications_public_dir>"
<Directory <path_to_your_applications_public_dir>>
Options +FollowSymlinks +ExecCGI
Order allow,deny
Allow from all
AddHandler fcgid-script .fcgi
RewriteEngine On
RewriteBase /<alias>
RewriteRule ^$ index.html [QSA]
RewriteRule <sup>([</sup>.]+)$ $1.html [QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ dispatch.fcgi [QSA,L]
ErrorDocument 500 /500.html
</Directory>
  • <path_to_your_applications_public_dir> means what it says (so if your application resides in `/var/www-rails/app`, it should be `/var/www-rails/app/public`).
  • <alias> is path relative to webroot where you want "place" the application (ie. `Alias /xxx/yyy` means the app will be available as `http://<yourwebserver>/xxx/yyy`).
  • <errorlog> and <accesslog> are names of the apache error and access logs, and they typically resides in `/var/log/apache2/` directory. Feel free to make a subdir for them, or place them elsewhere (change the path respectively).
  • Remember to include rule of "SetEnv RAILS_ENV development" in the VirtualHost setting

Because everything is set, you don't need the `.htaccess` file in your app's `public` dir anymore. Don't forget that your applications expects directories (namely images,stylesheets etc.} found in <path_to_your_applications_public_dir> in the webservers root, so either move them or change the application accordingly. No new site needs to be enabled, no new hosts/dns entrys are needed so just do (as root): /etc/init.d/apache2 restart and try if your application works.