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