1 00:00:00,220 --> 00:00:03,630 So we've got engine X forwarding our apps traffic to Puma, 2 00:00:03,630 --> 00:00:06,560 now let's get it working with unicorn instead. 3 00:00:06,560 --> 00:00:08,510 So I'm going to quit out of the rail server here. 4 00:00:10,430 --> 00:00:12,810 Unicorn is distributed as a Ruby gem, so 5 00:00:12,810 --> 00:00:15,830 to install it am going to edit our applications gem file. 6 00:00:18,570 --> 00:00:21,570 Down here at the bottom I'm going to add a new production group. 7 00:00:22,880 --> 00:00:26,030 So that this only gets installed on our production server. 8 00:00:26,030 --> 00:00:30,190 So group symbol production as well as block. 9 00:00:33,250 --> 00:00:36,530 And in this block we will specify the gems we want to install in production so 10 00:00:36,530 --> 00:00:38,660 we want to install the gym unicorn. 11 00:00:42,380 --> 00:00:46,140 And we're gonna use the total walk of syntax here to lock that 12 00:00:46,140 --> 00:00:51,070 two minor version 5.2, that's the current one at the time of recording. 13 00:00:52,290 --> 00:00:56,832 Okay Ctrl all to write that out for in the filename Ctrl+x to exit and 14 00:00:56,832 --> 00:01:00,668 now we need to run bundle install to actually install it. 15 00:01:04,229 --> 00:01:06,972 It will download Unicorn and all its dependencies. 16 00:01:09,177 --> 00:01:12,820 And when it returns to the prompts, Unicorn will be installed. 17 00:01:12,820 --> 00:01:15,760 Now, we need to set up a configuration file for Unicorn. 18 00:01:15,760 --> 00:01:20,510 Just like with Engine X, we're going to set it up within our config subdirectory. 19 00:01:20,510 --> 00:01:25,870 So we use the nano editor and will create a new file in the config directory, 20 00:01:25,870 --> 00:01:30,610 we're going to name it unicorn.rb. 21 00:01:30,610 --> 00:01:36,200 We name the other file nginx.conf because it was in nginxs format but 22 00:01:36,200 --> 00:01:40,850 unicorn configuration files are plain ruby files so we give this one a .rb extension. 23 00:01:42,860 --> 00:01:45,910 Okay, so we've got a few directives that we need to set up here first. 24 00:01:45,910 --> 00:01:54,510 The first is going to be the working directory option working_directory. 25 00:01:54,510 --> 00:01:59,410 Again this is basically a Ruby method call we're going to pass at string and 26 00:01:59,410 --> 00:02:01,475 we'll specify the path to our Rails app. 27 00:02:01,475 --> 00:02:07,650 So that's home/deploy/guestbook. 28 00:02:11,676 --> 00:02:15,360 We're gonna need that route a few more times, so I'm gonna copy that here. 29 00:02:18,095 --> 00:02:22,318 Then we need to specify a file that unicorn will write out its process ID to 30 00:02:22,318 --> 00:02:25,240 so that we can shut the server down if we need to. 31 00:02:25,240 --> 00:02:27,587 So I'm going to use the pid directive and 32 00:02:27,587 --> 00:02:32,090 specify that it should to a file within our guest book app directory. 33 00:02:33,600 --> 00:02:38,615 Within a PID's sub directory and we're gonna name that fur 34 00:02:38,615 --> 00:02:44,200 unicorn.pid. 35 00:02:44,200 --> 00:02:47,160 Next we have a couple options for logging. 36 00:02:48,600 --> 00:02:51,840 So I'm going to say that errors should be logged to 37 00:02:51,840 --> 00:02:55,700 a particular path with stderr_path. 38 00:02:55,700 --> 00:03:00,960 We're going to have it write to a file in our guestbook app directory again, 39 00:03:02,130 --> 00:03:04,210 within a unicorn sub directory. 40 00:03:07,010 --> 00:03:12,056 We'll name that unicorn.log And 41 00:03:12,056 --> 00:03:15,601 then we'll use the STD out path directive to have it 42 00:03:15,601 --> 00:03:20,060 write standard output to the same location to the same log file. 43 00:03:21,630 --> 00:03:23,520 Okay, this next part is crucial. 44 00:03:23,520 --> 00:03:28,580 So engine X is going to communicate with unicorn and Gen X 45 00:03:28,580 --> 00:03:34,049 is one process running on our Linux server and the Unicorn server is another process. 46 00:03:35,260 --> 00:03:39,340 In Linux processes communicate with each other through sockets which is kind of 47 00:03:39,340 --> 00:03:43,210 like a file except that one process writes into this virtual file and 48 00:03:43,210 --> 00:03:45,750 another process reads out of it. 49 00:03:45,750 --> 00:03:49,630 So we're going to give unicorn the listen directive and 50 00:03:49,630 --> 00:03:51,730 tell it where it should listen for requests. 51 00:03:53,050 --> 00:03:55,900 And we're gonna give it the name of the socket to listen on. 52 00:03:55,900 --> 00:04:02,120 So we'll create a string that points to a path in the tmp root directory. 53 00:04:02,120 --> 00:04:10,212 And we're going to name that socket unicorn.guestbook.sock. 54 00:04:10,212 --> 00:04:14,949 Okay, so there's the socket that unicorn is going to listen on will Set up ngenx to 55 00:04:14,949 --> 00:04:16,610 write to that socket later. 56 00:04:18,810 --> 00:04:22,160 Then we need to specify how many unicorn processes it should start up. 57 00:04:22,160 --> 00:04:29,060 For right now we're just going to say worker_processes and 58 00:04:29,060 --> 00:04:31,040 we'll just give it a number 2 for right now. 59 00:04:32,830 --> 00:04:36,536 And finally we should specify how long it should give one of those processes to 60 00:04:36,536 --> 00:04:38,199 respond before it terminates it. 61 00:04:38,199 --> 00:04:42,507 Be that with the timeout directive and we'll set that to 30 seconds for 62 00:04:42,507 --> 00:04:43,220 right now. 63 00:04:47,440 --> 00:04:48,980 Okay, let's save the file an exit. 64 00:04:50,540 --> 00:04:53,750 Okay and with that configuration in place we can try running the unicorn. 65 00:04:53,750 --> 00:04:56,370 We're going to want to do that in the production environment, so 66 00:04:56,370 --> 00:05:00,600 I'll specify rails equals production. 67 00:05:05,220 --> 00:05:06,740 Since we installed the unicorn through, 68 00:05:06,740 --> 00:05:10,980 bundler we're going to want to run it using bundlers So we use bundle exec. 69 00:05:12,770 --> 00:05:14,359 The executable is named unicorn. 70 00:05:17,241 --> 00:05:21,998 And next we're going to use the -c command line flag to specify a configuration 71 00:05:21,998 --> 00:05:23,280 file it should load. 72 00:05:25,450 --> 00:05:31,350 We're going to want to use the config directory unicorn.rb And 73 00:05:31,350 --> 00:05:35,680 finally we'll use dash capital D to specify that it should run as a demon, 74 00:05:35,680 --> 00:05:38,440 that is, it should continue running in the background. 75 00:05:39,750 --> 00:05:41,640 If we try running this, we'll see an error. 76 00:05:43,560 --> 00:05:47,380 It says that the directory for the P I D file isn't writeable and 77 00:05:47,380 --> 00:05:50,590 that's because we forgot to create that directory, so let's do that now. 78 00:05:52,030 --> 00:05:56,700 So, we'll create a, we'll make a directory and pids. 79 00:05:56,700 --> 00:05:58,280 Here within our rails app directory. 80 00:06:00,710 --> 00:06:02,070 If we try running it again and 81 00:06:02,070 --> 00:06:06,770 will fail again because it says the directory for unicorn.log isn't writable. 82 00:06:06,770 --> 00:06:09,270 We forgot to create a unicorn subdirectory as well. 83 00:06:10,840 --> 00:06:12,660 So, add directory unicorn. 84 00:06:14,400 --> 00:06:15,711 Okay let's try one more time. 85 00:06:19,341 --> 00:06:21,250 And this time it kicks off successfully. 86 00:06:22,530 --> 00:06:24,060 We've got unicorn running now but 87 00:06:24,060 --> 00:06:27,370 if we try to visit our Rails app we'll see 502 Bad Gateway again. 88 00:06:27,370 --> 00:06:31,579 This is because engine x isn't set up to communicate with unicorn via the socket we 89 00:06:31,579 --> 00:06:34,880 told unicorn to listen on so we're going to need to fix that now. 90 00:06:36,940 --> 00:06:41,945 So I'm going to go back into the nginx configuration file. 91 00:06:41,945 --> 00:06:45,420 config/nginxx.conf. 92 00:06:45,420 --> 00:06:50,500 And specify a socket that nginx should write requests out to. 93 00:06:50,500 --> 00:06:54,110 We're going to use the upstream directive. 94 00:06:54,110 --> 00:06:56,600 We're going to name this upstream location unicorn. 95 00:06:58,780 --> 00:07:00,170 And then options for 96 00:07:00,170 --> 00:07:04,970 that upstream location are here within brackets, curly brackets. 97 00:07:04,970 --> 00:07:08,310 And here we're going to use the server directive and 98 00:07:08,310 --> 00:07:12,370 we're going to specify the Unix socket that it should write out to. 99 00:07:12,370 --> 00:07:21,054 So unix:/tmp/unicorn.guestbook.sock. 100 00:07:22,510 --> 00:07:26,720 Which is the same path that we specified within the unicorn configuration. 101 00:07:28,150 --> 00:07:33,030 And here at the end of the line we're going to add one more option, 102 00:07:33,030 --> 00:07:38,470 we're going to say fail underscore timeout equals zero. 103 00:07:40,230 --> 00:07:44,570 That fail timeout equals zero causes engine X to always retry request if 104 00:07:44,570 --> 00:07:45,260 it fails. 105 00:07:45,260 --> 00:07:47,900 We need this because a unicorn worker might get shut 106 00:07:47,900 --> 00:07:51,000 down by the master process if it takes too long to respond and 107 00:07:51,000 --> 00:07:54,450 we don't want to lose the request so that will cause engine X to retry it. 108 00:07:56,050 --> 00:08:00,050 And now instead of forwarding requests that it gets onto local host port 3000, 109 00:08:00,050 --> 00:08:04,490 we want to get it to forwarded on to our upstream location. 110 00:08:04,490 --> 00:08:08,120 So we're going to have it forward it to another HTTP server. 111 00:08:09,250 --> 00:08:12,950 But we're going to have it go to the unicorn upstream location. 112 00:08:12,950 --> 00:08:17,922 So we use the same name as our upstream directive http://unicorn. 113 00:08:17,922 --> 00:08:22,053 Okay write that out save it, and 114 00:08:22,053 --> 00:08:26,960 as always we need to restart engine x. 115 00:08:28,710 --> 00:08:30,440 It will want our sudo password. 116 00:08:35,170 --> 00:08:39,280 And now if we go to our host IP and try to reload the root path. 117 00:08:39,280 --> 00:08:40,190 We'll see our Rails app. 118 00:08:41,650 --> 00:08:45,620 Nginx is successfully communicating with Unicorn using the Unix socket.