个人工具

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

来自Ubuntu中文

跳转至: 导航, 搜索
 
(未显示4个用户的48个中间版本)
第1行: 第1行:
 
{{From|https://help.ubuntu.com/community/RubyOnRails}}
 
{{From|https://help.ubuntu.com/community/RubyOnRails}}
 
{{Languages|UbuntuHelp:RubyOnRails}}
 
{{Languages|UbuntuHelp:RubyOnRails}}
== Notes ==
+
=== Alternative to manual installation ===
It would be swell if someone converted this to a mongrel / nginx guide since apache/fcgi is not the recommended way to go anymore.
+
Some users may prefer to skip manual installation by using the unofficial
http://www.urbanpuddle.com/articles/2007/11/16/install-ruby-rails-on-gutsy-gibbon-nginx-version
+
pre-integrated [http://www.turnkeylinux.org/rails TurnKey Ruby on Rails Appliance] based on Ubuntu LTS.
== [[UbuntuHelp:RubyOnRails|RubyOnRails]] ==
+
=== Intro Ubuntu 8.04 ===
Rails is a Model-View-Control web development framework implemented in Ruby for developing web database applications.
+
These instructions are for Ubuntu 8.04 LTS (hardy). Instead of using <code><nowiki>apt</nowiki></code> we use gems because of what ReinH put best...
=== 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 Amok Time. If you haven't seen that episode, trust me: you don't want that.
'';
+
1) Install Ruby
+
<pre><nowiki>sudo apt-get install ruby rdoc irb libyaml-ruby libzlib-ruby ri libopenssl-ruby ruby1.8-dev
+
</nowiki></pre>
+
2) Install RubyGems via source (recommended):
+
 
''
 
''
<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'''
+
=== Intro Ubuntu 9.04 ... Apache2.2 tested only ===
''
+
Slight additions from 8.04 documentation...
<pre><nowiki>wget http://rubyforge.org/frs/download.php/34638/rubygems-1.1.0.tgz
+
* Using Passenger ( mod_rails ) may be required, it was for me. see http://www.modrails.com/documentation/Users%20guide.html Do NOT try a manual installation, use gems only.
 +
* Do the minimum Ruby installation from Debian packages ( apt-get | Aptitude | Synaptic ). First install Ruby and Apache and rubygems as described below using Debian packages, then use gems only for the other Ruby-based applications.
 +
* Use an Apache  vhost configuration that puts the myapp/public directory at the document root.  see  http://httpd.apache.org/docs/2.2/vhosts/name-based.html
 +
* Using an .htaccess file in the myapp/public directory may be required.
 +
* Using fastcgi or fcgid Apache modules is not required, and may not work at all.
 +
* The Rails group is another good place to ask for assistance... http://groups.google.com/group/rubyonrails-talk
 +
* The Rails guide is excellent...  http://guides.rubyonrails.org/index.html
 +
=== Get Ruby ===
 +
<pre><nowiki>
 +
sudo apt-get install ruby-full build-essential
 +
</nowiki></pre>
 +
Note that `ruby-full` will install emacs and a slew of other non-essential packages. You can install all the necessary packages with this command:
 +
<pre><nowiki>
 +
sudo aptitude install ruby build-essential libopenssl-ruby ruby1.8-dev
 +
</nowiki></pre>
 +
=== Server Installation ===
 +
Now you have ruby, you already got a server included, WEBrick. You may settle with that, also because the installation is much easier, and reboots are faster. But it lacks the configuration options that apache and lighttpd offer. So if you want to have RubyOnRails running on apache or lighttpd, make sure you have installed these first.
 +
==== Apache ====
 +
<pre><nowiki>
 +
sudo apt-get install apache2 apache2-mpm-prefork apache2-prefork-dev
 +
</nowiki></pre>
 +
WARNING: libapache2-mod-passenger installation uninstalls libapache2-mod-php5 and php5.
 +
==== Apache + Mongrel ====
 +
Mongrel Cluster works as a proxy service under Apache (no additional modification is required).
 +
<pre><nowiki>
 +
sudo a2enmod proxy
 +
sudo a2enmod proxy_http
 +
sudo a2enmod rewrite
 +
sudo /etc/init.d/apache2 restart
 +
sudo apt-get install mongrel
 +
</nowiki></pre>
 +
[http://wiki.rubyonrails.org/deployment/apache-mongrel Rails Wiki - Apache + Mongrel]
 +
[http://mongrel.rubyforge.org/wiki/Apache RubyForge Mongrel - Apache Best Practice Deployment]
 +
==== Lighttpd ====
 +
Preparing the house:
 +
<pre><nowiki>
 +
 
 +
sudo apt-get install lighttpd libfcgi-ruby1.8
 +
 
 +
</nowiki></pre>
 +
=== Installing RubyGems ===
 +
The link below is to a specific version of RubyGems (version 1.1).  You can get a later version from http://rubyforge.org/frs/?group_id=126, but it should not be really necessary because we ask RubyGems to update itself at the end. But to make it more complete, a description of RubyGems 1.2 is also included, below the install instructions of RubyGems 1.2.
 +
==== RubyGems via apt ====
 +
<pre><nowiki>
 +
 
 +
sudo apt-get install rubygems
 +
 
 +
</nowiki></pre>
 +
''As of July 27, 2009, RubyGems 1.3.1 is installed via apt.''
 +
To make gem work properly you should write gem's path to PATH add to ~/.bashrc:
 +
<pre><nowiki>
 +
export PATH=/var/lib/gems/1.8/bin:$PATH
 +
</nowiki></pre>
 +
To export path once just enter previous code to bash.
 +
==== RubyGems 1.1.0 ====
 +
<pre><nowiki>
 +
wget http://rubyforge.org/frs/download.php/34638/rubygems-1.1.0.tgz
 
tar xzvf rubygems-1.1.0.tgz
 
tar xzvf rubygems-1.1.0.tgz
 
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):
+
==== RubyGems 1.2.0 ====
<pre><nowiki>sudo apt-get install rubygems
+
<pre><nowiki>
 +
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
 
sudo gem update --system
 
</nowiki></pre>
 
</nowiki></pre>
3) Install Rails and it's dependencies (with RubyGems)
+
The below error is sometimes encountered after running sudo gem update --system.
<pre><nowiki>sudo gem install rails
+
<pre><nowiki>
 +
/usr/bin/gem:23: uninitialized constant Gem::GemRunner (NameError)
 
</nowiki></pre>
 
</nowiki></pre>
Here is a good time to install any gems you may need.
+
To fix that error follow the below instructions.
4) Create your first Rails app, as the current user (i.e., no <code><nowiki>sudo</nowiki></code>):
+
<pre><nowiki>
<pre><nowiki>$ rails /path/to/new/railsapp
+
sudo gedit /usr/bin/gem
 +
 
 +
In the file find:
 +
require 'rubygems'
 +
 
 +
Add the following line below the above line:
 +
require 'rubygems/gem_runner'
 
</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.
+
==== RubyGems 1.3.1 Ubuntu 8.04 ====
=== Install through rubygems as normal user (non root) ===
+
<pre><nowiki>
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).
+
wget http://rubyforge.org/frs/download.php/45905/rubygems-1.3.1.tgz
1) Install Ruby
+
tar xzvf rubygems-1.3.1.tgz
<pre><nowiki>sudo apt-get install ruby rdoc irb libyaml-ruby libzlib-ruby ri libopenssl-ruby
+
cd rubygems-1.3.1
 +
sudo ruby setup.rb
 +
sudo ln -s /usr/bin/gem1.8 /usr/bin/gem
 +
sudo gem update --system
 
</nowiki></pre>
 
</nowiki></pre>
2) Install RubyGems
+
==== RubyGems 1.3.1 Ubuntu 9.04 ====
<pre><nowiki>wget http://rubyforge.org/frs/download.php/17190/rubygems-0.9.2.tgz
+
<pre><nowiki>
tar xzvf rubygems-0.9.2.tgz
+
sudo apt-get install rubygems1.8
cd rubygems-0.9.2
+
sudo gem install rubygems-update
PREFIX=$HOME
+
sudo update_rubygems    note: this will clean out your gems!
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).
+
=== Installing rails ===
'' Will someone please help with the previous? My ~/.profile is a bash script, and has no path statements (that I can see). ''
+
We can now use RubyGems to install rails:
(instructions based on [http://docs.rubygems.org/read/chapter/15#page101 rubygems FAQ] with a fix for the missing "local").
+
<pre><nowiki>
3) Install Rails and it's dependencies (with RubyGems)
+
 
<pre><nowiki>~/bin/gem install rails -y
+
sudo gem install rails
 +
 
 
</nowiki></pre>
 
</nowiki></pre>
Here is a good time to install any gems you may need.
+
Also we can install rails from apt ( not suggested for Ubuntu 9.04 )
4) Create your first Rails app, as the current user (i.e., no <code><nowiki>sudo</nowiki></code>):
+
<pre><nowiki>
<pre><nowiki>$ ~/lib/ruby/gems/1.8/bin/rails /path/to/new/railsapp
+
 
 +
sudo apt-get install rails
 +
 
 
</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.
+
I think, it would be better to install rails with gem.  
=== Mongrel ===
+
If you encounter difficulties installing fcgi (fastcgi) via gem, this might help:
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.
+
<pre><nowiki>
To install [http://mongrel.rubyforge.org/ mongrel], you can do the following:
+
 
<pre><nowiki>sudo apt-get install build-essential
+
sudo aptitude install libfcgi-dev
sudo apt-get install ruby1.8-dev
+
 
sudo gem install mongrel
+
 
</nowiki></pre>
 
</nowiki></pre>
If you would like to install the mysql gem,
+
=== Preparing for your first rails app ===
<pre><nowiki>sudo apt-get install libmysqlclient15-dev
+
Configuration of the webserver also assumes you have by then a rails application set up. Simply enter to get that done (it will create you a first test page):
 +
Default (sqlite3 backend):
 +
<pre><nowiki>
 +
rails /home/myuser/www/mynewapp
 
</nowiki></pre>
 
</nowiki></pre>
then run
+
For a mysql back-end.
<pre><nowiki>sudo gem install mysql
+
Run this one, if you installed rails with gem:
 +
<pre><nowiki>
 +
rails /home/myuser/www/mynewapp -d mysql
 
</nowiki></pre>
 
</nowiki></pre>
=== Advanced Apache + FCGI setup (not recommended) ===
+
Run this one, if you installed rails with apt:
Mongrel is the recommended way to setup a RoR server. It is was the whole RoR community uses.
+
<pre><nowiki>
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.
+
rails /home/myuser/www/mynewapp -D mysql
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>
 
</nowiki></pre>
Install FCGI for Apache and Ruby:
+
You will see a bunch of stuff on the shell... that should be fine (unless you see a error message). That created the rails folder with all the stuff, so now you should have something like have: /home/myuser/www/mynewapp/public
<pre><nowiki>sudo apt-get install libapache2-mod-fcgid
+
=== Configure the server ===
 +
==== WEBrick (Ruby's server) ====
 +
No configuring needed, everything is ready to create your first application. You can even try this step before you try launching your application via apache or lighttpd:
 +
<pre><nowiki>
 +
$cd /home/myuser/www/mynewapp
 +
$ruby script/server
 +
=> Booting WEBrick...
 +
=> Rails 2.1.0 application started on http://0.0.0.0:3000
 +
=> Ctrl-C to shutdown server; call with --help for options
 +
[2008-07-28 04:39:17] INFO  WEBrick 1.3.1
 +
[2008-07-28 04:39:17] INFO  ruby 1.8.6 (2007-09-24)
 +
[2008-07-28 04:39:17] INFO  WEBrick::HTTPServer#start: pid=6860 port=3000
 
</nowiki></pre>
 
</nowiki></pre>
After install mod_fcgid should be enabled, so you dont need to do  sudo a2enmod fcgid
+
Your application is now responding at `http://0.0.0.0:3000`; press Ctrl+C to stop the web server. If you get errors related to the database, '''you might need to install a DB connector, or create a database.''' Edit this in `$YOUR_RAILS_DIRECTORY/config/database.yml`
fcgid has a nasty default timeout value which is too low for rails. So edit..
+
Install the sqlite3 connector:
/etc/apache2/mods-available/fcgid.conf
+
 
<pre><nowiki>
 
<pre><nowiki>
<IfModule mod_fcgid.c>
+
sudo apt-get install libsqlite3-dev
  AddHandler fcgid-script .fcgi
+
sudo gem install sqlite3-ruby
  SocketPath /var/lib/apache2/fcgid/sock
+
  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.
+
Or install the MySQL database and it's connector:
That should take care of that. I found that fix here: http://brian.pontarelli.com/2006/08/06/fcgid-timeout-causing-rails-500-errors/
+
<pre><nowiki>
Moving on.
+
sudo apt-get install mysql-server mysql-client
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.
+
sudo apt-get install libmysql-ruby libmysqlclient-dev
<pre><nowiki>sudo apt-get install build-essential ruby1.8-dev libfcgi-dev
+
sudo gem install mysql
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.
+
Or install the [[UbuntuHelp:PostgreSQL|PostgreSQL]] database and it's connector:
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.
+
<pre><nowiki>
Set file permissions:
+
sudo apt-get install postgresql-server postgresql-client
<pre><nowiki>chgrp www-data -R /path/to/rails/app/log
+
sudo apt-get install libpq-dev
chgrp www-data -R /path/to/rails/app/tmp
+
sudo gem install pg
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>
 
</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.
+
==== Apache ====
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.
+
Run the following to install passenger, an apache module that adds support for running rails apps within apache:
==== Virtual Server Scenario ====
+
<pre><nowiki>
Create and edit new file:
+
sudo gem install passenger
<pre><nowiki>sudo nano /etc/apache2/sites-available/<servername>
+
sudo apt-get install apache2-dev libapr1-dev libaprutil1-dev
 +
sudo passenger-install-apache2-module
 
</nowiki></pre>
 
</nowiki></pre>
Contents of the new file should be:<br>
+
WARNING: libapache2-mod-passenger installation uninstalls libapache2-mod-php5 and php5.
 +
Now you have to configure apache to point to the passenger module. Simply copy the following suggested code from the <code><nowiki>passenger-install-apache2-module</nowiki></code> installation to the end of your Apache configuration file, which can be accessed by typing in the command <code><nowiki>sudo gedit /etc/apache2/apache2.conf</nowiki></code>:
 +
(if you are using RubyGems 1.1):
 +
<pre><nowiki>
 +
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
 +
</nowiki></pre>
 +
(if you are using RubyGems 1.2):
 +
<pre><nowiki>
 +
LoadModule passenger_module /usr/lib/ruby/gems/1.8/gems/passenger-2.0.3/ext/apache2/mod_passenger.so
 +
PassengerRoot /usr/lib/ruby/gems/1.8/gems/passenger-2.0.3
 +
PassengerRuby /usr/bin/ruby1.8
 +
</nowiki></pre>
 +
(if you are using RubyGems 1.3.1):
 +
<pre><nowiki>
 +
LoadModule passenger_module /usr/lib/ruby/gems/1.8/gems/passenger-2.0.6/ext/apache2/mod_passenger.so
 +
PassengerRoot /usr/lib/ruby/gems/1.8/gems/passenger-2.0.6
 +
PassengerRuby /usr/bin/ruby1.8
 +
</nowiki></pre>
 +
(if you are using RubyGems 1.3.6 and Ubuntu 9.10):
 +
<pre><nowiki>
 +
LoadModule passenger_module /usr/local/lib/ruby/gems/1.8/gems/passenger-2.2.11/ext/apache2/mod_passenger.so
 +
PassengerRoot /usr/local/lib/ruby/gems/1.8/gems/passenger-2.2.11
 +
PassengerRuby /usr/local/bin/ruby
 +
</nowiki></pre>
 +
Now you should point apache to the location of your Ruby app. Create something like this in /etc/apache2/sites-available/ror.myhost.com
 
<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
+
    RailsEnv development #It's optional string, but my rails app didn't work properly without it.
        <Directory /home/myuser/www/myrailsproject/public/>
+
</VirtualHost>
                Options ExecCGI FollowSymLinks
+
</nowiki></pre>
                AddHandler fcgid-script .fcgi
+
Alternatively, you could run rails in a specific sub uri of your current host, e.g. localhost. First, make a symbolic link to your rails public folder from with var/www (or wherever you have the root of your apache www folder), e.g.:
 +
<pre><nowiki>
 +
ln -s /home/myuser/www/mynewapp/public /var/www/mynewapp
 +
</nowiki></pre>
 +
In case of localhost (which is the default host with a fresh ubuntu/apache install) edit <code><nowiki>default</nowiki></code> in <code><nowiki>/etc/apache2/sites-available</nowiki></code> and add:
 +
<pre><nowiki>
 +
RailsBaseURI /mynewapp
 +
</nowiki></pre>
 +
''murb: I had to add <code><nowiki>RailsEnv development</nowiki></code> as well to get around the 'no route found to match "/rails/info/properties" with {:method=>:get}' warning... (can someone elaborate on why?)'' apparently this is because /info/properties is buggy and no longer supported: http://www.ruby-forum.com/topic/161924
 +
Save, and enable the rewrite module for apache, and register this site... and finally, restart apache.
 +
<pre><nowiki>
 +
sudo a2enmod rewrite
 +
sudo a2ensite ror.myhost.com
 +
sudo /etc/init.d/apache2 restart
 +
</nowiki></pre>
 +
==== An Alternate Apache Configuration with a named vhost Ubuntu 9.04 ====
 +
If you need to run php-based or other web applications on the same server, and if you can get a DNS change to point another name at the same ip address as your server, this configuration should work for you. Note: for the Rails app you must put its public folder in the Document Root, and that means you'll need two or more vhosts on your server.
 +
Lets say your old PHP apps are available at www.office.example.com, and the Rails app, myrailsapp,  will be at www.rails.example.com. Edit your /etc/apache2/sites-available/default file and add this to the end of it:
 +
<pre><nowiki>
 +
 
 +
LoadModule passenger_module /usr/lib/ruby/gems/1.8/gems/passenger-2.2.5/ext/apache2/mod_passenger.so
 +
 
 +
<VirtualHost *:80>
 +
ServerAdmin [email protected]
 +
ServerName www.rails.example.com
 +
DocumentRoot /var/www/myrailsapp/public/
 +
PassengerRoot /usr/lib/ruby/gems/1.8/gems/passenger-2.2.5
 +
  PassengerRuby /usr/bin/ruby1.8
 +
 +
        <Directory "/var/www/myrailsapp/public/">
 +
Options Indexes +ExecCGI FollowSymLinks
 
                 Order allow,deny
 
                 Order allow,deny
 
                 Allow from all
 
                 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>
 
         </Directory>
        ServerSignature On
+
 
#      ErrorDocument 500 /500.html
+
</VirtualHost>
        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:
+
* The loadmodule passenger line is required and will be given to you by the passenger setup script. Use whatever path it provides.
<pre><nowiki>sudo /etc/init.d/apache2 restart
+
* You MUST change your local DNS to point www.rails.example.com to the same ip address as www.office.example.com, see your administrator or your ISP.
 +
* The PassengerRoot and PassengerRuby are also required.
 +
* As usual with Apache there are many other ways to set up vhosts, see http://httpd.apache.org/docs/2.2/vhosts/
 +
The .htaccess file was needed by my application in the myapp/public directory ( note that it assumes a dispatch.cgi file):
 +
<pre><nowiki>
 +
Options +FollowSymLinks +ExecCGI
 +
 
 +
RewriteRule ^(.*)$ dispatch.cgi [QSA,L]
 +
RewriteEngine On
 +
 
 +
RewriteRule ^$ index.html [QSA]
 +
RewriteRule ^([^.]+)$ $1.html [QSA]
 +
RewriteCond %{REQUEST_FILENAME} !-f
 
</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.
+
==== Apache + Mongrel ====
==== Troubleshooting ====
+
You have to start Mongrel Cluster (under www-data ?).
Click the home page's "About your application's environment" button. Refer to output and errors below.
+
<pre><nowiki>
===== Lots of text, beginning with <code><nowiki>#!/usr/bin/ruby1.8</nowiki></code>... =====
+
sudo mongrel_rails start -d -p 8000 -e production -P /full/path/to/log/mongrel-1.pid
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 ====
+
And configure Apache's vhost:
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>
    Alias /<alias> "<path_to_your_applications_public_dir>"
+
<VirtualHost *:80>
    <Directory <path_to_your_applications_public_dir>>
+
        ServerName otherserver.me
         Options +FollowSymlinks +ExecCGI
+
 
         Order allow,deny
+
         ProxyPass / http://otherserver.me:8000/
        Allow from all
+
         ProxyPassReverse / http://otherserver.me:8000
        AddHandler fcgid-script .fcgi
+
        ProxyPreserveHost on
        RewriteEngine On
+
 
        RewriteBase /<alias>
+
  <Proxy *>
        RewriteRule ^$ index.html [QSA]
+
    Order allow,deny
        RewriteRule ^([^.]+)$ $1.html [QSA]
+
    Allow from all
        RewriteCond %{REQUEST_FILENAME} !-f
+
  </Proxy>
        RewriteRule ^(.*)$ dispatch.fcgi [QSA,L]
+
 
        ErrorDocument 500 /500.html
+
</VirtualHost>
    </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`).
+
</nowiki></pre>
* <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`).
+
[http://mongrel.rubyforge.org/wiki/Apache RubyForge Mongrel - Apache Best Practice Deployment]
* <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).
+
==== Lighttpd ====
* Remember to include rule of "<code><nowiki>SetEnv RAILS_ENV development</nowiki></code>" in the <code><nowiki>VirtualHost</nowiki></code> setting
+
Edit lighttpd.conf, e.g.:
Because everything is set, you don't need the `.htaccess` file in your app's `public` dir anymore.
+
<pre><nowiki>
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.
+
sudo kate /etc/lighttpd/lighttpd.conf
 +
 
 +
</nowiki></pre>
 +
and here starts the tricky part, paste at the end of the file:
 +
<pre><nowiki>
 +
 
 +
server.modules  += ( "mod_fastcgi", "mod_rewrite" )
 +
 
 +
 
 +
$HTTP["host"] == "localhost" {
 +
        server.document-root = "/home/santiago/proyectos/beholder/public/"
 +
 
 +
        server.dir-listing          = "disable"
 +
        server.error-handler-404 = "/dispatch.fcgi"
 +
 
 +
        fastcgi.server      = ( ".fcgi" => ( "localhost" => (
 +
                "min-procs"      => 1,
 +
                "max-procs"      => 1,
 +
                "socket" => "/tmp/ruby-beholder.socket",
 +
                "bin-path" => "/home/santiago/proyectos/beholder/public/dispatch.fcgi",
 +
                "bin-environment" => ( "RAILS_ENV" => "development" )
 +
        ) ) )
 +
 
 +
}
 +
 
 +
</nowiki></pre>
 +
now run
 +
<pre><nowiki>
 +
 
 +
sudo /etc/init.d/lighttpd restart
 +
 
 +
</nowiki></pre>  
 +
and you're set, go to http://localhost and you should see the rails index... click on the link "About your rails environment" and if you don't see a 404 or 500 error... then you're set (you should see either a rails error, or a rails message or something like that).
 +
=== Known Problems ===
 +
just in case you get a message like this:
 +
<pre><nowiki>
 +
kmin@metakmin:~/ruby_test$ gem -v
 +
1.2.0
 +
kmin@metakmin:~/ruby_test$ rails -v
 +
Rails 2.2.2
 +
kmin@metakmin:~/ruby_test$ gem update --system
 +
Updating RubyGems
 +
Nothing to update
 +
kmin@metakmin:~/ruby_test$ script/server
 +
Rails requires RubyGems >= 1.3.1 (you have 1.2.0). Please `gem update
 +
--system` and try again.
 +
kmin@metakmin:~/ruby_test$
 +
</nowiki></pre>
 +
dont panic, just run:
 +
<pre><nowiki>  
 +
sudo gem install rubygems-update
 +
sudo update_rubygems
 +
</nowiki></pre>
 +
If you get a message like this when starting WEBrick on 9.10
 +
<pre><nowiki>
 +
/usr/lib/ruby/gems/1.8/gems/rails-2.3.5/lib/initializer.rb:271:in `require_frameworks': no such file to load -- net/https (RuntimeError)
 +
</nowiki></pre>
 +
You may need to install these:
 +
<pre><nowiki>
 +
sudo apt-get install build-essential libopenssl-ruby
 +
sudo apt-get install ruby rubygems ruby1.8-dev libsqlite3-dev
 +
</nowiki></pre>
 +
* thanks to Tomasz Przybysz who mailed me with the problem and then with the solution
 +
for any comments on the lighttpd install please feel free to mail me to santiago-ve@ubuntu.com
 
----
 
----
[[category:CategoryDocumentation]]
+
[[category:CategoryDevelopment]]
  
 
[[category:UbuntuHelp]]
 
[[category:UbuntuHelp]]

2010年5月20日 (四) 00:07的最新版本

Alternative to manual installation

Some users may prefer to skip manual installation by using the unofficial pre-integrated TurnKey Ruby on Rails Appliance based on Ubuntu LTS.

Intro Ubuntu 8.04

These instructions are for Ubuntu 8.04 LTS (hardy). Instead of using apt 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 Amok Time. If you haven't seen that episode, trust me: you don't want that.

Intro Ubuntu 9.04 ... Apache2.2 tested only

Slight additions from 8.04 documentation...

Get Ruby

sudo apt-get install ruby-full build-essential

Note that `ruby-full` will install emacs and a slew of other non-essential packages. You can install all the necessary packages with this command:

sudo aptitude install ruby build-essential libopenssl-ruby ruby1.8-dev

Server Installation

Now you have ruby, you already got a server included, WEBrick. You may settle with that, also because the installation is much easier, and reboots are faster. But it lacks the configuration options that apache and lighttpd offer. So if you want to have RubyOnRails running on apache or lighttpd, make sure you have installed these first.

Apache

sudo apt-get install apache2 apache2-mpm-prefork apache2-prefork-dev

WARNING: libapache2-mod-passenger installation uninstalls libapache2-mod-php5 and php5.

Apache + Mongrel

Mongrel Cluster works as a proxy service under Apache (no additional modification is required).

sudo a2enmod proxy
sudo a2enmod proxy_http
sudo a2enmod rewrite
sudo /etc/init.d/apache2 restart
sudo apt-get install mongrel

Rails Wiki - Apache + Mongrel RubyForge Mongrel - Apache Best Practice Deployment

Lighttpd

Preparing the house:


sudo apt-get install lighttpd libfcgi-ruby1.8

Installing RubyGems

The link below is to a specific version of RubyGems (version 1.1). You can get a later version from http://rubyforge.org/frs/?group_id=126, but it should not be really necessary because we ask RubyGems to update itself at the end. But to make it more complete, a description of RubyGems 1.2 is also included, below the install instructions of RubyGems 1.2.

RubyGems via apt


sudo apt-get install rubygems

As of July 27, 2009, RubyGems 1.3.1 is installed via apt. To make gem work properly you should write gem's path to PATH add to ~/.bashrc:

export PATH=/var/lib/gems/1.8/bin:$PATH

To export path once just enter previous code to bash.

RubyGems 1.1.0

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

RubyGems 1.2.0

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

The below error is sometimes encountered after running sudo gem update --system.

/usr/bin/gem:23: uninitialized constant Gem::GemRunner (NameError)

To fix that error follow the below instructions.

sudo gedit /usr/bin/gem

In the file find:
require 'rubygems'

Add the following line below the above line:
require 'rubygems/gem_runner'

RubyGems 1.3.1 Ubuntu 8.04

wget http://rubyforge.org/frs/download.php/45905/rubygems-1.3.1.tgz
tar xzvf rubygems-1.3.1.tgz
cd rubygems-1.3.1
sudo ruby setup.rb
sudo ln -s /usr/bin/gem1.8 /usr/bin/gem
sudo gem update --system

RubyGems 1.3.1 Ubuntu 9.04

sudo apt-get install rubygems1.8
sudo gem install rubygems-update
sudo update_rubygems     note: this will clean out your gems!

Installing rails

We can now use RubyGems to install rails:


sudo gem install rails

Also we can install rails from apt ( not suggested for Ubuntu 9.04 )


sudo apt-get install rails

I think, it would be better to install rails with gem. If you encounter difficulties installing fcgi (fastcgi) via gem, this might help:


sudo aptitude install libfcgi-dev

Preparing for your first rails app

Configuration of the webserver also assumes you have by then a rails application set up. Simply enter to get that done (it will create you a first test page): Default (sqlite3 backend):

rails /home/myuser/www/mynewapp

For a mysql back-end. Run this one, if you installed rails with gem:

rails /home/myuser/www/mynewapp -d mysql

Run this one, if you installed rails with apt:

rails /home/myuser/www/mynewapp -D mysql

You will see a bunch of stuff on the shell... that should be fine (unless you see a error message). That created the rails folder with all the stuff, so now you should have something like have: /home/myuser/www/mynewapp/public

Configure the server

WEBrick (Ruby's server)

No configuring needed, everything is ready to create your first application. You can even try this step before you try launching your application via apache or lighttpd:

$cd /home/myuser/www/mynewapp
$ruby script/server
=> Booting WEBrick...
=> Rails 2.1.0 application started on http://0.0.0.0:3000
=> Ctrl-C to shutdown server; call with --help for options
[2008-07-28 04:39:17] INFO  WEBrick 1.3.1
[2008-07-28 04:39:17] INFO  ruby 1.8.6 (2007-09-24)
[2008-07-28 04:39:17] INFO  WEBrick::HTTPServer#start: pid=6860 port=3000

Your application is now responding at `http://0.0.0.0:3000`; press Ctrl+C to stop the web server. If you get errors related to the database, you might need to install a DB connector, or create a database. Edit this in `$YOUR_RAILS_DIRECTORY/config/database.yml` Install the sqlite3 connector:

sudo apt-get install libsqlite3-dev
sudo gem install sqlite3-ruby

Or install the MySQL database and it's connector:

sudo apt-get install mysql-server mysql-client
sudo apt-get install libmysql-ruby libmysqlclient-dev
sudo gem install mysql

Or install the PostgreSQL database and it's connector:

sudo apt-get install postgresql-server postgresql-client
sudo apt-get install libpq-dev
sudo gem install pg

Apache

Run the following to install passenger, an apache module that adds support for running rails apps within apache:

sudo gem install passenger
sudo apt-get install apache2-dev libapr1-dev libaprutil1-dev
sudo passenger-install-apache2-module

WARNING: libapache2-mod-passenger installation uninstalls libapache2-mod-php5 and php5. Now you have to configure apache to point to the passenger module. Simply copy the following suggested code from the passenger-install-apache2-module installation to the end of your Apache configuration file, which can be accessed by typing in the command sudo gedit /etc/apache2/apache2.conf: (if you are using RubyGems 1.1):

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

(if you are using RubyGems 1.2):

LoadModule passenger_module /usr/lib/ruby/gems/1.8/gems/passenger-2.0.3/ext/apache2/mod_passenger.so
PassengerRoot /usr/lib/ruby/gems/1.8/gems/passenger-2.0.3
PassengerRuby /usr/bin/ruby1.8

(if you are using RubyGems 1.3.1):

LoadModule passenger_module /usr/lib/ruby/gems/1.8/gems/passenger-2.0.6/ext/apache2/mod_passenger.so
PassengerRoot /usr/lib/ruby/gems/1.8/gems/passenger-2.0.6
PassengerRuby /usr/bin/ruby1.8

(if you are using RubyGems 1.3.6 and Ubuntu 9.10):

LoadModule passenger_module /usr/local/lib/ruby/gems/1.8/gems/passenger-2.2.11/ext/apache2/mod_passenger.so
PassengerRoot /usr/local/lib/ruby/gems/1.8/gems/passenger-2.2.11
PassengerRuby /usr/local/bin/ruby

Now you should point apache to the location of your Ruby app. Create something like this in /etc/apache2/sites-available/ror.myhost.com

<VirtualHost *>
    ServerName ror.myhost.com
    DocumentRoot /home/myuser/www/mynewapp/public
    RailsEnv development #It's optional string, but my rails app didn't work properly without it.
</VirtualHost>

Alternatively, you could run rails in a specific sub uri of your current host, e.g. localhost. First, make a symbolic link to your rails public folder from with var/www (or wherever you have the root of your apache www folder), e.g.:

ln -s /home/myuser/www/mynewapp/public /var/www/mynewapp

In case of localhost (which is the default host with a fresh ubuntu/apache install) edit default in /etc/apache2/sites-available and add:

RailsBaseURI /mynewapp

murb: I had to add RailsEnv development as well to get around the 'no route found to match "/rails/info/properties" with {:method=>:get}' warning... (can someone elaborate on why?) apparently this is because /info/properties is buggy and no longer supported: http://www.ruby-forum.com/topic/161924 Save, and enable the rewrite module for apache, and register this site... and finally, restart apache.

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

An Alternate Apache Configuration with a named vhost Ubuntu 9.04

If you need to run php-based or other web applications on the same server, and if you can get a DNS change to point another name at the same ip address as your server, this configuration should work for you. Note: for the Rails app you must put its public folder in the Document Root, and that means you'll need two or more vhosts on your server. Lets say your old PHP apps are available at www.office.example.com, and the Rails app, myrailsapp, will be at www.rails.example.com. Edit your /etc/apache2/sites-available/default file and add this to the end of it:


 LoadModule passenger_module /usr/lib/ruby/gems/1.8/gems/passenger-2.2.5/ext/apache2/mod_passenger.so
   
<VirtualHost *:80>
	ServerAdmin [email protected]
	ServerName www.rails.example.com
	DocumentRoot /var/www/myrailsapp/public/
	PassengerRoot /usr/lib/ruby/gems/1.8/gems/passenger-2.2.5
   	PassengerRuby /usr/bin/ruby1.8
	
        <Directory "/var/www/myrailsapp/public/">
		Options Indexes +ExecCGI FollowSymLinks 
                Order allow,deny
                Allow from all
        </Directory>

</VirtualHost>

  • The loadmodule passenger line is required and will be given to you by the passenger setup script. Use whatever path it provides.
  • You MUST change your local DNS to point www.rails.example.com to the same ip address as www.office.example.com, see your administrator or your ISP.
  • The PassengerRoot and PassengerRuby are also required.
  • As usual with Apache there are many other ways to set up vhosts, see http://httpd.apache.org/docs/2.2/vhosts/

The .htaccess file was needed by my application in the myapp/public directory ( note that it assumes a dispatch.cgi file):

Options +FollowSymLinks +ExecCGI

RewriteRule ^(.*)$ dispatch.cgi [QSA,L]
RewriteEngine On

RewriteRule ^$ index.html [QSA]
RewriteRule ^([^.]+)$ $1.html [QSA]
RewriteCond %{REQUEST_FILENAME} !-f

Apache + Mongrel

You have to start Mongrel Cluster (under www-data ?).

sudo mongrel_rails start -d -p 8000 -e production -P /full/path/to/log/mongrel-1.pid

And configure Apache's vhost:

<VirtualHost *:80>
        ServerName otherserver.me

        ProxyPass / http://otherserver.me:8000/
        ProxyPassReverse / http://otherserver.me:8000
        ProxyPreserveHost on

  <Proxy *>
    Order allow,deny
    Allow from all
  </Proxy>

</VirtualHost>

RubyForge Mongrel - Apache Best Practice Deployment

Lighttpd

Edit lighttpd.conf, e.g.:


sudo kate /etc/lighttpd/lighttpd.conf

and here starts the tricky part, paste at the end of the file:


server.modules   += ( "mod_fastcgi", "mod_rewrite" )


$HTTP["host"] == "localhost" {
        server.document-root = "/home/santiago/proyectos/beholder/public/"

        server.dir-listing          = "disable"
        server.error-handler-404 = "/dispatch.fcgi"

        fastcgi.server      = ( ".fcgi" => ( "localhost" => (
                "min-procs"       => 1,
                "max-procs"       => 1,
                "socket" => "/tmp/ruby-beholder.socket",
                "bin-path" => "/home/santiago/proyectos/beholder/public/dispatch.fcgi",
                "bin-environment" => ( "RAILS_ENV" => "development" )
        ) ) )

}

now run


sudo /etc/init.d/lighttpd restart

and you're set, go to http://localhost and you should see the rails index... click on the link "About your rails environment" and if you don't see a 404 or 500 error... then you're set (you should see either a rails error, or a rails message or something like that).

Known Problems

just in case you get a message like this:

kmin@metakmin:~/ruby_test$ gem -v
1.2.0
kmin@metakmin:~/ruby_test$ rails -v
Rails 2.2.2
kmin@metakmin:~/ruby_test$ gem update --system
Updating RubyGems
Nothing to update
kmin@metakmin:~/ruby_test$ script/server
Rails requires RubyGems >= 1.3.1 (you have 1.2.0). Please `gem update
--system` and try again.
kmin@metakmin:~/ruby_test$

dont panic, just run:

 
sudo gem install rubygems-update
sudo update_rubygems

If you get a message like this when starting WEBrick on 9.10

/usr/lib/ruby/gems/1.8/gems/rails-2.3.5/lib/initializer.rb:271:in `require_frameworks': no such file to load -- net/https (RuntimeError)

You may need to install these:

sudo apt-get install build-essential libopenssl-ruby
sudo apt-get install ruby rubygems ruby1.8-dev libsqlite3-dev 
  • thanks to Tomasz Przybysz who mailed me with the problem and then with the solution

for any comments on the lighttpd install please feel free to mail me to [email protected]