1 00:00:00,310 --> 00:00:03,690 With the app deployed, our next step is to start the web server. 2 00:00:03,690 --> 00:00:07,570 The guest book app is set up to run using nginx as a reverse proxy and 3 00:00:07,570 --> 00:00:09,320 unicorn as it's web server. 4 00:00:09,320 --> 00:00:10,707 We need to tweak our nginx and 5 00:00:10,707 --> 00:00:13,920 unicorn configurations before we can start them up. 6 00:00:13,920 --> 00:00:17,220 So first I'm going to go into my nginx config, 7 00:00:17,220 --> 00:00:21,850 which I've set up here in the config directory under nginx.conf. 8 00:00:21,850 --> 00:00:25,720 And it's got a configuration item that needs to point to the root 9 00:00:25,720 --> 00:00:27,180 path of our Rails app. 10 00:00:27,180 --> 00:00:32,780 That used to be home/deploy/guestbook, but now, the Capistrano is deploying our app. 11 00:00:32,780 --> 00:00:35,960 It's going to be home/deploy/guestbook/current. 12 00:00:35,960 --> 00:00:39,751 And then it can still refer to the public subdirectory within that. 13 00:00:42,194 --> 00:00:46,042 Let me copy that, since we're going to have to set up the current subdirectory 14 00:00:46,042 --> 00:00:48,310 in several other places as well. 15 00:00:48,310 --> 00:00:51,230 That's the only change we have to make for our nginx config, though. 16 00:00:51,230 --> 00:00:57,315 I'll close out of that, now let me go into the unicorn.rb file in the same directory. 17 00:00:57,315 --> 00:01:00,994 And we're gonna change all the occurrences of 18 00:01:00,994 --> 00:01:06,569 home/deploy/guestbook to home/deploy/guestbook/current. 19 00:01:08,238 --> 00:01:11,390 All the sub directories can remain the same however. 20 00:01:11,390 --> 00:01:16,640 Lastly, I've added a shell script here in config unicorn_init.sh that can start, 21 00:01:16,640 --> 00:01:18,820 stop, or restart unicorn for us. 22 00:01:18,820 --> 00:01:22,750 See the teacher's notes if you'd like to set up a copy of this script for yourself. 23 00:01:22,750 --> 00:01:23,851 At the top of the script, 24 00:01:23,851 --> 00:01:26,801 there's a bunch of environment variables that can be custified. 25 00:01:30,214 --> 00:01:33,925 So we just need to set the APP_ROOT variable to point to the path on 26 00:01:33,925 --> 00:01:36,350 the server where our app is installed. 27 00:01:36,350 --> 00:01:43,060 We're going to point it to the same location that we did in nginx.com for 28 00:01:43,060 --> 00:01:49,560 the unicorn.rb, home/deploy/guestbook/current. 29 00:01:49,560 --> 00:01:51,008 So I've saved all these files. 30 00:01:53,637 --> 00:01:59,077 And I'm gonna commit all of them to git, 31 00:01:59,077 --> 00:02:03,399 git add config/nginx.conf, 32 00:02:03,399 --> 00:02:08,201 git add config/unicorn.rb, and 33 00:02:08,201 --> 00:02:13,495 git add config/ unicorn_init.sh. 34 00:02:18,637 --> 00:02:24,035 Commit them with the message update paths in configuration. 35 00:02:26,526 --> 00:02:30,843 And then I'm gonna push that commit out to our remote repository so 36 00:02:30,843 --> 00:02:33,940 that we can deploy it to our production server. 37 00:02:36,700 --> 00:02:39,240 Now, let's run a Capistrano deployment again, but 38 00:02:39,240 --> 00:02:42,460 not to deploy initial, this is going to be a subsequent deploy. 39 00:02:42,460 --> 00:02:45,128 So, bundle exec cap production deploy. 40 00:02:47,814 --> 00:02:50,229 Okay, and with our deployment complete, 41 00:02:50,229 --> 00:02:53,990 the updated configuration files are setup on the server. 42 00:02:53,990 --> 00:02:57,200 Now we need to just link them into the places that nginx and 43 00:02:57,200 --> 00:02:58,860 unicorn will actually look for them. 44 00:02:59,990 --> 00:03:02,265 So I'm going to SSH onto the server. 45 00:03:06,696 --> 00:03:11,769 And first let's set up nginx, we're going to be altering a directory that you need, 46 00:03:11,769 --> 00:03:16,300 administrative privileges to change so I'm going to run the sudo command. 47 00:03:16,300 --> 00:03:21,200 And using sudo, I'm going to remove the default nginx site, so 48 00:03:21,200 --> 00:03:30,490 that's going to be stored in etc/nginx/sites-enabled, 49 00:03:30,490 --> 00:03:35,530 and it it'll be the default site inside that directory. 50 00:03:35,530 --> 00:03:38,082 I'll need to provide my deploy password. 51 00:03:40,501 --> 00:03:42,966 And now with the default NGINX site gone, 52 00:03:42,966 --> 00:03:46,130 we can link our custom site into its place. 53 00:03:46,130 --> 00:03:47,880 So I'm going to run some sudo command. 54 00:03:47,880 --> 00:03:52,890 And this is going to set us up symbolic link using the ln command with 55 00:03:52,890 --> 00:03:56,010 the dash s, command line flag. 56 00:03:56,010 --> 00:03:58,760 So first we're going to need the path that we're linking from, 57 00:03:58,760 --> 00:04:02,460 that's gonna be our nginx configuration within our Rails app directory. 58 00:04:02,460 --> 00:04:07,670 So that will be in home/deploy/guestbook/current and 59 00:04:09,640 --> 00:04:14,330 then within the cofig sub directory, nginx.conf. 60 00:04:14,330 --> 00:04:19,051 That's going to link into the etc/nginx directory in 61 00:04:19,051 --> 00:04:26,280 the sites-enabled subdirectory, just like the default site that we just deleted and 62 00:04:26,280 --> 00:04:29,730 I'm going to create a link within that directory named guestbook. 63 00:04:31,840 --> 00:04:34,760 Hit Enter and that will link our config and 64 00:04:34,760 --> 00:04:39,790 the next time we restart nginx with sudo service 65 00:04:39,790 --> 00:04:45,260 nginx restart, that should load our config instead. 66 00:04:45,260 --> 00:04:49,250 Next we need to insure that our unicorn gets restarted each time the server 67 00:04:49,250 --> 00:04:49,830 restarts. 68 00:04:49,830 --> 00:04:54,410 So we're going to link it into a directory used by services in Linux. 69 00:04:54,410 --> 00:04:59,570 We'll do that with sudo ln -s, again we're going to 70 00:04:59,570 --> 00:05:04,970 specify our file here within our app deployment directory. 71 00:05:04,970 --> 00:05:11,252 So home/deploy/guestbook/current/config/unic- 72 00:05:11,252 --> 00:05:13,028 orn_init.sh. 73 00:05:13,028 --> 00:05:21,020 We're going to link that into a special Linux directory called etc/init.d/. 74 00:05:21,020 --> 00:05:24,430 Services that run at system startup get linked to this director. 75 00:05:24,430 --> 00:05:27,624 And we're going to create a file, 76 00:05:27,624 --> 00:05:33,033 a link within this directory named unicorn_guestbook. 77 00:05:35,934 --> 00:05:40,232 Then we need to tell our Linux system about that new link we just created so 78 00:05:40,232 --> 00:05:43,210 that it will reload it when the system restarts. 79 00:05:43,210 --> 00:05:47,660 So I'm going to say sudo, and 80 00:05:47,660 --> 00:05:54,257 then I'll run the command update-rc.d. 81 00:05:57,273 --> 00:06:03,131 I'll specify the name of the link we just created, which is unicorn_guestbook. 82 00:06:07,132 --> 00:06:09,162 And I'll give it the argument defaults, 83 00:06:09,162 --> 00:06:11,950 which means that it should run when the system restarts. 84 00:06:13,840 --> 00:06:17,280 And now, let's use that unicorn init script to restart unicorn. 85 00:06:17,280 --> 00:06:22,400 So, I'll say sudo path to the Rails app which is 86 00:06:22,400 --> 00:06:30,211 home/deploy/guestbook/current/config/unic- orn_init.sh, 87 00:06:30,211 --> 00:06:35,337 and I'm going to pass the restart subcommand. 88 00:06:39,052 --> 00:06:42,990 With nginx and unicorn configured, our site should be up and running. 89 00:06:42,990 --> 00:06:47,934 But if we paste the IP address into our browser, we'll we're getting a 500 error. 90 00:06:50,890 --> 00:06:52,745 So, here on the server, let's take a look and 91 00:06:52,745 --> 00:06:54,510 see if we can figure out what's going on. 92 00:06:54,510 --> 00:06:57,550 I'm gonna take a look first at the production logs. 93 00:07:00,370 --> 00:07:04,270 And I don't really see an error in there that could explain what's going on. 94 00:07:04,270 --> 00:07:06,690 So, let's take a look at the unicorn log as well. 95 00:07:07,820 --> 00:07:13,470 That's configured to write to the unicorn subdirectory in a file called unicorn.log. 96 00:07:13,470 --> 00:07:17,180 We'll scroll down here to the bottom. 97 00:07:20,090 --> 00:07:23,850 And then if we scroll back up we'll see the error missing secret key base for 98 00:07:23,850 --> 00:07:25,750 the production environment. 99 00:07:25,750 --> 00:07:29,740 We need to set this value in config/secrets.yml. 100 00:07:29,740 --> 00:07:35,222 If you'll recall though, our config/secrets.yml, Simply 101 00:07:35,222 --> 00:07:40,700 loads the production secret_key_base value in from an environment variable. 102 00:07:40,700 --> 00:07:45,980 And if you recall our prior workshop, we set up rbenv-vars with the intention 103 00:07:45,980 --> 00:07:51,000 that it would let us load in our configuration variables from a file. 104 00:07:52,410 --> 00:07:56,490 But there is no rbenv-vars file here in guessbook/current, 105 00:07:56,490 --> 00:08:00,550 we could create one here but it'll get wiped out on the next deploy. 106 00:08:00,550 --> 00:08:06,340 So instead, we're going to switch to the home guestbook/shared directory and 107 00:08:06,340 --> 00:08:07,730 create it there. 108 00:08:07,730 --> 00:08:11,830 The shared directory contains files that need to be preserved between deployments. 109 00:08:11,830 --> 00:08:17,950 So first, let's change back to the Rails app directory real quick so 110 00:08:17,950 --> 00:08:25,020 that we can run the command, bin/rails secret. 111 00:08:25,020 --> 00:08:29,375 That'll generate a secret file that we can set in our environment variable. 112 00:08:33,287 --> 00:08:35,760 Then we'll change back to the shared directory. 113 00:08:39,190 --> 00:08:43,274 And we'll use the nano editor to create an rbenv-vars file. 114 00:08:46,446 --> 00:08:50,758 Within that file, we're going to set 115 00:08:50,758 --> 00:08:56,790 the SECRET_KEY_BASE environment variable. 116 00:08:56,790 --> 00:09:02,640 We put an equal sign after the name of the variable we want to set with no space, 117 00:09:02,640 --> 00:09:04,710 characters on either side of it. 118 00:09:04,710 --> 00:09:07,950 And then we paste in the value that we want to set it to which is gonna be our 119 00:09:07,950 --> 00:09:08,545 secret key. 120 00:09:11,503 --> 00:09:15,440 The nano editor requires a little o when we write the file out. 121 00:09:15,440 --> 00:09:19,670 We can print the file name we want to write, and then we can hit CTRL X to exit. 122 00:09:19,670 --> 00:09:23,670 So now we have an rbenv-vars file that will be preserved between deployments, but 123 00:09:23,670 --> 00:09:28,410 unfortunately it's not in the current directory where we need it to be. 124 00:09:28,410 --> 00:09:31,484 To fix this, we're going to exit the shell here on our server so 125 00:09:31,484 --> 00:09:33,693 that we get back to our development machine. 126 00:09:36,967 --> 00:09:41,389 And then here in my editor, I'm going to make a change to the config, 127 00:09:41,389 --> 00:09:42,627 deploy.rb file. 128 00:09:43,960 --> 00:09:47,010 Down here, you can see a commented out section that's devoted 129 00:09:47,010 --> 00:09:49,540 to setting the linked files variable. 130 00:09:51,220 --> 00:09:54,400 The default value for linked files is an empty array, and 131 00:09:54,400 --> 00:09:58,960 then here is a call to the append method for Capistrano, 132 00:09:58,960 --> 00:10:03,710 which takes a variable and that variable is assumed to contain an array. 133 00:10:05,220 --> 00:10:09,890 Any subsequent arguments to append are values that are gonna get appended 134 00:10:09,890 --> 00:10:11,130 to the array. 135 00:10:11,130 --> 00:10:13,080 So since the above line is commented out, 136 00:10:13,080 --> 00:10:17,190 our linked files variable should be completely empty already. 137 00:10:17,190 --> 00:10:22,370 We're going to make another call to append here, and we're going to append 138 00:10:22,370 --> 00:10:29,560 to the linked_files array, and we're going to add one value on to it. 139 00:10:29,560 --> 00:10:35,947 We want it to link a file from the shared directory named .rbenv-vars. 140 00:10:35,947 --> 00:10:40,852 So let's save and close this, and the next time we do a deploy, it's going to create 141 00:10:40,852 --> 00:10:45,066 a symbolic link from the shared directory into the current directory for 142 00:10:45,066 --> 00:10:49,834 the current deploy, it'll be just like our rbenv-vars file would sit in there in 143 00:10:49,834 --> 00:10:52,020 the current directory. 144 00:10:52,020 --> 00:10:55,929 So let's do that deploy now, bundle exec cap 145 00:10:55,929 --> 00:11:02,150 production deploy. 146 00:11:02,150 --> 00:11:06,645 And here you can see that it's creating a symbolic link 147 00:11:06,645 --> 00:11:11,055 from the rbenv-vars file into the current release. 148 00:11:14,902 --> 00:11:17,625 So now with that secret key environment variable in place, 149 00:11:17,625 --> 00:11:19,920 we should be able to try restarting unicorn. 150 00:11:19,920 --> 00:11:25,966 Let's connect to our server via SSH, and try re running that restart command. 151 00:11:32,958 --> 00:11:38,210 Now we'll go back to the browser, hit reload, and we get to our guestbook app. 152 00:11:38,210 --> 00:11:41,970 Our app is successfully deployed, and nginx and unicorn are up and running.