With one person on each side, safely hold Mac Pro. Pull the orange release latches on both rails, then remove Mac Pro from the rack. Note: You can detach the rails from the side of Mac Pro by pulling the spring latch forward, then sliding the rail back. For Skycap, choose “I have a native Ruby or Node app” and then “Deploy Rails & Rack Frameworks” (Deploy a Ruby App). Step 1 Step 2 If you’re already using Cloud 66 just click New Application → Rails (Rack frameworks) button on the dashboard. Tokaido is a Mac OS X application you can download and install almost as easily as any other Mac application. Launching the application gives you a fully configured Ruby on Rails development environment. You’ll still use the Terminal application for many Rails commands, and you’ll use a text editor to write code. How to Install Ruby on Rails on MacOS. Contribute to RailsApps/install-rails-mac development by creating an account on GitHub.
(Related article: How to Install Ruby on Rails for Linux (Ubuntu))
Step 1. Upgrade Your System to OS X Mavericks
Installing Ruby on Rails is simple, but unless you have an old Mac machine (a pre ‘08 model), you should seriously consider upgrading your system to OS X Mavericks (10.9). It comes with the latest improvements from Apple, and it’s completely free! You can follow this guide for this upgrade.
Step 2. Install XCode Command Line Developer Tools
We need to first install Apple’s XCode Command Line Developer Tools, so that we can use the build tools and utilities that come with it to build Ruby gem native extensions and install other system packages.
This used to be a lengthy process, but if you are on Mavericks, all you have to do is to run this command in your terminal:
After the installation, type
and if the response has something like
Apple LLVM version 5.0 , you are all set.
Step 3. Install Homebrew
Homebrew is the best package manager on Mac. If you are still using MacPorts, it’s time to start brewing! To install homebrew, use this one line installer:
After it finishes, type
you should see the response as
/usr/local/bin/brew
Step 4. Install Ruby with RBENV
Your Mac already ships with Ruby (we used it to install homebrew in the last step). However, it’s still a good idea to use a Ruby version manager because:
RVM and RBENV are the two leading Ruby version managers. I prefer RBENV because it’s a simpler and lighter weight solution, and together with bundler, it solves the project gemset problem in a more elegant way.
We can just use homebrew to install rbenv.
rbenv by itself only manages switching ruby versions. ruby-build and rbenv-gem-rehash are both rbenv plugins. ruby-build allows you to install rubies with rbenv and rbenv-gem-rehash automatically hashes new gems for you when they are installed. You can see here to learn more about rbenv plugins.
You need to initialize rbenv by adding this line in your
~/.bashrc file.
Now you are ready to install Ruby with rbenv. At the time of this writing, the latest stable Ruby version is 2.1.0, so let’s install that.
Step 5. Install Git and Set Up Github Account
Git is the version control system of choice for the Ruby community. If you followed this guide, you should already have git installed as part of the XCode Command Line Developer Tools. You may also want to install git separately with homebrew for easier upgrading.
Now tell git your name and email that it will use for your commits.
Github is the leading platform for source code hosting and collaboration. If you don’t have an account yet, go ahead and sign up for one at https://github.com. Make sure you sign up with the same email address from the step above.
For easier authentication with Github when you push or pull code, follow this guide to set up ssh keys for your Mac.
Step 6. Create a New Rails Application
If you don’t have a directory to hold all your development projects yet, you can create that directory like below:
Now you can create a Rails project in that directory:
Wait until the the last step finishes, and you just created your first Rails project! You can verify that you set up Rails by first starting the server
Now open up your browser and type in the address bar
http://localhost:3000 and if you see a welcome page, your app is running locally.
Step 7. Set Up Sublime Text as Code Editor
If you already have an editor of choice, such as Vim or Emacs, you can skip this step. :) If you are not familiar with code editors, Sublime Text is an excellent choice and you can download it here.
After you install it, run the following command:
and now you can simply type
in your Rails project directory to start coding.
The convention for Ruby programs is to use two spaces as indentation. You can follow
Sublime Text 2 => Preferences => Settings - User and add these lines.
Optional Step 1. Use iTerm 2, zsh and oh-my-zsh to set up an awesome terminal
Download and Install iTerm 2. It comes with more features and is easier to customize than the built in Terminal.
Now it’s time to customize your terminal! Here are some of my preferences.
Zsh is an alternative shell to the default bash shell that comes with Mac. It adds nice features such as smart tab completions, but what really sets it apart is its scriptability. Together with oh-my-zsh, an open source zsh configuration management framework, it becomes really easy to customize both the look and functionality of your terminal.
Your Mac already comes with zsh. To use zsh, go to iTerm 2 => Preferences => Profiles => General and in the “Command” section, select “Command”, and type
/bin/zsh in the box after it. Now close your terminal and relaunch it, you should be on zsh!
Next, let’s install oh-my-zsh:
Now you can customize the
~/.zshrc file.
If you do not like the default theme, you can pick from one of the many themes that come with oh-my-zsh. You can see the list of themes here and here.
If you feel really adventurous, you can even build your own theme! Take a look at how themes are implemented, and copy/tweak/build one exactly to your taste!
Optional step 2. Install Postgresql as a production quality database
By default, Rails uses sqlite3 as the default development database. It’s a nice database but probably not one that you want to use in production. Postgresql is a solid, production quality relational database and works well with Rails. It’s generally a good idea to set up your local database to match the database on the production environment.
The easiest way to use Postgresql on Mac is to download and install the Postgres.app
With Postgresql running, add
gem 'pg' to the Gemfile in your rails project and run bundle install to install the Postgresql Ruby driver. This RailsCast talks about how to set up with Postgres in detail. You can skip the “taps” part if you don’t already have data that needs to be migrated.
Congratulations!
This is it! You just made it to the end of our first rails tutorial, and have now set up your local Ruby on Rails development environment like a pro, now it’s time to start coding. :)
If you've ever wondered how to deloy Ruby on Rails to production on your own server, you've come to the right place.
I wrote this guide to cover the ENTIRE process from choosing a server, installing dependencies, configuring NGINX, setting up your database, and making your first deployment using Capistrano.
So if you're looking to step up your Ruby on Rails game this year, you'll love this guide.
ContentsChapter 1:Creating a Production Server
A Virtual Private Server (VPS) is where our Rails application and all it's relevant services will live.
We have a ton of different options for this, so we'll discuss where to find a good VPS and how large of a server you should choose.
![]() Choosing a Server Hosting Provider
There are many, many options when it comes to hosting providers. These are the places that own servers in a datacenter and will rent them out to you monthly.
It's important to buy a server at a place like this because they will handle issues with hardware failures and provide you a super fast internet connection which is crucial to running anything in production on the internet.
Ignore managed hosting providers. They don't give enough control over your server to set things up and they're usually very outdated.
Here are some hosting provider options:
I recommend DigitalOcean. It's super cheap, really fast, and the easiest to use by far. You'll save a good amount of money by using them and they continue offering lots of new products.
Plus, if you use my DigitalOcean link, you'll get $100 in free credit to spend over 60 days! That's a perfect way to try things out and see how it goes without spending a dime.
What Size Server Do I Need?
Ruby and Rails apps tend to require a lot of RAM, so that's the main metric we're going to be focusing on when deciding what size server we should use. Plus, we'll need to run a database like Postgresql or MySQL as well as Redis for background workers. Each of these will require RAM to operate so we want to take those into account too.
I recommend a 2GB RAM server ($10/mo) if you're deploying for the first time. You might even be able to get by with a 1GB server, but you'll likely run out of RAM when compiling assets during deployment to production.
GoRails uses a 4GB RAM server ($20/mo) and serves over 2 MILLION pages per year! That's quite a lot of traffic and the server barely breaks a sweat.
If you have large dependencies like ElasticSearch, you'll have to add more RAM. ElasticSearch, for example, requires 4GB of RAM at a minimum, so you'll need to consider that in addition to what RAM you need for your Ruby and Rails apps. You can run ElasticSearch on a separate server to make it easier to scale the services up separately.
Create Your Server
It's time for us to create our server. Open up DigitalOcean and go to the Create Droplet page.
Step 1: Choose your operating system
We want to use Ubuntu 18.04 for our server's operating system. It's an long-term support (LTS) release which means it will receive security updates for several more years than normal. This is crucial for production.
Choose
Ubuntu 18.04 from the dropdown in the Choose an image section.
Step 2: Choose your size
Go ahead and select the server size under Choose a size based upon what you feel comfortable with.
Not sure what size to use? Just start with a 2GB RAM server. You can always resize the server to a larger size later without losing any data if you want. That's the nice part of these 'virtual' servers. They're not real servers, so you easily add more RAM or CPUs to your server at any time.
Step 3: Choose your region
Next we'll choose the server region. This is the datacenter our server lives in. Choose one that's closest to where your users (or you) will be.
Step 4: Optional settings
You can enable a few other options if you choose:
Step 5: Create your server
Last, click 'Create' to create your server. It will take about 60 seconds for DigitalOcean to create your server.
Once that's complete, check your email for the password to the new server.
You can login to your server as root by running the following command, just change
1.2.3.4 to your server's public IP address:
Step 6: Creating a Deploy user
To run software on our server, we're going to create a deploy user. The deploy user has limited permissions in production to help prevent anyone from getting full control of our server if a hacker broke into our server.
While logged in as
root on the server, we can run the following commands to create the deploy user and add them to the sudo group.
adduser deploy sudo
Next let's add our SSH key to the server to make it faster to login. We're using a tool called
ssh-copy-id for this.
If you're on a Mac, you may need to install ssh-copy-id with homebrew first:
brew install ssh-copy-id
ssh-copy-id [email protected]
Now you can login as either
root or deploy without having to type in a password!
For the rest of this tutorial, we want to be logged in as deploy to setup everything. Let's SSH in as deploy now and we shouldn't be prompted for a password this time.
Chapter 2:Installing Ruby
Installing Ruby in production is similar to development except we need to make sure we have all the Linux dependencies installed to compile Ruby correctly.
We also want to use a Ruby version manager for this so we can easily deploy new versions of Ruby to production in the future without having to edit config files.
Installing Ruby
We're going to be installing Ruby using a ruby version manager. You're probably using one in development, but this is also handy in production so it allows you to upgrade Ruby version in production quickly.
The first step is to install some dependencies for compiling Ruby as well as some Rails dependencies.
To make sure we have everything necessary for Webpacker support in Rails, we're first going to start by adding the Node.js and Yarn repositories to our system before installing them.
We're also going to install Redis so we can use ActionCable for websockets in production as well. You might also want to configure Redis as your production store for caching.
Make sure you're logged in as the
deploy user on the server, and run the following commands:
curl -sL https://deb.nodesource.com/setup_12.x | sudo -E bash -
curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | sudo apt-key add -
echo 'deb https://dl.yarnpkg.com/debian/ stable main' | sudo tee /etc/apt/sources.list.d/yarn.list
sudo add-apt-repository ppa:chris-lea/redis-server
# Refresh our packages list with the new repositories
# Install our dependencies for compiiling Ruby along with Node.js and Yarn
sudo apt-get install git-core curl zlib1g-dev build-essential libssl-dev libreadline-dev libyaml-dev libsqlite3-dev sqlite3 libxml2-dev libxslt1-dev libcurl4-openssl-dev software-properties-common libffi-dev dirmngr gnupg apt-transport-https ca-certificates redis-server redis-tools nodejs yarn
Yay! Now that we have our dependencies installed, we can begin installing Ruby.
Choose the version of Ruby you want to install:
Next we're going to install Ruby using a Ruby version mmanager called rbenv. It is the easiest and simplest option, plus it comes with some handy plugins to let us easily manage environment variables in production.
git clone https://github.com/rbenv/rbenv.git ~/.rbenv
echo 'export PATH='$HOME/.rbenv/bin:$PATH' >> ~/.bashrc
git clone https://github.com/rbenv/ruby-build.git ~/.rbenv/plugins/ruby-build
echo 'export PATH='$HOME/.rbenv/plugins/ruby-build/bin:$PATH' >> ~/.bashrc
git clone https://github.com/rbenv/rbenv-vars.git ~/.rbenv/plugins/rbenv-vars
rbenv install 2.7.2
ruby -v
The last step is to install Bundler:
# This installs the latest Bundler, currently 2.x.
# For older apps that require Bundler 1.x, you can install it as well.
# Test and make sure bundler is installed correctly, you should see a version number.
# Bundler version 2.0
If it tells you bundle not found, run
rbenv rehash and try again.
Chapter 3:Configuring A Web Server
Rails won't live on its own in production. In front of Rails, we'll put up NGINX to handle SSL and serving static files because it's way faster than Ruby.
To run our Rails app, we'll install the Passenger module for NGINX to forward requests over to Rails.
Installing NGINX & Passenger
For production, we'll be using NGINX as our webserver to receive HTTP requests. Those requests will then be handed over to Passenger which will run our Ruby app.
Installing Passenger is pretty straightforward. We'll add their repository and then install and configure their packages.
sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv-keys 561F9B9CAC40B2F7
sudo sh -c 'echo deb https://oss-binaries.phusionpassenger.com/apt/passenger bionic main > /etc/apt/sources.list.d/passenger.list'
sudo apt-get install -y nginx-extras libnginx-mod-http-passenger
if [ ! -f /etc/nginx/modules-enabled/50-mod-http-passenger.conf ]; then sudo ln -s /usr/share/nginx/modules-available/mod-http-passenger.load /etc/nginx/modules-enabled/50-mod-http-passenger.conf ; fi
Now that we have NGINX and Passenger installed, we need to point Passenger to the correct version of Ruby.
We'll start by opening up the Passenger config file in your favorite editor,
nano or vim
sudo nano /etc/nginx/conf.d/mod-http-passenger.conf
sudo vim /etc/nginx/conf.d/mod-http-passenger.conf
We simply want to change the
passenger_ruby line to match the following:
Save this file and we'll start NGINX.
Deploy Rails App Mac Download
You can check and make sure NGINX is running by visiting your server's public IP address in your browser and you should be greeted with the 'Welcome to NGINX' message.
Next we're going to remove this default NGINX server and add one for our application instead.
sudo nano /etc/nginx/sites-enabled/myapp
sudo vim /etc/nginx/sites-enabled/myapp
We want the contents of our NGINX site to look like the following.
Change
myapp to the name of your app. We'll use this same folder later on when we define our Capistrano deploy_to folder.
Save the file and then we'll reload NGINX to load the new server files.
Chapter 4:Creating A Database
For production, we'll need to create a new database for all our production records.
We recommend running PostgreSQL, but we'll also include MySQL for those of you more familiar with it.
We recommend PostgreSQL for your production database but feel free to use MySQL instead. Just follow the instructions for the database you want to use and skip to the next part when you're done.
Creating a PostgreSQL Database
For Postgres, we're going to start by installing the Postgres server and libpq which will allow us to compile the pg rubygem.
Then, we're going to become the
postgres linux user who has full access to the database and use that account to create a new database user for our apps. We'll call that user deploy .
And finally, the last command will create a database called
myapp and make the deploy user owner.
Make sure to change
myapp to the name of your application.
sudo apt-get install postgresql postgresql-contrib libpq-dev
createuser --pwprompt deploy
exit
You can manually connect to your database anytime by running
psql -U deploy -W -h 127.0.0.1 -d myapp . Make sure to use 127.0.0.1 when connecting to the database instead of localhost.
Creating a MySQL Database
For MySQL, we'll install both the server and client libraries so we can compile the mysql2 rubygem.
sudo apt-get install mysql-server mysql-client libmysqlclient-dev
# Open the MySQL CLI to create the user and database
Now we're inside the MySQL command line interface, we can create a database for our app and a special user who only has access to this database. Having an app-specific user will provide better security in case something gets compromised.
It may look like we're creating two users and that's because MySQL treats users over localhost different than users over an IP address. We're setting it up so you can do both.
In the following example, make sure you replace the following names:
'>CREATE USER IF NOT EXISTS 'deploy'@'localhost' IDENTIFIED BY '$omeFancyPassword123';
'>CREATE USER IF NOT EXISTS 'deploy'@'%' IDENTIFIED BY '$omeFancyPassword123';
'>GRANT ALL PRIVILEGES ON myapp.* TO 'deploy'@'localhost';
Deploy Rails App Mac Os
'>FLUSH PRIVILEGES;
Chapter 5:Deploying Code
Once we have everything on our server configured and ready to go, we need a way to upload our code to production.
Capistrano is a tool for making copies of your repository in production and then easily making new releases.
Setting Up Capistrano
Back on our local machine, we can install Capistrano in our Rails app.
We'll need to add the following gems to our Gemfile:
Once added, we can run the following to install the gems and have Capistrano install its config files:
This generates several files for us:
We're need to edit the
Capfile and add the following lines:
Then we can modify
config/deploy.rb to define our application and git repo details.
Now we need to modify
config/deploy/production.rb to point to our server's IP address for production deployments. Make sure to replace 1.2.3.4 with your server's public IP.
Before we can deploy our app to production, we need to SSH into the server one last time and add our environment variables.
mkdir /home/deploy/myapp
Add any environment variables you need for production to this file.
Save this file and these environment variables will be automatically loaded every time you run Ruby commands inside your app's directory on the server.
Using this method, we can have separate env variables for every application we deploy to this server.
Now we can deploy our app to production:
Open your server's IP in your browser and you should be greeted with your Rails application.
If you see an error, you can SSH into the server and view the log files to see what's wrong.
less /home/deploy/myapp/current/log/production.log
sudo less /var/log/nginx/error.log
Once you find your error (often times a missing environment variable or config for production), you can fix it and restart or redeploy your app.
Chapter 6:Deploy Rails App Mac ProNext Steps
Congratulations! You've deployed your app to production.
There are several things you may want to do like configuring a domain, SSL, backups, and log rotation.
Ps App Deploy Kit
Here are a few common next steps we highly recommend doing:
Deploy Rails AppAdd SSL with LetsEncrypt
Lets Encrypt provides free SSL and every site on the internet should be using SSL.
Hourly Backups
Setting up hourly backups to S3 makes sure that you can recover quickly in case anything bad happens.
Setup Log Rotation
Deploy Rails App To Aws
This helps make sure your Rails logs don't fill your disk space and crash your server.
Comments are closed.
|
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |