Ruby on Rails runs on web server Webrick. But in order to deploy Ruby on Rails with Nginx server we will need Passenger too.
Install Passenger and Ngnix
First install PGP key
sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys 561F9B9CAC40B2F7
Create APT source file
sudo nano /etc/apt/sources.list.d/passenger.list
Insert following lines in the file:
deb https://oss-binaries.phusionpassenger.com/apt/passenger trusty main
Now in order to save the file, press Ctrl+x
, then type y
to save it and press Enter
to confirm location.
Change owner and permissions of the file
sudo chown root: /etc/apt/sources.list.d/passenger.list
sudo chmod 600 /etc/apt/sources.list.d/passenger.list
Update the packages
sudo apt-get update
Now final step is to install Nginx and Passenger
sudo apt-get install nginx-extras passenger
Overwrite Ruby to an older version
sudo rm /usr/bin/ruby
sudo ln -s /usr/local/bin/ruby /usr/bin/ruby
Setup Web Server
Open the Configuration file by typing
sudo nano /etc/nginx/nginx.conf
Find the following lines on Configuration file:
# passenger_root /usr/lib/ruby/vendor_ruby/phusion_passenger/locations.ini;
# passenger_ruby /usr/bin/ruby;
Uncomment these lines:
passenger_root /usr/lib/ruby/vendor_ruby/phusion_passenger/locations.ini;
passenger_ruby /usr/local/bin/ruby;
Now lets deploy Ruby on Rails app with Nginx and Passenger
cd ~
sudo gem install --no-rdoc --no-ri rails
rails new testapp --skip-bundle
cd testapp
nano Gemfile
# gem 'therubyracer', platforms: :ruby
gem 'therubyracer', platforms: :ruby
bundle install
We need to disable Nginx configuration
sudo nano /etc/nginx/sites-available/default
Find these lines:
listen 80 default_server;
listen [::]:80 default_server ipv6only=on;
Comment these lines
# listen 80 default_server;
# listen [::]:80 default_server ipv6only=on;
Save this file and create Nginx configuration file for our app:
sudo nano /etc/nginx/sites-available/testapp
Add the following server
block. The settings are explained below.
server {
listen 80 default_server;
server_name www.mydomain.com;
passenger_enabled on;
passenger_app_env development;
root /home/rails/testapp/public;
}
Save the file (CTRL+x, y, ENTER).
Create a symlink for it:
sudo ln -s /etc/nginx/sites-available/testapp /etc/nginx/sites-enabled/testapp
Restart Nginx:
sudo nginx -s reload
Now your app's website should be accessible. Navigate to your domain or IP address:
http://ip_address or http://locahost
Thats all!
Thanks for reading the blog
0 Comment(s)