Bummer! This is just a preview. You need to be signed in with a Pro account to view the entire video.
Cloning a Rails App9:51 with Jay McGavren
Now it's time to deploy that app you've been working on in development to the server. The easiest and best way to do that is to clone the source code from version control (e.g. Git).
Run these commands while logged in as the deployment user on your server:
$ echo 'gem: --no-document' >> ~/.gemrc $ git clone firstname.lastname@example.org:treehouse-projects/rails-guestbook.git $ gem install bundler $ cd rails-guestbook $ bundle install $ RAILS_ENV=production bin/rails db:setup
We need to store the Rails secret key as an environment variable. The
rbenv-vars library will let us do so. On your server, run:
$ git clone https://github.com/sstephenson/rbenv-vars.git ~/.rbenv/plugins/rbenv-vars $ bin/rails secret
Copy the secret key it generates, then paste it in place of
<secret_key> in the command below:
$ echo 'SECRET_KEY_BASE=<secret_key>' >> .rbenv-vars
You should be ready to run your app now.
Running the app
Run this command while logged in as the deployment user on your server:
$ RAILS_ENV=production bin/rails server
Finding your server's public IP address
Then, in your browser address bar, you'll need to type an IP address and port number, separated by a colon:
<address>:3000. (The Rails server will be running on port 3000.) For the address portion, you'll need to find the public IP address of your server.
If you're running the server locally as a virtual machine, you'll need to forward traffic from port 3000 on your own computer to port 3000 on the virtual machine. This video includes additional information about forwarding ports in VirtualBox. Then you'll need to connect to your own computer from your browser using the special IP address
If you're running an actual server with a web hosting company, consult their online documentation to learn how to look up your server's IP address. Most hosting companies offer a web-based control panel where you can configure your servers, and the IP address should be listed there.
Let's run a Rails app on our server to test it out. 0:00 We're going to clone a Rails ap to the server via Git. 0:03 And then, use Bungler to install the specific version of 0:05 the rails gem our app needs. 0:08 Along with all the apps other gem dependencies. 0:09 We're going to be installing a large number of gems here. 0:11 And since this is a production server, 0:15 we're not going to need the documentation that comes along with them. 0:17 So. let's make a quick change to our Gem 0:20 configuration here. 0:21 I'm going to use the Nano editor to edit the hidden gem RC file. 0:23 That's dot gem RC in our home directory. 0:29 That command will create the file if it doesn't exist and 0:33 I'm going to add a line to it. 0:38 Gem, colon double dash no-document. 0:41 And that'll add a flag to the command line. 0:42 That gets used when it's installing gems. 0:45 As usual, hit Ctrl+O to write out the file and Ctrl+X to exit. 0:49 Now, let's visit the site for the project will be cloning. 0:55 It's a project here on Github. 0:58 The link's in the show notes if you want it and 1:00 I'm going to get its clone URL .I'm not going to use the SSH URL. 1:04 I'm going to use the Https URL. 1:09 I'm going to select that, copy it. 1:11 Switch back to my terminal and I'm going to run the command git clone and the URL. 1:16 That will clone the app down from Github. 1:24 In a moment, we're going to change it to the apps directory and 1:27 use bundler to install its dependent gems. 1:29 But first, we're going to need the bundler gem installed. 1:32 So, we'll do that with the command gem. 1:35 Install bundler. 1:37 No need to put the word suedo before it. 1:41 Hit return. 1:46 It'll install bundler and then, 1:47 we can change the app directory and run bundle install. 1:49 It will take a few minutes to download rails and all its dependent gems. 2:01 And when it goes back to the command line, your install will be complete. 2:11 Since we've never run this Rails app in the production environment before, 2:15 the database doesn't exist there. 2:18 Also, since this is a brand new instance of the rails app, 2:20 we should load the schema for the database. 2:23 We shouldn't create it by running migrations. 2:25 Rails has a sub command that'll do all that for us. 2:28 First, we need to ensure that it's running in the production environment. 2:31 Not development or testing. 2:34 So, we do that by specifying the RAILS_ENV 2:36 environment variable here on the command line. 2:40 And we're going to set that to the string production. 2:47 Type a space and then right after that, we're going to run the bin/rails command. 2:50 So, this set up here will run the bin/rails command 2:55 with the rails nth environment variable set to production. 2:58 With all that set, we're going to run the sub command db:setup. 3:03 Db:setup is like several other commands rolled into one. 3:08 It'll create a database for us and then, it'll automatically load the schema for 3:11 that database. 3:15 Hit return to run it and 3:18 it will create a database for us and load the schema into it. 3:20 Okay with all that done, our app should be ready to test. 3:24 Let's give it a try. 3:27 We're going to run the exact same command. 3:28 RAILS EMV production bin slash rails, 3:31 but we're going to run the server sub-command this time. 3:34 It'll start up and note, that it's running on port 3000. 3:40 We'll be showing you how to hook it into a web server 3:43 running on port 80 in a later workshop. 3:47 But we'll just connect to it on port 3000 for now. 3:49 In a browser address bar, 3:52 type the public IP address of your server, followed by the port. 3:54 In my case, I'm running my server in VirtualBox on my local machine. 4:00 So, I'll connect 127.0.0.1:3000 So that's 127.0.0.1:3000. 4:05 For info on finding your server's public 4:10 IP address, see the teacher's notes. 4:15 So, I hit Enter to attempt to load the page but what I see is, 4:19 127.0.0.1 refused to connect. 4:23 There's a variety of reasons this can happen. 4:27 You should see the teacher's notes and it's for some troubleshooting help. 4:29 But in my case, it's because port 3000 on my virtual machine can't be reached. 4:32 So, I'm going to go to virtual box and bring up the settings for 4:37 my virtual server. 4:41 Go to the network tab and expand the advanced settings and 4:43 click on the port forwarding button. 4:48 And we currently have rules set up to forward port 80 to port 8080 on the host. 4:51 As well as port 22 to port 2222, but we don't have anything for port 3000. 4:58 So, let's go ahead and add that one now. 5:03 I'm going to go and give this rule this rule a name of H-T-T-P for development. 5:15 Protocol as always is, going to be T-C-P. 5:16 I won't specify a host I-P, so that'll match any host. 5:19 And it's actually okay if I forward to the exact same host on the port 5:24 that I'm getting from the guest. 5:28 Because there isn't going to be a service already running on port three thousand in 5:30 my case. 5:33 So, I'll forward all traffic received on host port 3,000 to port 5:34 3,000 on the guest. 5:39 Click OK and OK again to save the changes and they should take effect immediately. 5:41 So, let me go back to my browser and try sending the request again to port 3,000. 5:46 I actually connect successfully this time, but I see the error. 5:51 An unhandled low level error occurred. 5:55 The application logs may have details. 5:57 If I go back to my terminal, we'll see the rails log there. 6:01 And we see a RuntimeError. 6:05 Missing secret_key_base for the production environment. 6:06 Set this value in config/secret.yml. 6:10 So, this looks like something we're going to need to fix in our configuration. 6:15 Let's take a look at that file it recommends. 6:18 So, we're going to fire up the Nano editor and 6:21 we're going to edit the file it requests. 6:23 Config, directory, 6:25 secrets.yml and 6:28 down here at the bottom, we see a warning in the comments. 6:35 Do not keep production secrets in the repository. 6:37 Instead, read the values from the environment. 6:40 By repository, it means our GitHub, our source code repo. 6:43 And it suggests that instead, we should read values from an environment variable. 6:48 That's precisely what it's doing here. 6:52 It's got the secret key base setting for the production environment. 6:54 Just like it does for the development and test environments. 6:57 But it's using an ERB tag to load it from an environment variable. 7:00 So all we have to do is, set that environment variable and 7:06 it will be loaded here. 7:09 So, I'm going to go ahead an exit out of this without making any changes. 7:11 So, first we need to generate the secret key. 7:15 Then, we're going to need a way to store it in an environment variable without 7:17 checking any kind of script into our Git repository. 7:21 So we're going to need a place to store that environment variable. 7:24 There's an RBM plugin named RBM VARs that can help us. 7:28 Here's its page on Github. 7:33 Basically as its description says, RBM Fars is a plugin for our RBM. 7:38 That lets you set global and 7:42 project specific environment variables before spawning Ruby processes. 7:43 Sounds like just what we need. 7:48 There's a command here that you can copy and paste into a terminal to install it. 7:50 So, let's copy it, Switch to our server terminal and paste it here. 7:54 It'll clone the rbenv-vars plugin repository into our rbenv plugins 8:02 directory and after that, it will be installed. 8:07 Now, let's run the command that will generate a rail secret key for 8:12 a spin slash rails secret. 8:15 It will generate this big long hexadecimal string, which will copy to our clipboard. 8:17 Environment variables for 8:27 rbrnv fars get stored in a hidden conflict file within your Rails apps directory. 8:28 So, let's edit file now. 8:33 We'll run our nano editor and 8:35 will edit the file name to dot rbenv dash bars. 8:38 The dot at the start of course, 8:43 keeps the files hidden from an ordinary directory listing. 8:45 Hit inner, it'll create the file, if it doesn't exist already. 8:49 And the format for 8:53 this file is just the name of the environment variable you want to set. 8:54 Which in this case is, secret underscore key underscore base. 8:59 All caps, an equal sign and then, 9:03 the value you want to assign to it, which we'll paste here. 9:06 Make sure there are no spaces surrounding the equal sign. 9:10 Okay, that should be all we need, so let's hit control O to write out the file. 9:14 Hit enter to confirm the file name and hit Control x to exit. 9:19 Now, let's try running our real server again. 9:23 I'll just bring up the command from before, 9:26 RAILs_ENV=production bin/rails server. 9:30 Okay, and let's go back to our browser and try reloading the page again. 9:34 The page loads successfully. 9:41 We're also able to click the link to create a new signature. 9:43 And it will be saved to the database. 9:49
You need to sign up for Treehouse in order to download course files.Sign up