Install Ruby on Rails with Apache on Debian 8
- Debian 9
- Deprecated guides:
- Ubuntu 9.10
- Ubuntu 9.04
- Ubuntu 10.10
- Ubuntu 10.04
- Debian 7
- Debian 6
Traducciones al EspañolEstamos traduciendo nuestros guías y tutoriales al Español. Es posible que usted esté viendo una traducción generada automáticamente. Estamos trabajando con traductores profesionales para verificar las traducciones de nuestro sitio web. Este proyecto es un trabajo en curso.
DeprecatedThis guide has been deprecated and is no longer being maintained. Please refer to the updated version of this guide.
Ruby on Rails is a rapid development web framework that allows web designers and developers to implement dynamic fully featured web applications. This guide deploys Rails applications using the Phusion Passenger or mod_rails
method. Passenger allows you to embed Rails apps directly in Apache applications without needing to worry about FastCGI or complex web server proxies.
sudo
. If you’re not familiar with the sudo command, you can check our
Users and Groups guide.Before You Begin
If you have not already done so, create a Linode account and Compute Instance. See our Getting Started with Linode and Creating a Compute Instance guides.
Follow our Setting Up and Securing a Compute Instance guide to update your system and configure your hostname. You may also wish to set the timezone, create a limited user account, and harden SSH access.
To check your hostname run:
hostname hostname -f
The first command should show your short hostname, and the second should show your fully qualified domain name (FQDN) if you have one assigned.
Installing Passenger and Dependencies
Install the system packages required for using Ruby, building Ruby modules, and running Rails applications:
sudo apt-get install build-essential libapache2-mod-passenger apache2 ruby ruby-dev libruby zlib1g-dev libsqlite3-dev
Using the
gem
package manager for Ruby modules, install thefastthread
gem:sudo gem install fastthread
Install Ruby On Rails:
sudo gem install rails
Note If you require a specific version of Ruby On Rails, issue one of the following commands for the version you need:
gem install rails –version 2.1.2 gem install rails –version 2.2.2 gem install rails –version 2.3.5 gem install rails –version 3.0.4
This will install the appropriate versions of all required packages including ruby, rack, and other dependencies needed for basic Rails development.
(Optional) Install additional dependencies for your application, such as MySQL support:
sudo apt-get install mysql-server libmysqlclient-dev mysql-client mysql-common sudo gem install mysql
Any other dependencies can be installed mimicking the steps above: Install the needed packages, then the needed gem.
Add
rails
to your $PATH environment variable. Make sure to replaceVERSION
with the version of Ruby you are running:ls /var/lib/gems PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/var/lib/gems/VERSION/bin"
Note The step above will only add this PATH to your current session. To retain the change persistently, add the PATH to your local ~/.bashrc file:
echo “PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/var/lib/gems/VERSION/bin” » ~/.bashrc
In your website’s public folder start a new rails project, to ensure everything is properly configured. Replace
application
with your application’s name:sudo rails new /var/www/html/example.com/public_html/application
Configuring Apache to Work with Passenger
If your Apache virtual hosts file(s) mimics the ones create in the
Apache Web Server on Debian 8 guide, you will have a <VirtualHost>
block containing a DocumentRoot
value similar to /var/www/html/example.com/public_html/
.
Open the file in a text editor, and edit the
DocumentRoot
to reflect the public directory of your application:- File: /etc/apache2/sites-available/example.com.conf
1
DocumentRoot /var/www/html/example.com/application/public
Restart Apache to ensure all settings have been loaded:
sudo systemctl restart apache2
Deploying Multiple Rails Apps
There are a number of strategies for deploying more than one Rails application using Passenger. The simplest approach requires running multiple distinct virtual hosts configured as above to host a single Rails app each. Alternatively, you may host multiple Rails apps within a single virtual host. This section covers the latter option.
Add
RailsBaseURI
directives that specify the path to your Rails application within the VirtualHost configuration as in the following example:- File: /etc/apache2/sites-available/example.com.conf
1 2 3 4
DocumentRoot /var/www/html/example.com/public_html/ RailsBaseURI /app1 RailsBaseURI /app2 RailsBaseURI /app3
These directives configure Passenger to run three Rails apps on the
example.com
site at the three locations specified.Link the
public/
directory of the application to a directory within thepublic_html/
directory:ln -s /var/www/html/example.com/app1/public/ /var/www/html/example.com/public_html/app1/ ln -s /var/www/html/example.com/app1/app2/ /var/www/html/example.com/public_html/app2/ ln -s /var/www/html/example.com/app1/app3/ /var/www/html/example.com/public_html/app3/
Congratulations! You have successfully deployed Ruby On Rails applications with the Apache Web server and Passenger.
More Information
You may wish to consult the following resources for additional information on this topic. While these are provided in the hope that they will be useful, please note that we cannot vouch for the accuracy or timeliness of externally hosted materials.
This page was originally published on