Heads up! To view this whole video, sign in with your Courses Plus account or enroll in your free 7-day trial. Sign In Enroll
Start a free Courses trial
to watch this video
Next we'll show you how to install Unicorn on your server, and configure your Rails app to use Unicorn instead of Puma in production.
On your server, in your Rails app directory, add the following to your Gemfile
:
group :production do
gem 'unicorn', '~> 5.2'
end
Next, edit config/unicorn.rb
:
working_directory "/home/deploy/guestbook"
pid "/home/deploy/guestbook/tmp/pids/unicorn.pid"
stderr_path "/home/deploy/guestbook/unicorn/unicorn.log"
stdout_path "/home/deploy/guestbook/unicorn/unicorn.log"
listen "/tmp/unicorn.guestbook.sock"
worker_processes 2
timeout 30
Modify config/nginx.conf
to read:
upstream unicorn {
server unix:/tmp/unicorn.guestbook.sock fail_timeout=0;
}
server {
listen 80 default;
root /home/deploy/guestbook/public;
try_files $uri/index.html $uri @unicorn;
location @unicorn {
proxy_pass http://unicorn;
}
}
Run the following from your Rails app directory:
bundle install
mkdir unicorn
sudo service nginx restart
RAILS_ENV=production bundle exec unicorn -c config/unicorn.rb -D
At this point, if you type your server's address into your browser, you should see your app, but this time it will be running under Unicorn instead of Puma.
So we've got engine X forwarding our apps traffic to Puma, 0:00 now let's get it working with unicorn instead. 0:03 So I'm going to quit out of the rail server here. 0:06 Unicorn is distributed as a Ruby gem, so 0:10 to install it am going to edit our applications gem file. 0:12 Down here at the bottom I'm going to add a new production group. 0:18 So that this only gets installed on our production server. 0:22 So group symbol production as well as block. 0:26 And in this block we will specify the gems we want to install in production so 0:33 we want to install the gym unicorn. 0:36 And we're gonna use the total walk of syntax here to lock that 0:42 two minor version 5.2, that's the current one at the time of recording. 0:46 Okay Ctrl all to write that out for in the filename Ctrl+x to exit and 0:52 now we need to run bundle install to actually install it. 0:56 It will download Unicorn and all its dependencies. 1:04 And when it returns to the prompts, Unicorn will be installed. 1:09 Now, we need to set up a configuration file for Unicorn. 1:12 Just like with Engine X, we're going to set it up within our config subdirectory. 1:15 So we use the nano editor and will create a new file in the config directory, 1:20 we're going to name it unicorn.rb. 1:25 We name the other file nginx.conf because it was in nginxs format but 1:30 unicorn configuration files are plain ruby files so we give this one a .rb extension. 1:36 Okay, so we've got a few directives that we need to set up here first. 1:42 The first is going to be the working directory option working_directory. 1:45 Again this is basically a Ruby method call we're going to pass at string and 1:54 we'll specify the path to our Rails app. 1:59 So that's home/deploy/guestbook. 2:01 We're gonna need that route a few more times, so I'm gonna copy that here. 2:11 Then we need to specify a file that unicorn will write out its process ID to 2:18 so that we can shut the server down if we need to. 2:22 So I'm going to use the pid directive and 2:25 specify that it should to a file within our guest book app directory. 2:27 Within a PID's sub directory and we're gonna name that fur 2:33 unicorn.pid. 2:38 Next we have a couple options for logging. 2:44 So I'm going to say that errors should be logged to 2:48 a particular path with stderr_path. 2:51 We're going to have it write to a file in our guestbook app directory again, 2:55 within a unicorn sub directory. 3:02 We'll name that unicorn.log And 3:07 then we'll use the STD out path directive to have it 3:12 write standard output to the same location to the same log file. 3:15 Okay, this next part is crucial. 3:21 So engine X is going to communicate with unicorn and Gen X 3:23 is one process running on our Linux server and the Unicorn server is another process. 3:28 In Linux processes communicate with each other through sockets which is kind of 3:35 like a file except that one process writes into this virtual file and 3:39 another process reads out of it. 3:43 So we're going to give unicorn the listen directive and 3:45 tell it where it should listen for requests. 3:49 And we're gonna give it the name of the socket to listen on. 3:53 So we'll create a string that points to a path in the tmp root directory. 3:55 And we're going to name that socket unicorn.guestbook.sock. 4:02 Okay, so there's the socket that unicorn is going to listen on will Set up ngenx to 4:10 write to that socket later. 4:14 Then we need to specify how many unicorn processes it should start up. 4:18 For right now we're just going to say worker_processes and 4:22 we'll just give it a number 2 for right now. 4:29 And finally we should specify how long it should give one of those processes to 4:32 respond before it terminates it. 4:36 Be that with the timeout directive and we'll set that to 30 seconds for 4:38 right now. 4:42 Okay, let's save the file an exit. 4:47 Okay and with that configuration in place we can try running the unicorn. 4:50 We're going to want to do that in the production environment, so 4:53 I'll specify rails equals production. 4:56 Since we installed the unicorn through, 5:05 bundler we're going to want to run it using bundlers So we use bundle exec. 5:06 The executable is named unicorn. 5:12 And next we're going to use the -c command line flag to specify a configuration 5:17 file it should load. 5:21 We're going to want to use the config directory unicorn.rb And 5:25 finally we'll use dash capital D to specify that it should run as a demon, 5:31 that is, it should continue running in the background. 5:35 If we try running this, we'll see an error. 5:39 It says that the directory for the P I D file isn't writeable and 5:43 that's because we forgot to create that directory, so let's do that now. 5:47 So, we'll create a, we'll make a directory and pids. 5:52 Here within our rails app directory. 5:56 If we try running it again and 6:00 will fail again because it says the directory for unicorn.log isn't writable. 6:02 We forgot to create a unicorn subdirectory as well. 6:06 So, add directory unicorn. 6:10 Okay let's try one more time. 6:14 And this time it kicks off successfully. 6:19 We've got unicorn running now but 6:22 if we try to visit our Rails app we'll see 502 Bad Gateway again. 6:24 This is because engine x isn't set up to communicate with unicorn via the socket we 6:27 told unicorn to listen on so we're going to need to fix that now. 6:31 So I'm going to go back into the nginx configuration file. 6:36 config/nginxx.conf. 6:41 And specify a socket that nginx should write requests out to. 6:45 We're going to use the upstream directive. 6:50 We're going to name this upstream location unicorn. 6:54 And then options for 6:58 that upstream location are here within brackets, curly brackets. 7:00 And here we're going to use the server directive and 7:04 we're going to specify the Unix socket that it should write out to. 7:08 So unix:/tmp/unicorn.guestbook.sock. 7:12 Which is the same path that we specified within the unicorn configuration. 7:22 And here at the end of the line we're going to add one more option, 7:28 we're going to say fail underscore timeout equals zero. 7:33 That fail timeout equals zero causes engine X to always retry request if 7:40 it fails. 7:44 We need this because a unicorn worker might get shut 7:45 down by the master process if it takes too long to respond and 7:47 we don't want to lose the request so that will cause engine X to retry it. 7:51 And now instead of forwarding requests that it gets onto local host port 3000, 7:56 we want to get it to forwarded on to our upstream location. 8:00 So we're going to have it forward it to another HTTP server. 8:04 But we're going to have it go to the unicorn upstream location. 8:09 So we use the same name as our upstream directive http://unicorn. 8:12 Okay write that out save it, and 8:17 as always we need to restart engine x. 8:22 It will want our sudo password. 8:28 And now if we go to our host IP and try to reload the root path. 8:35 We'll see our Rails app. 8:39 Nginx is successfully communicating with Unicorn using the Unix socket. 8:41
You need to sign up for Treehouse in order to download course files.
Sign up