Ruby on Rails with NGINX On Debian 9
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.
Ruby on Rails is a web framework that allows web designers and developers to implement dynamic, fully featured web applications. When deploying a Rails app in production, developers can choose from several popular app servers including Puma, Unicorn, and Passenger. This guide will use Passenger, because of its convenient integration with NGINX.
NoteThis guide is written for a non-root user. Commands that require elevated privileges are prefixed withsudo
. If you’re not familiar with thesudo
command, you can check our Users and Groups guide.
Before You Begin
Follow the Getting Started and Securing the Server guides, and set the Linode’s hostname.
To check the hostname run:
hostname hostname -f
The first command should show the short hostname, and the second should show the fully qualified domain name (FQDN).
Update the system:
sudo apt-get update && sudo apt-get upgrade
Install Dependencies
Install the system packages required for using Ruby, building Ruby modules, and running Rails applications:
sudo apt-get install build-essential dirmngr gnupg ruby ruby-dev zlib1g-dev libruby libssl-dev libpcre3-dev libcurl4-openssl-dev rake ruby-rack
Install Ruby
Use the Ruby Version Manager (RVM) to install Ruby. Be sure to install a Ruby version that is compatible with the version of Rails in your Gemfile. This guide will use Rails 5.1.4 and Ruby 2.4.2.
Install the mpapis GPG key:
gpg --keyserver hkp://keys.gnupg.net --recv-keys 409B6B1796C275462A1703113804BB82D39DC0E3 7D2BAF1CF37B13E2069D6956105BD0E739499BDB
If this does not work, your system may not have
dirmngr
installed by default. Install it to correct the error:sudo apt install dirmngr
Run the official RVM installation script:
curl -sSL https://get.rvm.io | bash -s stable --ruby
The installation process will output a command that must be run before RVM can be used:
source /home/username/.rvm/scripts/rvm
Check the requirements for
rvm
:rvm requirements
Install a version of Ruby and set it as the default version for your system:
rvm install ruby rvm --default use ruby
If your project requires a different version of ruby, install that version explicitly:
rvm install ruby-2.5.0 rvm --default use ruby-2.5.0
Install Rails
Use the Rubygems package manager to install Rails. Replace the version below with the appropriate version for your app:
gem install rails -v 5.1.4
Install NGINX And Passenger
Install NGINX:
sudo apt install nginx
Phusion hosts a repository containing the latest version of Phusion Passenger. To add this to the package manager, first install the Phusion PGP key:
sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv-keys 561F9B9CAC40B2F7 sudo sh -c 'echo deb https://oss-binaries.phusionpassenger.com/apt/passenger stretch main > /etc/apt/sources.list.d/passenger.list'
Enable HTTPS support for APT:
sudo apt-get install apt-transport-https ca-certificates
Update the local package database and install Phusion Passenger:
sudo apt-get update sudo apt-get install libnginx-mod-http-passenger
Enable Passenger Support and Start NGINX
NGINX is now installed on the system, but support for Phusion Passenger is not enabled. As root, or with the
sudo
command, open the file/etc/nginx/conf.d/mod-http-passenger.conf
and verify that the following two lines are present and uncommented:- File: /etc/nginx/conf.d/mod-http-passenger.conf
1 2
passenger_root /usr/lib/ruby/vendor_ruby/phusion_passenger/locations.ini; passenger_ruby /usr/bin/passenger_free_ruby;
Note
If the file does not already exist, you will need to create it and add the lines manually.Restart NGINX:
sudo systemctl restart nginx
To verify that Passenger support has been installed and enabled correctly:
sudo passenger-memory-stats
If Passenger is running, a few running processes should be displayed under the “Passenger processes” section:
----- Passenger processes ----- PID VMSize Private Name ------------------------------- 14337 420.8 MB 1.1 MB Passenger watchdog 14340 559.3 MB 1.4 MB Passenger core 14345 292.5 MB 1.2 MB Passenger ust-router
Install MySQL Support (Optional)
If the application deployed uses MySQL, install the database server by following our MySQL on Debian 8 guide. Once it’s installed and configured properly, issue the following command:
sudo apt-get install libmysqlclient-dev
Deploy Rails App
Copy your Rails app to your Linode. Navigate to the app’s root directory and install any dependencies:
cd railsapp bundle install
Rails requires a JavaScript runtime. Install Node.js:
sudo curl -sL https://deb.nodesource.com/setup_6.x | sudo -E bash - sudo apt install nodejs
Note
If your Gemfile already includestherubyracer
, or you have another JavaScript runtime on your system, you can skip this step.Open
/etc/nginx/sites-available/default
in a text editor and removedefault_server
from the first two lines of theserver
block:- File: /etc/nginx/sites-available/default
server { listen 80; listen [::]:80; . . .
Since you are using RVM, you will need to specify which version of Ruby should be used by Passenger:
rvm use passenger-config --ruby-command
The
passenger-config
command will generate several lines of output, similar to:passenger-config was invoked through the following Ruby interpreter: Command: /home/username/.rvm/gems/ruby-2.4.2/wrappers/ruby Version: ruby 2.4.2p198 (2017-09-14 revision 59899) [x86_64-linux] To use in Apache: PassengerRuby /home/username/.rvm/gems/ruby-2.4.2/wrappers/ruby To use in Nginx : passenger_ruby /home/username/.rvm/gems/ruby-2.4.2/wrappers/ruby To use with Standalone: /home/username/.rvm/gems/ruby-2.4.2/wrappers/ruby /home/username/.rvm/gems/ruby-2.4.2/gems/passenger-5.1.11/bin/passenger start
Copy the NGINX line for use in the next step.
Configure a new site for your Rails app. Create
/etc/nginx/sites-available/railsapp
in a text editor and add the following content:- File: /etc/nginx/sites-available/railsapp
server { listen 80 default_server; server_name 192.0.2.0; passenger_ruby /home/path/to/ruby/installation; passenger_enabled on; root /path/to/app/public; }
Set the server_name
to the public IP address or FQDN of your Linode and replace the root
path with the path to your Rails application. Paste the output of the passenger-config
command to replace the passenger_ruby
line.
Create a symlink to
sites-enabled
to activate the new site:sudo ln -s /etc/nginx/sites-available/railsapp /etc/nginx/sites-enabled/railsapp
Restart NGINX:
sudo systemctl restart nginx
In a web browser, navigate to your Linode’s public IP address. Your Rails app should now be live.
Next Steps
Now that your app is running, consider using build tools such as Capistrano, or continuous integration (CI) tools such as Travis or Jenkins, to speed up your deployment workflow.
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