
7/12/2014 Deploy Ruby On Rails on Ubuntu 14.04 Trusty Tahr - GoRails Deploy Ruby On Rails on Ubuntu 14.04 Trusty Tahr A guide to setting up a Ruby on Rails production environment Ubuntu 14.04 (/deploy/ubuntu/14.04) Ubuntu 12.04 (/deploy/ubuntu/12.04) Overview This will take about 45 minutes. We will be setting up a Ruby on Rails production environment on Ubuntu 14.04 LTS Trusty Tahr. 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. Creating A Virtual Private Server 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 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. https://gorails.com/deploy/ubuntu/14.04 1/25 7/12/2014 Deploy Ruby On Rails on Ubuntu 14.04 Trusty Tahr - GoRails Since we're using Digital Ocean (https://www.digitalocean.com/? refcode=87fcb9dab7a3) for our cloud server, the first thing we're going to do is configure 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. (http://cl.ly/RGNu/Screen%20Shot%202013-09-08%20at%206.00.59%20PM.png) The next step is to choose your location. Choose one close to you so that you can have better connection speeds. (http://cl.ly/RFcK/Screen%20Shot%202013-09-08%20at%206.02.14%20PM.png) Last, but not least we need to choose which OS to use. We're going to be using Ubuntu 14.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/deploy/ubuntu/14.04 2/25 7/12/2014 Deploy Ruby On Rails on Ubuntu 14.04 Trusty Tahr - GoRails (http://cl.ly/VRNn/Screen%20Shot%202014-05-08%20at%205.54.39%20PM.png) Once Digital Ocean has configured your server, check your email to get your password for the new cloud server. (http://cl.ly/RGaf/Screen%20Shot%202013-09-08%20at%206.43.18%20PM.png) You should follow the instructions in the email to login via SSH for the very first time and verify it is working. The first 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. sudo adduser deploy sudo adduser deploy sudo su deploy https://gorails.com/deploy/ubuntu/14.04 3/25 7/12/2014 Deploy Ruby On Rails on Ubuntu 14.04 Trusty Tahr - 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 rest of this tutorial, make sure you are logged in as the deploy user on the server! Installing Ruby Choose the version of Ruby you want to install: 2.1.2 (Recommended) The first step is to install some dependencies for Ruby. sudo apt-get update sudo apt-get install git-core curl zlib1g-dev build-essential libssl-dev libreadl ine-dev libyaml-dev libsqlite3-dev sqlite3 libxml2-dev libxslt1-dev libcurl4-open ssl-dev python-software-properties Next we're going to be installing Ruby using one of three methods. Each have their own benefits, 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 conflict with each other, so choose the one that https://gorails.com/deploy/ubuntu/14.04 4/25 7/12/2014 Deploy Ruby On Rails on Ubuntu 14.04 Trusty Tahr - GoRails sounds the most interesting to you, or go with my suggestion, rbenv. Using rbenv Using rvm From source Installing with rbenv is a simple two step process. First you install rbenv , and then ruby-build : cd git clone git://github.com/sstephenson/rbenv.git .rbenv echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bashrc echo 'eval "$(rbenv init -)"' >> ~/.bashrc exec $SHELL git clone git://github.com/sstephenson/ruby-build.git ~/.rbenv/plugins/ruby-build echo 'export PATH="$HOME/.rbenv/plugins/ruby-build/bin:$PATH"' >> ~/.bashrc exec $SHELL rbenv install 2.1.2 rbenv global 2.1.2 ruby -v The last step is to tell Rubygems not to install the documentation for each package locally echo "gem: --no-ri --no-rdoc" > ~/.gemrc Installing Nginx Phusion is the company that develops Passenger and they recently put out an official 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. https://gorails.com/deploy/ubuntu/14.04 5/25 7/12/2014 Deploy Ruby On Rails on Ubuntu 14.04 Trusty Tahr - GoRails # Install Phusion's PGP key to verify packages gpg --keyserver keyserver.ubuntu.com --recv-keys 561F9B9CAC40B2F7 gpg --armor --export 561F9B9CAC40B2F7 | sudo apt-key add - # Add HTTPS support to APT sudo apt-get install apt-transport-https # Add the passenger repository sudo sh -c "echo 'deb https://oss-binaries.phusionpassenger.com/apt/passenger tru sty main' >> /etc/apt/sources.list.d/passenger.list" sudo chown root: /etc/apt/sources.list.d/passenger.list sudo chmod 600 /etc/apt/sources.list.d/passenger.list sudo apt-get update # Install nginx and passenger sudo apt-get install nginx-full passenger So now we have Nginx and passenger installed. We can manage the Nginx webserver by using the service command: sudo service nginx start 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 configuration 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, find the following lines, and uncomment them: ## # Phusion Passenger ## # Uncomment it if you installed ruby-passenger or ruby-passenger-enterprise ## passenger_root /usr/lib/ruby/vendor_ruby/phusion_passenger/locations.ini; passenger_ruby /usr/bin/ruby; # passenger_ruby /home/deploy/.rvm/wrappers/ruby-2.1.2/ruby; # If use use rvm, be sure to change the version number # passenger_ruby /home/deploy/.rbenv/shims/ruby; # If you use rbenv The passenger_ruby is the important line here. We want this to match the output of which ruby in terminal. This is probably correct already if you installed Ruby from source. If you used rbenv, it would look something like this /home/deploy/.rbenv/shims/ruby . https://gorails.com/deploy/ubuntu/14.04 6/25 7/12/2014 Deploy Ruby On Rails on Ubuntu 14.04 Trusty Tahr - GoRails Once you've changed passenger_ruby to use the right version Ruby, you can run sudo service nginx restart to restart Nginx with the new Passenger configuration. 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 configuring Nginx to serve up your Rails application. MySQL and PostgreSQL Database Setup 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: sudo apt-get install mysql-server mysql-client libmysqlclient-dev 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.
Details
-
File Typepdf
-
Upload Time-
-
Content LanguagesEnglish
-
Upload UserAnonymous/Not logged-in
-
File Pages25 Page
-
File Size-