特殊:Badtitle/NS100:RubyOnRails:修订间差异

来自Ubuntu中文
跳到导航跳到搜索
Wikibot留言 | 贡献
无编辑摘要
Wikibot留言 | 贡献
无编辑摘要
第1行: 第1行:
{{From|https://help.ubuntu.com/community/RubyOnRails}}
{{From|https://help.ubuntu.com/community/RubyOnRails}}
{{Languages|UbuntuHelp:RubyOnRails}}
{{Languages|UbuntuHelp:RubyOnRails}}
== Notes ==
=== Intro ===
It would be swell if someone converted this to a mongrel / nginx guide since apache/fcgi is not the recommended way to go anymore.
These instructions are for Ubuntu 8.04 LTS (hardy).
http://www.urbanpuddle.com/articles/2007/11/16/install-ruby-rails-on-gutsy-gibbon-nginx-version
We use gems because of what ReinH put best...
== [[UbuntuHelp:RubyOnRails|RubyOnRails]] ==
Rails is a Model-View-Control web development framework implemented in Ruby for developing web database applications.
=== Getting started.  What is needed: ===
*  <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>Rails</nowiki></code> - With <code><nowiki>RubyGems</nowiki></code> 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.
<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
=== Installation ===
<pre><nowiki>sudo apt-get install ruby rdoc irb libyaml-ruby libzlib-ruby ri libopenssl-ruby ruby1.8-dev
<pre><nowiki>
sudo apt-get install ruby-full build-essential apache2 apache2-mpm-prefork apache2-prefork-dev
</nowiki></pre>
</nowiki></pre>
2) Install RubyGems via source (recommended):
''The link below is to a specific version of rubygemsIf you go to http://rubyforge.org/frs/?group_id=126 you'll get a list of all of the versions, most recent on top.
''
<WalterW> For me, the below installed the gem executable to /usr/bin/gem1.8'''I created a symbolic link so the rest of these commands will work: sudo ln -s /usr/bin/gem1.8 /usr/bin/gem'''
''
''
<pre><nowiki>wget http://rubyforge.org/frs/download.php/34638/rubygems-1.1.0.tgz
<pre><nowiki>wget http://rubyforge.org/frs/download.php/34638/rubygems-1.1.0.tgz
第27行: 第17行:
cd rubygems-1.1.0
cd rubygems-1.1.0
sudo ruby setup.rb
sudo ruby setup.rb
sudo ln -s /usr/bin/gem1.8 /usr/bin/gem
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):
<pre><nowiki>
<pre><nowiki>sudo apt-get install rubygems
sudo gem install rails
sudo gem update --system
sudo gem install passenger
</nowiki></pre>
sudo passenger-install-apache2-module
3) Install Rails and it's dependencies (with RubyGems)
<pre><nowiki>sudo gem install rails
</nowiki></pre>
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>):
<pre><nowiki>$ rails /path/to/new/railsapp
</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.
=== 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><nowiki>sudo apt-get install ruby rdoc irb libyaml-ruby libzlib-ruby ri libopenssl-ruby
</nowiki></pre>
2) Install RubyGems
<pre><nowiki>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
</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).
Add these lines to /etc/apache2/apache2.conf:
'' Will someone please help with the previous? My ~/.profile is a bash script, and has no path statements (that I can see). ''
(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><nowiki>~/bin/gem install rails -y
</nowiki></pre>
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>):
<pre><nowiki>$ ~/lib/ruby/gems/1.8/bin/rails /path/to/new/railsapp
</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.
=== Mongrel ===
Mongrel is the preferred way. Unfortunately, this section of the wiki has not been created thoroughly. Information about http://nginx.net/  should also be added here.
To install [http://mongrel.rubyforge.org/ mongrel], you can do the following:
<pre><nowiki>sudo apt-get install build-essential
sudo apt-get install ruby1.8-dev
sudo gem install mongrel
</nowiki></pre>
If you would like to install the mysql gem,
<pre><nowiki>sudo apt-get install libmysqlclient15-dev
</nowiki></pre>
then run
<pre><nowiki>sudo gem install mysql
</nowiki></pre>
=== Advanced Apache + FCGI setup (not recommended) ===
Mongrel is the recommended way to setup a RoR server. It is was the whole RoR community uses.
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 [[UbuntuHelp:RubyOnRails|RubyOnRails]] or search the web if you haven't done this already. You also need to have Apache up and running. See [[UbuntuHelp:ApacheMySQLPHP|ApacheMySQLPHP]]. apt-get install apache2 mysql-server  would be a good thing to do here.
Enable mod_rewrite:
<pre><nowiki>sudo a2enmod rewrite
</nowiki></pre>
Install FCGI for Apache and Ruby:
<pre><nowiki>sudo apt-get install libapache2-mod-fcgid
</nowiki></pre>
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><nowiki>
<pre><nowiki>
<IfModule mod_fcgid.c>
LoadModule passenger_module /usr/lib/ruby/gems/1.8/gems/passenger-1.0.3/ext/apache2/mod_passenger.so
  AddHandler fcgid-script .fcgi
RailsSpawnServer /usr/lib/ruby/gems/1.8/gems/passenger-1.0.3/bin/passenger-spawn-server
  SocketPath /var/lib/apache2/fcgid/sock
RailsRuby /usr/bin/ruby1.8
  SharememPath /var/lib/apache2/fcgid/shm
  IdleTimeout 600
  ProcessLifeTime 3600
  MaxProcessCount 8
  IPCConnectTimeout 8
  IPCCommTimeout 48
</IfModule>
</nowiki></pre>
</nowiki></pre>
If you didnt add that, you probably wouldnt notice it until you were processing substantial amounts of data.
Create something like this in /etc/apache2/sites-available/ror.myhost.com
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><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
sudo gem install fcgi
</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.
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><nowiki>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
</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.
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><nowiki>sudo nano /etc/apache2/sites-available/<servername>
</nowiki></pre>
Contents of the new file should be:<br>
<pre><nowiki>
<pre><nowiki>
<VirtualHost *>
<VirtualHost *>
        SetEnv RAILS_ENV development
    ServerName ror.myhost.com
        ServerName www.mysite.com
    DocumentRoot /home/myuser/www/mynewapp/public
        DocumentRoot /home/myuser/www/myrailsproject/public
</VirtualHost>
        <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 ^([^.]+)$ $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></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.
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><nowiki>sudo a2ensite <servername>
</nowiki></pre>
</nowiki></pre>
Afterwards:
<pre><nowiki>
<pre><nowiki>sudo /etc/init.d/apache2 restart
sudo a2enmod rewrite
sudo a2ensite ror.myhost.com
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.
maybe also do..
==== Troubleshooting ====
<pre><nowiki>
Click the home page's "About your application's environment" button. Refer to output and errors below.
rails mynewapp
===== 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>".
===== 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>".
===== 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><nowiki>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
</nowiki></pre>
</nowiki></pre>
==== 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><nowiki>
    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 ^([^.]+)$ $1.html [QSA]
        RewriteCond %{REQUEST_FILENAME} !-f
        RewriteRule ^(.*)$ dispatch.fcgi [QSA,L]
        ErrorDocument 500 /500.html
    </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`).
* <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><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.
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.
----
[[category:CategoryDocumentation]]


[[category:UbuntuHelp]]
[[category:UbuntuHelp]]

2008年5月9日 (五) 19:19的版本

{{#ifexist: :RubyOnRails/zh | | {{#ifexist: RubyOnRails/zh | | {{#ifeq: {{#titleparts:RubyOnRails|1|-1|}} | zh | | }} }} }} {{#ifeq: {{#titleparts:RubyOnRails|1|-1|}} | zh | | }}

Intro

These instructions are for Ubuntu 8.04 LTS (hardy). We use gems because of what ReinH put best... <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. ;

Installation

sudo apt-get install ruby-full build-essential apache2 apache2-mpm-prefork apache2-prefork-dev

The link below is to a specific version of rubygems. If you go to http://rubyforge.org/frs/?group_id=126 you'll get a list of all of the versions, most recent on top.

wget http://rubyforge.org/frs/download.php/34638/rubygems-1.1.0.tgz
tar xzvf rubygems-1.1.0.tgz
cd rubygems-1.1.0
sudo ruby setup.rb
sudo ln -s /usr/bin/gem1.8 /usr/bin/gem
sudo gem update --system
sudo gem install rails
sudo gem install passenger
sudo passenger-install-apache2-module

Add these lines to /etc/apache2/apache2.conf:

LoadModule passenger_module /usr/lib/ruby/gems/1.8/gems/passenger-1.0.3/ext/apache2/mod_passenger.so
RailsSpawnServer /usr/lib/ruby/gems/1.8/gems/passenger-1.0.3/bin/passenger-spawn-server
RailsRuby /usr/bin/ruby1.8

Create something like this in /etc/apache2/sites-available/ror.myhost.com

<VirtualHost *>
    ServerName ror.myhost.com
    DocumentRoot /home/myuser/www/mynewapp/public
</VirtualHost>

sudo a2enmod rewrite
sudo a2ensite ror.myhost.com
sudo /etc/init.d/apache2 restart

maybe also do..

rails mynewapp