Академический Документы
Профессиональный Документы
Культура Документы
Series (/series) Screencasts (/episodes) Community (/forum) Account (/users/edit)
Want an automated way to con gure a server and deploy Rails apps to it? Check out Hatch! (https://hatch.gorails.com)
Overview
We will be setting up a Ruby on Rails production environment on Ubuntu 16.04 LTS Xenial Xerus.
Since we setup Ubuntu for our development environment, we also want to use it in production. This keeps your application running consistently
between development and production. We're using an LTS version of Ubuntu in production because it is supported for several years where a normal
version of Ubuntu isn't.
Using Ubuntu LTS in production allows you to continue receiving security updates which is important for your production server(s).
We're going to be setting up a Droplet on Digital Ocean (https://www.digitalocean.com/?refcode=87fcb9dab7a3) for our server. It costs $5/mo and
is a great place to host your applications.
If you sign up with my Digital Ocean referral link (https://www.digitalocean.com/?refcode=87fcb9dab7a3), you'll get 2 months ($10) free credit
to try it out.
You can use any cloud server hosting company you choose for your Rails application. I've had excellent experience with Digital Ocean
(https://www.digitalocean.com/?refcode=87fcb9dab7a3) and Linode (https://www.linode.com/?
r=a02b271802c33ff2f38b3d5335089d76648ca6c2) with the servers I have used. If you're looking for alternatives outside the US or otherwise, just
https://gorails.com/deploy/ubuntu/16.04 1/14
7/26/2017 Deploy Ruby On Rails on Ubuntu 16.04 Xenial Xerus - GoRails
google "VPS hosting". A VPS is a virtual private server. It's just like a server you setup at home, only virtualize and running with a suite of other
servers in a datacenter.
First, we need to choose which OS to use. We're going to be using Ubuntu 16.04 LTS x64. Your application may require a different OS or version, but
if you're not sure this is generally what you should use.
(https://gorails.com/assets/droplet-image-16.04-a565619112de1557e5e60640097671a4202f732dd87cbc92dd5924bd1ececb1f.png)
Since we're using Digital Ocean (https://www.digitalocean.com/?refcode=87fcb9dab7a3) for our cloud server, the rst thing we're going to do is
con gure a new one. I'm going with the Droplet with 1GB of RAM. You can setup whichever size server you prefer, keep in mind that if you choose a
512MB server you may run into some slowness with a low amount of RAM.
(https://gorails.com/assets/droplet-size-3751c8c849b2371987fd8e0b194c625c2ffae0a451a43b28682cbe1254c2ad8d.png)
The next step is to choose your location. Choose one close to you so that you can have better connection speeds.
(https://gorails.com/assets/droplet-region-0bc8eef1015fc1998ed2cbf666ded266765073cdd149f802377dc07fdd31be70.png)
Optionally you can add your SSH key into the Droplet so you can SSH in and skip the ssh-copy-id step.
Once Digital Ocean has con gured your server, check your email to get your password for the new cloud server.
You should follow the instructions in the email to login via SSH for the very rst time and verify it is working.
The rst thing we will do on our new server is create the user account we'll be using to run our applications and work from there.
https://gorails.com/deploy/ubuntu/16.04 2/14
7/26/2017 Deploy Ruby On Rails on Ubuntu 16.04 Xenial Xerus - GoRails
Before we move forward is that we're going to setup SSH to authenticate via keys instead of having to use a password to login. It's more secure and
will save you time in the long run.
We're going to use ssh-copy-id to do this. If you're on OSX you may need to run brew install ssh-copy-id but if you're following this tutorial
on Linux desktop, you should already have it.
Once you've got ssh-copy-id installed, run the following and replace IPADDRESS with the one for your server:
Make sure you run ssh-copy-id on your computer, and NOT the server.
ssh-copy-id deploy@IPADDRESS
Now when you run ssh deploy@IPADDRESS you will be logged in automatically. Go ahead and SSH again and verify that it doesn't ask for your
password before moving onto the next step.
For the next steps, make sure you are logged in as the deploy user on the server!
Installing Ruby
2.4.0 (Recommended)
Next we're going to be installing Ruby using one of three methods. Each have their own bene ts, most people prefer using rbenv these days, but if
you're familiar with rvm you can follow those steps as well. I've included instructions for installing from source as well, but in general, you'll want to
choose either rbenv or rvm.
Choose one method. Some of these con ict with each other, so choose the one that sounds the most interesting to you, or go with my suggestion,
rbenv.
Installing with rbenv is a simple two step process. First you install rbenv , and then ruby-build :
cd
git clone https://github.com/rbenv/rbenv.git ~/.rbenv
echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bashrc
echo 'eval "$(rbenv init -)"' >> ~/.bashrc
exec $SHELL
https://gorails.com/deploy/ubuntu/16.04 3/14
7/26/2017 Deploy Ruby On Rails on Ubuntu 16.04 Xenial Xerus - GoRails
Installing Nginx
Phusion is the company that develops Passenger and they recently put out an of cial Ubuntu package that ships with Nginx and Passenger pre-
installed.
We'll be using that to setup our production server because it's very easy to setup.
So now we have Nginx and passenger installed. We can manage the Nginx webserver by using the service command:
Open up the server's IP address in your browser to make sure that nginx is up and running.
The service command also provides some other methods such as restart and stop that allow you to easily restart and stop your webserver.
Next, we need to update the Nginx con guration to point Passenger to the version of Ruby that we're using. You'll want to open up
/etc/nginx/nginx.conf in your favorite editor. I like to use vim , so I'd run this command:
##
# Phusion Passenger
##
# Uncomment it if you installed ruby-passenger or ruby-passenger-enterprise
##
include /etc/nginx/passenger.conf;
Save and close nginx.conf. Then open /etc/nginx/passenger.conf in your editor to modify the ruby line:
https://gorails.com/deploy/ubuntu/16.04 4/14
7/26/2017 Deploy Ruby On Rails on Ubuntu 16.04 Xenial Xerus - GoRails
The passenger_ruby is the important line here. Make sure you only set this once and use the line from the example that pertains to the version of
Ruby you installed.
Once you've changed passenger_ruby to use the right version Ruby, you can run the following command to restart Nginx with the new Passenger
con guration.
Now that we've restarted Nginx, the Rails application will be served up using the deploy user just how we want. In the Capistrano section we will talk
about con guring Nginx to serve up your Rails application.
Setting up your production database is pretty easy. Make sure to keep in mind that you should use a different password for your production
databases.
Depending on what database you want to use, follow the steps related to the database:
Installing MySQL
All you need to do in order to install MySQL is to run the following command:
You can use the root user and password set during installation for your database or you can add a new user
(https://www.digitalocean.com/community/articles/how-to-create-a-new-user-and-grant-permissions-in-mysql) to MySQL.
Make sure you also create your app's database now. If you're not sure how to do this, follow this guide to create your mysql database
(https://wiki.gandi.net/en/hosting/using-linux/tutorials/ubuntu/createdatabase). Take note of your database name and password so you can use this
when we setup the database.yml le later on.
Installing PostgreSQL
Postgres 9.3 is available in the Ubuntu repositories and we can install it like so:
Next we need to setup our postgres user (also named "deploy" but different from our linux user named "deploy") and database:
sudo su - postgres
createuser --pwprompt deploy
createdb -O deploy my_app_name_production # change "my_app_name" to your app's name which we'll also use later on
exit
The password you type in here will be the one to put in your my_app/current/config/database.yml later when you deploy your app for the rst
time.
Capistrano Setup
For Capistrano, make sure you do these steps on your development machine inside your Rails app.
https://gorails.com/deploy/ubuntu/16.04 5/14
7/26/2017 Deploy Ruby On Rails on Ubuntu 16.04 Xenial Xerus - GoRails
Once these are added, run the following command to generate your capistrano con guration.
Next we need to make some additions to our Cap le to include bundler, rails, and rbenv/rvm (if you're using them). Edit your Capfile and add these
lines:
require 'capistrano/rails'
require 'capistrano/passenger'
After we've got Capistrano installed, we can con gure the config/deploy.rb to setup our general con guration for our app. Edit that le and make
it like the following replacing "myapp" with the name of your application and git repository:
Now we need to open up our config/deploy/production.rb le to set the server IP address that we want to deploy to:
If you have any trouble with Capistrano or the extensions for it, check out Capistrano's Github page (https://github.com/capistrano/).
Final Steps
Open up /etc/nginx/sites-enabled/default in your text editor and we will replace the le's contents with the following:
https://gorails.com/deploy/ubuntu/16.04 6/14
7/26/2017 Deploy Ruby On Rails on Ubuntu 16.04 Xenial Xerus - GoRails
server {
listen 80;
listen [::]:80 ipv6only=on;
server_name mydomain.com;
passenger_enabled on;
rails_env production;
root /home/deploy/my_app_name/current/public;
This is our Nginx con guration for a server listening on port 80. You need to change the server_name values to match the domain you want to use
and in root replace "myapp" with the name of your application.
First we'll move these les to their example names in the git repo.
You can run cap production deploy to deploy your application, but it's going to fail this rst time because we haven't created either of these les
on the server which we will do in just a second.
One last time, ssh into your server as the deploy user and this time we need to create two les. First is the database.yml that uses the password
for the postgres user you created earlier.
# /home/deploy/my_app_name/shared/config/database.yml
production:
adapter: postgresql
host: 127.0.0.1
database: my_app_name_production
username: deploy
password: YOUR_POSTGRES_PASSWORD
encoding: unicode
pool: 5
Now we create secrets.yml . We need to run rake secret on your development machine and take the secret key output of that and paste that in
as YOUR_SECRET_KEY on the server.
# /home/deploy/my_app_name/shared/config/secrets.yml
production:
secret_key_base: YOUR_SECRET_KEY
You can run cap production deploy one last time to get your full deployment to run. This should completed successfully and you should see your
new site live! You can just run Capistrano again to deploy any new changes you've pushed up to your Git repository.
Conclusion
And there you have it, a very long-winded explanation of all the different things you need to do while setting up an application to be deployed. There
is a lot of system administration pieces that can expand upon this, but that's for another time. Please let me know if you have any questions,
comments, or suggestions!
Sort by Best
Recommend 6 ⤤ Share
cap aborted!
SSHKit::Runner::ExecuteError: Exception while executing as deploy@redacted: passenger-config exit status: 1
passenger-config stdout: Nothing written
passenger-config stderr: *** ERROR: Phusion Passenger doesn't seem to be running. If you are sure that it
is running, then the causes of this problem could be one of:
https://gorails.com/deploy/ubuntu/16.04 8/14
7/26/2017 Deploy Ruby On Rails on Ubuntu 16.04 Xenial Xerus - GoRails
You can try using the older trusty repository for now while they work up on updating their apt repository:
You guys can remove that passenger repository and install the version directly from Ubuntu. Should be good enough for
now, and then once Phusion updates their repo, you can switch back to it so you can continue to get updates.
http://packages.ubuntu.com/...§ion=all
△ ▽ • Reply • Share ›
Unfortunately they haven't released a package for xenial yet. It's on their todo list, but they said by June at the latest. That's
a long time from now.
[EDIT] Seem that my passenger is not running correctly. Can anybody help me?
1△ ▽ • Reply • Share ›
I have a issue here: No Rakefile found (looking for: capfile, Capfile, capfile.rb, Capfile.rb, /usr/lib/ruby/vendor_ruby/Capfile)
by the way when I opened /etc/nginx/nginx.conf the first time, I haven't find the lines passenger_ruby and passenger_root
1△ ▽ • Reply • Share ›
"config.force_ssl = true"
What was happening was that each time you connected, the rails application forced you to redirect to "https" for SSL but your SSL cert is
not setup yet!
△ ▽ • Reply • Share ›
passenger_root /usr/lib/ruby/vendor_ruby/phusion_passenger/locations.ini;
passenger-config --root
△ ▽ • Reply • Share ›
https://gorails.com/deploy/ubuntu/16.04 10/14
7/26/2017 Deploy Ruby On Rails on Ubuntu 16.04 Xenial Xerus - GoRails
Incredibly useful tutorial! For a small bit of extra config and command line hacking, I get to deploy my app to a server I control rather than an
option like Heroku. Thanks for putting this together!
△ ▽ • Reply • Share ›
Logged into the server as deploy, ruby version was 2.4.1. After a lot of troubleshooting, I su'd into root, and found that root had ruby 2.3.1 as it's
version.
I figured i must have screwed something up, so I started fresh.... this time watching the output of each command closely.
It seems that the Passenger install (as deploy user) imported ruby 2.3.1... 2.4.1 was already set up under deploy... and this 2.3.1 did not seem to
change anything for deploy. However, root, which had no ruby prior to passenger, was now set to 2.3.1.
I've updated root's ruby to be 2.4.1 using the rbenv method as root so that I could get something deployed to practice on... but...
I'm confused as to how and why 2.3.1 got there, and why it's interfering with the deploy command when i try to push an update...
△ ▽ • Reply • Share ›
Meant to reply sooner, but all your ssh keys are located in ~/.ssh so you can backup that folder and just replace it on your new install to
use the same keys.
△ ▽ • Reply • Share ›
my gemfile has:
group :development do
gem 'capistrano', '~> 3.7', '>= 3.7.1'
gem 'capistrano-rails', '~> 1.2'
gem 'capistrano-passenger', '~> 0.2.0'
gem 'capistrano-rbenv', '~> 2.1'
my capfile has:
# Capfile
see more
△ ▽ • Reply • Share ›
you can create the database.yml same way as you did with secrets.yml. Create an empty file in same place where secrets.yml are and
https://gorails.com/deploy/ubuntu/16.04 11/14
7/26/2017 Deploy Ruby On Rails on Ubuntu 16.04 Xenial Xerus - GoRails
you can create the database.yml same way as you did with secrets.yml. Create an empty file in same place where secrets.yml are and
then copy the code from your local copy (ie development env). You have to set the credentials accordning to the ones you have set on
production obviously :-)
To create the files (assuming you are connected on server) cd into your shared/config/ folder and then type touch database.yml and touch
secrets.yml
△ ▽ • Reply • Share ›
I'm just having an issue when trying to set the branch for the repo, it always took master and I'm using the set branch but nothing happens
△ ▽ • Reply • Share ›
Hey @Erendira! I'm not sure how you've got it setup, but if you set the branch in your Capistrano config, that should do the trick. You can
either do it in config/deploy.rb globally (it defaults to master) or inside each of the stages like config/deploy/production.rb. It's usually just
set :branch, "mybranch"
deploy.rb:
# ask :branch, proc { `git rev-parse --abbrev-ref HEAD`.chomp } -- try this and ask for the branch, I typed develop, but neither
worked
set :branch, 'develop'
0:02 git:check
01 git ls-remote git@bitbucket.org:altocustos/custos_api.git HEAD
01 a64ca2f22cfaeee296f994b1475d422b179ec94a -- this is the only commit on master branch
01
01 HEAD
01
✔ 01 ubuntu@34.208.184.149 1.825s
△ ▽ • Reply • Share ›
Yep your code looks right. I think that git ls-remote is fine because Capistrano actually clones your full repo to the server (all
the branches) and then when it does a "git archive branch | tar" command later to make the new release folder, it will grab
the branch there. I don't think this step is gonna be the problem, so it might actually be using the right branch.
△ ▽ • Reply • Share ›
Now I have another problem :( when it tries to precompile the assets it gives me an error, because mi app is a rails 5 api,
how can I disable the assets:precompile?
△ ▽ • Reply • Share ›
Awesome! :)
I believe for that, you can either override the deploy:assets:precompile task with an empty task, or you can also possibly
remove the web role from the server line in config/deploy/production.rb. That may also disable some other things that run
on the web role, but I don't remember what those might be so it might be the best solution for an api-only app.
△ ▽ • Reply • Share ›
I fixed the issue I initally posted about lol. But now when I visit the URL there's no site to visit, it says unable to connect
-----
Great tutorial say I'm trying to deploy a rails 5 app with ubuntu 16.04 etc... On cap production deploy I get the following error on deploy:migrate
△ ▽ • Reply • Share ›
Thanks
△ ▽ • Reply • Share ›
Hope it helps ;)
Edit: Don't forget to set up DO namespaces at your domain registrar panel. (tutorial link: https://www.digitalocean.co...
△ ▽ • Reply • Share ›
https://gorails.com/deploy/ubuntu/16.04 13/14
7/26/2017 Deploy Ruby On Rails on Ubuntu 16.04 Xenial Xerus - GoRails
(/)
© 2014-2017, Chris Oliver. (http://excid3.com)
LEARN EXTRAS ABOUT
Series (/series) Deploy Rails (https://hatch.gorails.com) Feedback
Episodes (/episodes) Pricing (/pricing) (http://gorails.uservoice.com/forums/259979-
Community (/forum) Testimonials (/testimonials) general)
Guides (/guides) Blog (/blog) Terms (/terms)
Courses (https://courses.gorails.com/) Privacy (/privacy)
About Us (/about)
Icons by Icons8 (https://icons8.com)
https://gorails.com/deploy/ubuntu/16.04 14/14