Full Coverage available on Medium
View on Github Page
View on Gitbook Page
This guide will show you how to create a virtual server and deploy an app to it.
The goal of this guide is to introduce you to the concept of provisioning a virtual server. In the world of cloud computing, you'll likely deploy applications to various providers such as Heroku, Amazon Web Services, Google Cloud, Microsoft Azure, Rackspace, ... and more. Understanding how to do this on your own is incredibly important. This is meant to be an introductory guide; it only notes where you should implement a more secure best practice - you'll see them next to a π. After practicing your first deployment, you should create a test server and test out the recommended techniques that will be mentioned.
- Registering for Digital Ocean
- Identify what Droplets are and how to create one
- Work with a remote server (droplet) via command line
- Provisioning a your virtual server
- Deploying an application
- FAQ
Digital Ocean is a Platform as a Service (PaaS). It is designed to do one thing and to do it well - create instances of web servers for you to use. Once you're out working on a job, you'll be exposed to a variety of PaaS providers - Amazon's EC2, Heroku, AppHarbor, and of course, Digital Ocean. Today you're going to sign up for Digital Ocean, create a droplet (their word for a server), and write an app and put it out for the world to see! Because as cool as it is to show your classmates what you've been working on, wouldn't it be cooler to show your friends and prospective employers? Plus, this will give you a leg up when looking for a job: being able to say I can configure and setup a server is kind of a big deal.
Digital Ocean requires a credit card on file. Please be aware that if you run out of credits, your card will be billed. This cost is worth it to show your work off to employers.
- Browse to https://www.digitalocean.com/
- Sign up for a new account on the main page by entering your email address and creating a password.
- You'll be sent a confirmation email (can take up to 5 minutes).
- Once you confirm your account, you'll need to enter in credit card information. Do so.
- Now, before doing anything else, select the profile icon and select billing.
- Enter in the unique code sent to you on email for your free credits!
A droplet is a scalable server offered by Digital Ocean. Digital Ocean supports a variety of Linux platforms to develop on (amongst others). A droplet can serve a small website that you use for your own portfolio and it can scale up to host an enterprise application! One of the best things about a droplet is that it can scale - if your site blows up, you can expand the resources it has without needing to create a new server!
Now, we need to locate something to make logging into our droplet easy and secure.
π Keep your SSH key private. These are meant to be kept secret; kept safe. Don't share it with strangers.
We need to create a secure way for you to log into any Droplet that you create. We're going to use an private key that you already are using on your computer. You should only share private keys with entities you trust! I only share mine with my computers and the servers I run. I even have a copy of mine in my will! They're private!
Because we want to make sure that you and only you - not some hacker in Russia, not some script kiddie in China - has access to your droplet, we'll use a private key that we're already comfortable with to connect to the server.
Open up terminal and enter in the following commands:
ls -al ~/.ssh
- list all of the keys in the ./ssh directory. You should see anid_rsa.pub
. This is your public key.subl ~/.ssh/id_rsa.pub
- Open the key in Sublime Text so we can use it in just a moment.
Now that we have our SSH key, it is time to create a droplet!
- Log in to Digital Ocean if you have not already done so.
- Select "Create Droplet" in the top-right corner.
- Give your droplet a name. It can be
my-site
ormyawesomesite.com
. The name is just used for reference. - Select the $5/month size for your Droplet (hobby sites use less resources than large production sites)
- Select a region.
- Ignore the available settings.
- Select the Ubuntu 16.x x64 operating system.
- Select Add SSH Key. You will copy/paste the SSH key that we retreived just moments ago into the text box.
- Select Create Droplet.
- Annnnndd we wait!
- Log into the remote server (Droplet)
ssh [email protected]
- I'm magically logged in because it used my private key from earlier to authenticate who I am!
- I need to update the system's repositores!
apt update
- Now, explore your filesystem.
apt install tree
to use thetree
command. apt
is similar tobrew
for Mac OS X - it is a package manager for command line applications.pwd
andcd
around. Feel free tomkdir
a few files. Things should look very familiar.- If you installed the default version of Ubuntu, you might notice you're in a
bash
shell. - Consider looking for your
.bash_profile
on this system. - Once you're done practicing your Unix/Linux skills, move on.
- Like any other computer, a user can
logout
of a system to exit.
π Right now you're automatically logged in as the root user. This user has all of the power on your server. It is best practice create new users to handle specific tasks (such as one user named dba_admin for databases and one named webmaster for web servers).
We're going to use the apt package manager to install a few tools. You might remember using brew
to do this in Mac OS X earlier during the cohort. Because each environment and application is different, we have provided a few scripts in this repository to help make life easier. This guide will contain a few familiar stacks. You should only install what you need and nothing else. Unneccesary software installed on your software can expose security vulnerabilities that you don't need in the first place.
Install Git
apt install git
apt install build-essential
Install Ruby on your linux box
apt install ruby # installs ruby
apt install ruby-dev # install build tools necessary for building some gems (bcrypt, json, ...)
Verify Ruby is installed by running ruby -v
.
MySQL requires that you add a link to Oracle's repositories. It is not hosted publically on apt
. First, we'll grab that repository, add it to apt
, and update apt
so we can find MySQL.
# get the MySQL repository information
wget http://dev.mysql.com/get/mysql-apt-config_0.8.0-1_all.deb
# install it
sudo dpkg -i mysql-apt-config_0.8.0-1_all.deb
# you'll be provided a GUI option; select the default options (5.7)
# and exit. this is ok! nothing flashy happens here.
# update apt so it can point to the MySQL repository
sudo apt update
Once that is installed, we'll install MySQL.
# install a C library that Ruby uses to build the mysql2 gem with
apt-get install libmysqlclient-dev
# install mysql
apt install mysql-server
Once installed, we can control MySQL with the following commands:
# start
sudo service mysql start
# stop
sudo service mysql stop
# info
sudo service mysql status
To login to MySQL, you may do so with mysql -p
. -p
specifies that the user is using a password (so it requests you enter one).
We recommend using the official installation guide for Ubuntu from Mongodb: https://docs.mongodb.com/v3.2/tutorial/install-mongodb-on-ubuntu/
This version of node is the first LTS release post the io.js merger. Usage of many Javascript 2015 (ES6) features requires 'use strict'
or are not available at all. Node isn't included with the standard list of applications available in apt
. We'll need to add it ourselves:
curl -sL https://deb.nodesource.com/setup_4.x | sudo -E bash -
Once completed, we can then install Node by running:
apt install nodejs
You can verify that Node and npm have been installed by running the following commands.
npm -v
node -v
This version of node contains most Javasript 2015 (ES6) features availability directly in Node without the need of transpiling. Node isn't included with the standard list of applications available in apt
. We'll need to add it ourselves:
curl -sL https://deb.nodesource.com/setup_6.x | sudo -E bash -
Once completed, we can then install Node by running:
apt install nodejs
You can verify that Node and npm have been installed by running the following commands.
npm -v
node -v
Applications on a server are ran just like any other application on your laptop - through terminal commands. To make an application run on your virtual server, you must consider how it runs on your laptop. Does it need a database? Is that database running? Did you change your environment variables to reflect the different SQL users between your laptop and server? There are a lot of things to consider when deploying an application. Before diving into specific platforms, here are some questions to consider:
- Is Git installed?
- Have you cloned your application to your server yet?
- Does my application require a database? Is it the same type on my computer and server?
- Have I created appropriate SQL users for my database?
- Have I updated my config files and environment variables to point to the proper server?
- Have I removed all breakpoints and set my app to run in production mode (vs development)?
- Have I ran all database related tasks (Rake, Gulp, ...)?
Now, we need to create a directory to store all of our web applications.
cd / # root directory
cd /var # var dir
mkdir www # creates /var/www
pwd # /var/www
Clone any applications you'd like to run using Git in the /var/www
folder. This is the typical location for storing web applications on Debian (Ubuntu) linux servers. This location is one of the defaults that has been constant throughout decades of web development.
π Consider using Puma over Rack for hosting higher-traffic websites.
To deploy a Ruby application, clone your Git project into your /var/www/
folder and change into it. Install the required gems for your application:
bundle install
Now, you can deploy your application via:
nohup bundle exec rackup -p 80 --host 0.0.0.0
What is nohup?
nohup
: Do not listen to thehup
signal when terminal is closedbundle exec
: Use the gem versions in the Gemfile.lock to execute the commandrackup -p 80
: Run the application on port 80&
: Run this command in the background0.0.0.0
broadcasts to all addresses on this machine.
To deploy a Node application, clone your Git project into your /var/www/
folder and change into it. Install the required modules for your application:
npm install
Next, install pm2, a process monitor for Node applications.
npm install pm2 -g
Finally, you can start your application by running the script specified for npm start
inside of your package.json
.
pm2 start app.js -x -- --prod
You can stop your application, too (for maintenance and upgrading):
pm2 stop app.js -x -- --prod
How do I use Nano?
- Exit - ctrl-x
- Prompts you to save - select Y or N
- Prompts you to confirm where saving. Either edit or press return/enter.
How do I exit vi?
:
+q!
- Forces an exit without a save Digital ocean CLI
Digital Ocean CLI