个人工具

UbuntuHelp:RubyOnRails

来自Ubuntu中文

Oneleaf讨论 | 贡献2007年5月13日 (日) 11:23的版本 (New page: {{From|https://help.ubuntu.com/community/RubyOnRails}} {{Languages|php5}} == RubyOnRails == Rails is a Model-View-Control web development framework implemented in Ruby for developing web ...)

(差异) ←上一版本 | 最后版本 (差异) | 下一版本→ (差异)
跳转至: 导航, 搜索

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
</code>

2) Install RubyGems

<pre>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
</code>

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

<pre>sudo gem install rails -y
</code>

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>sudo</code>):
<pre>$ rails /path/to/new/railsapp
</code>
Of course, replace <code>/path/to/new/railsapp</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) ===

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

<pre>sudo apt-get install ruby rdoc irb libyaml-ruby libzlib-ruby ri
</code>

2) Install RubyGems

<pre>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
</code>

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").

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

<pre>~/bin/gem install rails -y
</code>

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>sudo</code>):
<pre>$ ~/lib/ruby/gems/1.8/bin/rails /path/to/new/railsapp
</code>
Of course, replace <code>/path/to/new/railsapp</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 ===
To install [http://mongrel.rubyforge.org/ mongrel], you can do the following:
<pre>sudo apt-get install build-essential
sudo apt-get install ruby1.8-dev
sudo gem install mongrel
</code>


If you would like to install the mysql gem,
<pre>sudo apt-get install libmysqlclient15-dev
</code>
then run
<pre>sudo gem install mysql
</code>

=== 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 (<code><appdir>/script/server</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:
<pre>sudo a2enmod rewrite
</code>

Install FCGI for Apache and Ruby:
<pre>sudo apt-get install libapache2-mod-fcgid 
</code>

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

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

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 <code>libfcgi-ruby1.8</code> package with apt-get, but the way that works best is to install it using ruby gems as follows.

<pre>sudo apt-get install build-essential ruby1.8-dev libfcgi-dev
sudo gem install fcgi
</code>

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:
<pre>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
</code>

You can skip this too. You should check that the first line of this file points to a valid ruby interpreter. <code>#!/usr/bin/ruby</code> or <code>#!/usr/bin/ruby1.8</code> should work. The former is just a symlink installed by <code>ruby</code> package which depends on the <code>ruby1.8</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.

==== Virtual Server Scenario ====

Create and edit new file:
<pre>sudo nano /etc/apache2/sites-available/<servername>
</code>

Contents of the new file should be:<br>
<pre>
<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></code>

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:
<pre>sudo a2ensite <servername>
</code>

Afterwards:
<pre>sudo /etc/init.d/apache2 restart
</code>

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 <code>#!/usr/bin/ruby1.8</code>... =====
The CGI/FCGI handler isn't configured properly. If the <code>VirtualHost</code> configuration in Apache has "<code>AllowOverride none</code>" in it, then the .htaccess handler rules cannot take effect. Either move the rules to the <code>VirtualHost</code> directory configuration or remove "<code>AllowOverride none</code>".

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

===== 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)

<pre>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
</code>


==== 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>
<pre>
    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></code>
* <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 "<code>SetEnv RAILS_ENV development</code>" in the <code>VirtualHost</code> 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 <code>images</code>,<code>stylesheets</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>/etc/init.d/apache2 restart</code> and try if your application works.


----
CategoryDocumentation

[[category:UbuntuHelp]]