个人工具

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

来自Ubuntu中文

跳转至: 导航, 搜索
(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 ...)
 
第28行: 第28行:
  
 
<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></code>
+
</nowiki></pre>
  
 
2) Install RubyGems
 
2) Install RubyGems
第37行: 第37行:
 
sudo ruby setup.rb
 
sudo ruby setup.rb
 
sudo gem update --system
 
sudo gem update --system
</nowiki></code>
+
</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></code>
+
</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.
第48行: 第48行:
 
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></code>
+
</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.
  
第58行: 第58行:
  
 
<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></code>
+
</nowiki></pre>
  
 
2) Install RubyGems
 
2) Install RubyGems
第69行: 第69行:
 
export RUBYLIB=$PREFIX/local/lib/ruby:$PREFIX/local/lib/site_ruby/1.8
 
export RUBYLIB=$PREFIX/local/lib/ruby:$PREFIX/local/lib/site_ruby/1.8
 
ruby setup.rb all --prefix=$PREFIX
 
ruby setup.rb all --prefix=$PREFIX
</nowiki></code>
+
</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).
第78行: 第78行:
  
 
<pre><nowiki>~/bin/gem install rails -y
 
<pre><nowiki>~/bin/gem install rails -y
</nowiki></code>
+
</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.
第84行: 第84行:
 
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></code>
+
</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.
  
第92行: 第92行:
 
sudo apt-get install ruby1.8-dev
 
sudo apt-get install ruby1.8-dev
 
sudo gem install mongrel
 
sudo gem install mongrel
</nowiki></code>
+
</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
</nowiki></code>
+
</nowiki></pre>
 
then run
 
then run
 
<pre><nowiki>sudo gem install mysql
 
<pre><nowiki>sudo gem install mysql
</nowiki></code>
+
</nowiki></pre>
  
 
=== Advanced Apache + FCGI setup ===
 
=== Advanced Apache + FCGI setup ===
第109行: 第109行:
 
Enable mod_rewrite:
 
Enable mod_rewrite:
 
<pre><nowiki>sudo a2enmod rewrite
 
<pre><nowiki>sudo a2enmod rewrite
</nowiki></code>
+
</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></code>
+
</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
第131行: 第131行:
 
   IPCCommTimeout 48
 
   IPCCommTimeout 48
 
</IfModule>
 
</IfModule>
</nowiki></code>
+
</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.
第142行: 第142行:
 
<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></code>
+
</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.  
第155行: 第155行:
 
find /path/to/rails/app -name *.rb | xargs chmod 755
 
find /path/to/rails/app -name *.rb | xargs chmod 755
 
find /path/to/rails/app -name *.*cgi | xargs chmod 755
 
find /path/to/rails/app -name *.*cgi | xargs chmod 755
</nowiki></code>
+
</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.
第165行: 第165行:
 
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></code>
+
</nowiki></pre>
  
 
Contents of the new file should be:<br>
 
Contents of the new file should be:<br>
第190行: 第190行:
 
         CustomLog /var/log/apache2/www.mysite.com.access.log combined
 
         CustomLog /var/log/apache2/www.mysite.com.access.log combined
 
         LogLevel warn
 
         LogLevel warn
</VirtualHost></nowiki></code>
+
</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.
第204行: 第204行:
 
Now lets enable the site:
 
Now lets enable the site:
 
<pre><nowiki>sudo a2ensite <servername>
 
<pre><nowiki>sudo a2ensite <servername>
</nowiki></code>
+
</nowiki></pre>
  
 
Afterwards:
 
Afterwards:
 
<pre><nowiki>sudo /etc/init.d/apache2 restart
 
<pre><nowiki>sudo /etc/init.d/apache2 restart
</nowiki></code>
+
</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.
第228行: 第228行:
 
chmod 744 -R /path/to/rails/app/tmp
 
chmod 744 -R /path/to/rails/app/tmp
 
chmod 744 -R /path/to/rails/app/log
 
chmod 744 -R /path/to/rails/app/log
</nowiki></code>
+
</nowiki></pre>
  
  
第248行: 第248行:
 
         RewriteRule ^(.*)$ dispatch.fcgi [QSA,L]
 
         RewriteRule ^(.*)$ dispatch.fcgi [QSA,L]
 
         ErrorDocument 500 /500.html
 
         ErrorDocument 500 /500.html
     </Directory></nowiki></code>
+
     </Directory></nowiki></pre>
 
* <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`).
 
* <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`).
 
* <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`).

2007年5月13日 (日) 12:33的版本

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

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

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



CategoryDocumentation