Deploy Ruby on Rails To Production in 2019

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] what's up guys in this video we're gonna be talking about deploying rails to production on your own server we're gonna walk through installing all the dependencies compiling rubies setting up our database configuring our web server nginx and passenger and then finally deploying our code to production using Capistrano so there's quite a bit we need to cover but if you want to follow the latest version of this guide go ahead and go to go rails comm you can go to guides deploy rails or you can just go to go rails comm slash deploy slash Ubuntu and it will redirect you to the latest version that's available we want to use 18.4 right now because that is the current long-term support release which will give us security updates for many years in the future which is better than using the every six months release which doesn't get long-term support so that is where we're gonna start take a look at this guide and follow along if you want in case the instructions change use the ones on the website but we're gonna walk through at a high level in this video everything you need to know if you just want to wrap your head around how to do this before we dive in I want to mention that I've turned this guide into a product called hatch box where it will go and do all of this in an automated fashion for you so you don't have to do any of this manually plus it comes with a lot of other features like cluster support which allows you to set up a load balancer and have redundant web servers and even move your databases off to their own servers and manage all of that with firewalls SSL and all of that taking care of for you so if you go through this guide and you realize wow this is a lot of work I'd rather have someone do this for me take a look at hachbach style so we're gonna be using digitalocean for our hosting provider but you can use any other provider that gives you a boon to you and root access so you could use Amazon ec2 Linode vulture whatever it is a feel free we're going to be using digitalocean because it's super fast and they have a bunch of other cheap options like manage databases that will help us scale out in the future but for now we're gonna start on a single server and have everything set up there so we're going to choose Ubuntu 18 point o4 we want the long-term release or long-term support release of Ubuntu and that's going to give us security updates for a few years allowing us to keep our production server a lot more secure then we're gonna choose a size for the server just start small you can always upgrade this later on just by simply resizing your droplet or your server we're gonna start with a five dollar server because we have a very small app as you have other requirements or maybe you've gotten a lot of JavaScript that you need to compile with web Packer in production you might want to use a two gig or four gig server but we're gonna start simple with the five dollar one if you get out of memory errors just resize your server to the next size then you can optionally enable backups if you like choose the data center that's closest to you and/or your customers and we're gonna choose to enable private networking so in the future if we want to have this talk to a separate database server inside of our datacenter we can go ahead and do that we can have ipv6 support and monitoring and optionally if you want to add your SSH key here you can go ahead this is not going to email you a password it will actually just set up the SSH key for the root user if you add one here we're gonna leave it out so I can show you how to set this up manually so we're gonna have go rails test production is our hostname name this whatever you like and hit create when this is finished setting up you're gonna get an email with the root password for this server and you'll be able to see the IP address here on the dashboard now that our server setup digitalocean emailed us with the IP address of the server and our root password so we can SSH into that server by saying SSH root link and you can type in yes to confirm that that is the server you want and then you're gonna have to put in the password they gave you so I'm going to paste that in and then immediately they're gonna ask you for your current password one more time and that is going to be the prompt for setting a brand new password so go ahead and create your own password they know that they emailed you the password so they're gonna make you reset it immediately so we're gonna have a new password here and then we can go ahead and while we're in here adduser deploy this is gonna create a deploy user and give him a password as well and then you can fill out the rest of these as blanks and then lastly we want to add user deploy sudo it's gonna add the deploy user to the pseudo group so that the deploy user has access to run commands as root temporarily so then we can exit the server and we can use a tool called SSH copy ID you may have to install this with homebrew but this is going to take our local SSH key and allow us to copy it over to a user on the server so here if we say root at 45.58 even this is going to ask for our brand-new password that we set on the root user and copy over our local SSH key now if you don't have a local SSH key the instructions on github will help you set up a key and you can add that to your machine and then to your github account and that will get you all set up there then we can run SSH copy ID one more time and we'll say it 40 5.30 5.97 so we want to copy it over to the deploy users as well this is going to allow us to SSH into the server without a password now and it will only ask for our SSH keys password not our server user password so now we can log in as deploy to that server and follow the rest of our deployment stuff so the first thing we want to do here is we want to set up our dependencies that we're going to need and so the first one we want is node source comms nodejs install so we're gonna grab that so you know that I have an e bash then we're going to so this will install node.js repository for then we're going to add the yarn repository real quickly this is the two lines that we want from that so I'll paste those in then we want to sudo add apt repository PPA Chris lay / Redis server this is going to give us the Redis server so that we can use that with action cable if we're going to use that or maybe for caching then we can run once these have been done we'll run our sudo apt update just to make sure we have all of those even though it looked like this just ran it for us then we can run sudo apt install git core curl Z lib 1g - dev build essential Lib SSL dev Lib red line dev Lib gamal dev lip sequel Lite 3 dev sequel Lite 3 Lib XML - dev Lib XSLT 1 dev lip coral for open SSL dev software properties common Lib FFI dev dura manager jr. PG app transport HTTP CA certificates Redis server Redis tools nodejs and yarn then you'll get a big old prompt like this where you can hit enter to install all of those packages that's gonna download them and just take a minute to install them all now I made a couple typos in those package names so grab and copy/paste that from the guide and you will have no problems with any of those package names now that that's done we're going to install our bien a ruby version manager so that we can have an easy way of compiling new ruby versions on our server so we're gonna say let's copy it the git repo for our bf we're gonna install that to our home directory dot RB end we're going to get clone H GPS github.com RBM ruby build don't get that's going to go to RB m plugins ruby build and then we're also going to get clone HTTP github.com /r b n RB n varves dot get and this one's going to also go into RB n plugins RB and VARs now RB end is the version manager Ruby build is the tools to compile Ruby and RB n VARs is going to allow us to create an environment variables file on the server so that every app can have its own database URL rails master key any other environment variables that you would want unique to each app so they're not overlapping which is super handy now installing these git repos is step one but we need to also modify our bash RC for that so if we run vim dot bash RC tilde dot bash or see down here at the bottom we can type path equals home slash dot RB and bin path and this is going to then add our BM to our path but we also want to make sure that we have home our BM plugins Ruby build slash bin in there as well so we add that and then we want to add a line called eval and RB n vennett - space and net space - and this is going to evaluate the RB m init scripts so that everything is available for us now if we close this and we say exact dollar sign shell so reload our shell and we should be able to type RB ends now and see that that command works and we can type our be an install and our ruby version so currently the latest version of ruby that we want to use to deploy our app is to 6:1 so we're gonna go ahead and install that but feel free to change that with whatever Ruby version number you want to install this we'll go ahead and download the source code for Ruby and then compile it for your server it's gonna take a while especially on a five dollar server so give it a minute while it installs and we'll be good to go feel free to also install any other Ruby versions that you might want so you can have two Ruby versions or three or four if you have say two or three or four apps on your server that might be wanting to use different ones you can install multiple ones on your server using rpm which can come in super handy now that we have Ruby installed we can run our VM global two point six point one to set the global Ruby version you can run Ruby - B to see that output now we want to install bundler this is gonna install the latest bundler and 2.0 recently came out if you have an application that's not using that you can also gem install bundler v 1.1 7.3 slightest version of bundler one point X and that will just be useful in case you happen to have an application that needs the older bundler next up we want to install nginx and passenger so we're gonna run sudo apt key adv key server hkp key server or boon to comm : 80 or Steve keys and the key we want is five 6 1 F 9 b 9 CA c-40b to f7 that should be the one for a passenger and we'll download that key and here we can see the fusion automated software signing key which is fusion passenger they're the guys that make passenger and we can run sudo SH C echo Deb HTTP OSS binaries fusion passenger dot-com apt passenger ionic main to write that into Etsy app sources.list D slash passenger dot list and that's going to write that repository out we can sudo apt update to update that in your repository in our list and sudo apt install - why nginx extras and live nginx mind HTTP passenger that's going to install a passenger and nginx along with all the extra functionality that you can get so at the very end of this it's going to trigger and nginx reload which if you open up your IP address in the browser you should see the welcome to nginx site and that is one thing we want to remove but first before we do that we want to check and check and see if a file exists or not and so we're gonna say Etsy nginx modules enabled 50 minus G DP passenger kampf and then sudo Ln - s user share nginx modules available mod h GP [Music] passenger load we're gonna symlink that - Etsy and nginx modules enabled 50 mod HTTP a passenger kampf and then we're gonna say sudo vim that's the nginx conf D mod HTTP passenger and then we're gonna change the passenger Ruby line to point to our our BM installation so home deploy dot RB M shims Ruby and we're gonna save that file and quit that's gonna tell passenger where to find Ruby and make sure it uses the correct Ruby version for us and then we want to remove the Etsy nginx sites enable default file we want to get rid that default page that we see here and we're gonna add our own site to that folder so we're going to open up a file in there so it's enabled and you can call this whatever you want I'm gonna call my app to do list so I'm going to name it to do list is the file name and inside here we want to create a server block with our configuration so I'm gonna listen on port 80 and we also want to listen on colon colon in square brackets colon 80 and that will be ipv6 port 80 then we're gonna have our server name as just an underscore this underscore will make it so that it doesn't care what domain comes in it will serve that up and if you want you can say domain comm or whatever your domain is if you want to have multiple sites on the same server nginx just needs to know the domain name so it can separate out the requests to the correct server since we only have one we're just going to use an underscore and that'll work just fine then we want to give it a route and this is going to be where our code will be deployed to in the future so we want to be careful with this and make sure we have the name right so I'm going to name everything to do list so that we have that set up correctly and we want to point it to the current slash public folder and then we can say passenger enabled on this is going to set it up by default to use production mode but we can also set passenger app and to production or staging and that's going to set your rails and VIN your rack and end environment variables accordingly to staging their production then we're going to add a location for cable so this will set up our action cable stuff and we'll say passenger app group name to do list underscore web socket and passenger force max concurrent requests per process to zero and then we can have client max max body size is saying a hundred megabytes so that will allow file uploads up 100 megabytes in size feel free to change that for whatever makes the most sense for your app and then we're going to say assets packs we want these to be the maximum expiration and gzip static on so that our assets and JavaScript packs are set with the proper expirations then we can run sudo service nginx reload to go ahead and reload those configuration files that we just changed and now we're going to need to create a database so we're going to be using Postgres for this and we'll say sudo apt install PostgreSQL PostgreSQL and trib and lib PQ dev that's going to allow us to install the Postgres server as well as lib PQ is going to allow us to compile the PG gem so that we can connect to Postgres through Ruby then o once we're done with this this is going to set up our little cluster here for the database and then we can sudo su the Postgres Linux user this Linux user has access to do a create user so we'll say PW prompt we're gonna call our database user deploy just like our Linux user and we're gonna give this a password I'm just gonna call it password to keep it simple and that's going to create a Postgres user called deploy then this will make sure our applications have certain permissions to interact with their database but not the entire Postgres cluster and any other databases you might have in there so you might want to create a separate user for every application just like Heroku does so if one application gets attacked the other Postgres databases should be safe so we're also going to say create DB with - capital o that's not a zero we're gonna give the deploy user as the owner and we'll call our database like to do list production and if we create the data then we can exit out of our Postgres user so now to test this out we can use the SQL command we'll do - capital u the deploy user you have a little W means password prompt and we're gonna have to use 127.0.0.1 instead of localhost to connect and we want to use to do list production as our database name and if we run that we should get prompted for our deploy users password for the database and we should be able to log in to sequel and we can so that's great that means that when we connect later on to our database we just need to fill out those same details in our database URL environment variable the same sort of steps are listed on the guide for my sequel if you want to use that instead it's a little bit different and the commands you need to run to setup a database but it's very simple and all listed out there now we're gonna hop into our code base and add our Capistrano configs so first things first you want to go to your gem file and at the bottom we're gonna add a couple gems for Capistrano first things first we're gonna add the latest Capistrano gem which in my case is 3.11 and go look up the latest version if you would like or for sure these versions will work but you might want to use a newer version if you want then we're gonna add Capistrano Rails Capistrano passenger and passenger is zero point two point O and gem Capistrano RB n and then this one is two point one and greater than or equal to two point one point four and then we'll go back we'll run bundle to install it on our local machine and you'll notice here that I'm using Ruby two point five point three for this I want to show you what will happen if you get an error during deployment when you basically upload code and passenger doesn't know how to start it I'm going to show you how to debug those issues when you deploy things to production so I'm going to show you a thing that fails so that we can go fix it then we want to run after installing those Capistrano gems we wanna run cap install stages equals production you could also add in staging here if you like and you can have those two to deploy to staging in our production but we're just gonna stick with production what this will do is create a cap file a config deployed our beam config deploy production and potentially if you added staging config deploy staging dot RB now the three of these files we need to modify each one has a little bit different purpose so we're gonna go into our cap file this is where we kind of define our dependencies that we need and so in this case we're going to uncomment require Capistrano rbn Capistrano passenger and we want Capistrano rails but we're going to just you our own line here so let's just do this Capistrano rails because it's going to basically do bundler assets and migrations that's what Capistrano rails will do but it's just one command then at the bottom we want to set our BM type to user and set our BM Ruby to the version of Ruby we want to use on the server so this is important that we'll make sure that it chooses the correct version of Ruby when you were doing your deployments then we can go into our config and deploy dot RB file this is where we're gonna set our application name so here we want to call it to do list this means to match the folder that we defined in our nginx config and we're also going to modify set deploy to to the same thing so here we're gonna say home slash deploy slash and we can call fetch application to basically grab this to do list value and insert it into the string for us you could also hard code it but it's a little bit easier if you set this up like so so then I'm gonna change my repo URL to github.com slash exit three slash to do list which is where my code is then we want to uncomment the linked directories now we want to change this just slightly we want to add in here before public system vendor bundle and dot bundle and then at the end we want to add public uploads I believe uploads and that will take care of any file uploads that you might do to local disk that would allow you to handle active storage and carrier wave and paper clip for there whatever folder names they are this is also going to take the logs and the pids and any temp stuff and keep them all in a single folder no matter how many times you deploy it because Capistrano and production will create multiple folders every time that you make a deployment it creates a new folder and then there's a current sim link which will point to the current version folder so whenever you make a deploy and it's successful the current sim link changes to the new folder and then your application gets restarted so that it uses your new code that makes it really easy to roll back so you can have that current symlink point to the old folder and then you're good to go on a rollback last thing we're gonna do is set releases to five we're only going to keep five releases so we don't have too much stuff on our server if you have a large git repo or something like that this will keep that disk usage down low then the last thing we need to modify for Capistrano is we go into config deploy production not RB and add a line for the server paste in your IP address user will be deployed and the roles the server is going to run our app DB and web so that's going to make sure that everything is run correctly your migrations will run your asset compilations will run and all that good stuff then we can go to our server and make a directory for our application so we inside of our home directory want to make that to-do list folder so that to-do list folder is the same as doing the exact same thing as doing home slash deploy slash to-do list that's where our codes going to live but we don't have anything in there yet what we do want to do is create a to-do list dot RB end VARs file now it's special about this is these are the environment variables we can define just for this application and the main one we're concerned about is the database URL we're gonna set this equal to PostgreSQL colon slash slash deploy our password in my case i just named it password and then one two seven zero zero one slash to do list and i believe i called mine to do list production as the database name so that's the database that you're going to use their username their password the host and your database name so we can save that if you're using rails 5.2 also add your rails master key in here so the credentials can be decrypted you might need to do that for any other variables that you want for production in your stripe keys your secret key base and so secret key base would look something like this secret secret key base you can go in to your application say bundle rails secret or bundle exec rails secret this is going to generate a secret key base that you can go ahead and set up for all of your sessions in production all of your sessions will be signed with that key so that will be that for what we need to do on the server and we can go back to our application now and run cap production deploy now this is going to do quite a bit of different things but namely the important things here is it's creating the folders it's cloning our git repository onto the server and then it's running bundle install so you can see the exact commands here that it's running an orange the sections of things that's working on is linking directory is setting the current revision creating a release and all that stuff and so this is now going to take a minute while it installs all over Jem's for the very first time then next time once this has installed all our gems it would be much quicker it will only need to install any gems that have changed since your last deployment this may take a while and it's gonna also run your database migrations and compile your assets so I will see you back in just a second all right our application has been deployed for the very first time everything completed but don't pat yourself on the back just yet if we open up this in the browser passenger is gonna tell us that it's broken now the way to debug this if you get this specific passenger error is you can go into your server and type sudo les var log nginx error dot log and press after typing in your password type shift G to go down to the very bottom of the file and look for the error what we see here is the app output says rpm said version two point five point three is not installed and so that's our error our application isn't able to start whenever you see this passenger error the fix for that is pretty simple we can edit our dot Ruby version file and make it match the version that we have on the server so then we're going to say get status we're going to add dot Ruby version in this commit and we'll say update Ruby version yes git commit dash M update ruby version well push that up to get and we'll run cat production deploy to go ahead and run the new deployment with our code that's fresh up on github this time it ran a lot faster and if we go back to our browser we can refresh the page and we see that our application is now running now if you see any rails errors that you get the default rails 500 error page you can go to your server and type less your the name of your app in your home directory so to do list current slash log slash production RB & log and then shift G to go down to the very bottom of the file and you can see those logs you can also do tail - F to do list current slash log production not log to keep those running in a terminal so if you refresh the page you'll see that lines get added to this automatically so there you have it we've set up our own a server in production we've configured everything we've installed Ruby and we've made it easy for us to go ahead and add new Ruby versions on the server and then change our dot Ruby version file and cap file to go and make our next Capistrano deployment use that in production we've also made it really easy to add environment variables on the server and the next steps are to add things like our Lea database backups SSL log rotations and a few other things so I'll leave links to those other guides in the notes below but Pat yourself in the back if you've made it this far and if you have any questions or issues let us know in the comments below and we'll take a look and if you've made it this far and found yourself thinking boy with someone else would take care of this for me take a look at hatch box Donna Oh we'll go ahead and configure your servers for you and add a bunch of other features like clusters and load balancers support as well as all of those little details like log rotation already set up for you so you don't have to worry about any of those configuration changes even between versions of Ubuntu we keep track of that and all of the various differences between them so you don't have to worry about it so if you're interested take a look at hatch box dot IO [Music]
Info
Channel: GoRails
Views: 31,059
Rating: undefined out of 5
Keywords: Ruby, Ruby on Rails, Rails, Javascript, HTML, CSS, Servers, Databases, Screencast, Tutorial, deploy rails, deploy ruby, passenger, deploy ruby on rails, deploy ruby on rails to ubuntu server, deploy rails to ubuntu server, ubuntu server, ubuntu 18.04
Id: vzEEfAj45zw
Channel Id: undefined
Length: 32min 39sec (1959 seconds)
Published: Tue Feb 19 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.