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 Key . After practising your first deployment, you should create a test server and test out the recommended techniques that will be mentioned.
_Digital Ocean_is aPlatform 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 sayI can configure and setup a serveris 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.
Adropletis 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
id_rsa.pub
. This is your public key.subl ~/.ssh/id_rsa.pub
Now that we have our SSH key, it is time to create a droplet!
my-site
or
myawesomesite.com
. The name is just used for reference.ssh [email protected]
apt update
apt install tree
to use the
tree
command.apt
is similar to
brew
for Mac OS X - it is a package manager for command line applications.pwd
and
cd
around. Feel free to
mkdir
a few files. Things should look
very
familiar.bash
shell..bash_profile
on this system.logout
of a system to exit.
Right now you're automatically logged in as therootuser. 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 usingbrew
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 gitapt 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 runningruby -v
.
MySQL requires that you add a link to Oracle's repositories. It is not hosted publically onapt
. First, we'll grab that repository, add it toapt
, and updateapt
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 withmysql -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 inapt
. 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 inapt
. 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:
Now, we need to create a directory to store all of our web applications.
cd / # root directorycd /var # var dir
mkdir www # creates /var/wwwpwd# /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 the
hup
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, installpm2, a process monitor for Node applications.
npm install pm2 -g
Finally, you can start your application by running the script specified fornpm start
inside of yourpackage.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?
How do I exit vi?
:
+
q!
Digital Ocean CLI