1 of 279

2 of 279

Jem Young

Senior Software Engineer

3 of 279

4 of 279

Slides

5 of 279

Part 1

getting started

  • “Full Stack”
  • Command line
  • Shells
  • How the internet works
  • VIM

6 of 279

What is “Full Stack?”

7 of 279

8 of 279

Frontend

Backend

magic?

9 of 279

Frontend

browsers

desktop apps

mobile devices

cars

televisions

appliances

10 of 279

security

reliability

databases

APIs

data analytics

platform

Backend

11 of 279

What is “Full Stack?”

12 of 279

Full Stack Engineer

Someone who can build an application from start to finish

13 of 279

Why Full Stack?

14 of 279

Command Line

15 of 279

Command Line

  • Fast
  • Consistent
  • Easier to automate

Why the command line?

16 of 279

Command Line

Exercise

17 of 279

Command Line

  • cd - change directory
  • ls - list directory contents
  • pwd - print working directory
  • mkdir - make directory
  • rmdir - remove directory
  • cat - show file contents
  • man - command manual
  • less - show file contents by page
  • rm - remove file
  • echo - repeat input

18 of 279

Shells

19 of 279

Shells

20 of 279

Shells

Shell

Command interpreter to interface with system

Terminal

Runs shell applications

21 of 279

$ echo $0

Shells

Show current shell

shell

application

kernel

22 of 279

How does the internet work?

23 of 279

How does the internet work?

Internet

A system of globally interconnected devices

24 of 279

How does the internet work?

Intranet

Private internet

25 of 279

How does the internet work?

IP

Internet Protocol

IP Address

A label assigned to an internet connected device

26 of 279

How does the internet work?

IPv4

8.8.8.8

IPv6

2001:4860:4860:8888

27 of 279

How does the internet work?

TCP

Transmission Control Protocol

UDP

User Datagram Protocol

28 of 279

Ping

Exercise

29 of 279

$ ping twitter.com

30 of 279

31 of 279

How does the internet work?

DNS

Domain Name System

32 of 279

How does the internet work?

subdomain

domain

tld

33 of 279

How does the internet work?

jemyoung.com

====>

23.23.185.61

34 of 279

How does the internet work?

Nameservers

Map domains to IP addresses

35 of 279

Trace Route

Exercise

36 of 279

$ traceroute google.com

$ man traceroute

Traceroute manual

Run traceroute

37 of 279

38 of 279

$ traceroute frontendmasters.com

39 of 279

How does the internet work?

ICMP

Internet Control Message Protocol

ping

traceroute

40 of 279

How does the internet work?

Packet

41 of 279

(Again) How does the internet work?

42 of 279

domain

IP

TCP

?

43 of 279

VIM

44 of 279

VIM

VIM

Vi Improved

45 of 279

VIM

command mode

insert mode

last line mode

Primary mode

Text editing

Searching, saving, exiting

46 of 279

VIM

command mode

insert mode

last line mode

ESC

i

:

47 of 279

VIM

ESC

:q!

HOW TO QUIT VIM

48 of 279

VIM

49 of 279

VIM

Exercise

50 of 279

$ vi

$ man vi

51 of 279

Creating and editing in VIM

Exercise

52 of 279

$ vi temp

53 of 279

Part 2

servers ftw

  • Servers
  • The cloud
  • VPS
  • SSH
  • Buying a domain

54 of 279

Servers

55 of 279

Servers

What does a server do?

56 of 279

Servers

57 of 279

Let’s make a server!

Exercise

58 of 279

$ vi simpleServer.js

59 of 279

60 of 279

$ node simpleServer.js

Run NodeJS script

61 of 279

Servers

62 of 279

Servers

63 of 279

Servers

Datacenter

64 of 279

The cloud

65 of 279

The cloud

66 of 279

The cloud

67 of 279

The cloud

VPS

Virtual Private Server

68 of 279

Buying a VPS

Exercise

69 of 279

VPS

70 of 279

Operating Systems

71 of 279

Operating Systems

72 of 279

Operating Systems

Two main types of server operating systems

windows

unix

BSD

solaris

linux*

freeBSD

OSX/MacOS

ubuntu

red hat

*there are a lot more than 3

debian

73 of 279

Operating Systems

Kernel

Utilities

74 of 279

Operating Systems

75 of 279

Operating Systems

LTS

Long Term Support

76 of 279

SSH

77 of 279

SSH

username/password

Authentication

ssh key

78 of 279

SSH

SSH

Secure Socket Shell

79 of 279

SSH

public key

private key

SSH key pair

80 of 279

SSH

your computer

server

(secret)

encrypted messages

public key

private key

81 of 279

SSH Key

Exercise

82 of 279

$ cd ~/.ssh

$ ls

$ ssh-keygen

Move into .ssh directory

List files

Generate ssh key

83 of 279

84 of 279

public key

private key

85 of 279

Logging in

Exercise

86 of 279

$ ssh root@YOUR_IP

SSH into server

87 of 279

$ ssh -i ~/.ssh/fsfe root@YOUR_IP

SSH into server with key file

88 of 279

89 of 279

$ ssh-add -K ~/.ssh/fsfe

$ vi ~/.ssh/config

Add private key to keychain

Make sure keychain is active

90 of 279

$ ssh root@YOUR_IP

SSH into server

91 of 279

Buying a domain

Exercise

92 of 279

Buying a domain

93 of 279

DNS Records

94 of 279

DNS Records

A record

Maps name to IP address

CNAME

Maps name to name

95 of 279

DNS Records

96 of 279

DNS Records

97 of 279

$ dig blog.jemyoung.com

DNS Records

98 of 279

Domain Setup

Exercise

99 of 279

DNS Records

Add 2 A Records with your IP address

    • @
    • www
    • Use digital ocean to add domain
    • Update nameserver on namecheap

100 of 279

Part 3

Satisfying Server Setup

  • Server setup
  • Nginx
  • NodeJS

101 of 279

Server Setup

102 of 279

Server Setup

  1. Update software
  2. Create a new user
  3. Make that user a superuser
  4. Enable login for new user
  5. Disable root login

103 of 279

Server Setup

# apt update

# adduser $USERNAME

# apt upgrade

Update software

Upgrade software

Add new user

104 of 279

Server Setup

# usermod -aG sudo $YOUR_USERNAME

Add user to “sudo” group

# su $YOUR_USERNAME

Switch user

$ sudo cat /var/log/auth.log

Check sudo access

105 of 279

Server Setup

$ cd ~

Change to home directory

$ mkdir -p ~/.ssh

Create a new directory if it doesn’t exist

$ vi ~/.ssh/authorized_keys

Create authorized_keys file and paste PUBLIC key

106 of 279

Server Setup

$ exit

# exit

Exit

$ ssh $USERNAME@IP_ADDRESS

Login with new user

107 of 279

Server Setup

$ chmod 644 ~/.ssh/authorized_keys

Change file permissions

$ sudo vi /etc/ssh/sshd_config

Disable root login

108 of 279

Server Setup

$ sudo service sshd restart

Restart ssh daemon

109 of 279

Nginx

110 of 279

Nginx

Nginx

(engine-x)

  • Reverse proxy
  • Web server
  • Proxy server

111 of 279

Nginx

App

Database

request

112 of 279

Nginx

App

Database

request

Nginx

113 of 279

Nginx

Exercise

114 of 279

$ sudo apt install nginx

$ sudo service nginx start

Install nginx

Start nginx

115 of 279

116 of 279

Nginx

domain

IP

Nginx

?

117 of 279

Nginx

$ sudo less /etc/nginx/sites-available/default

Show nginx configuration

118 of 279

Nginx

base directory for requests

119 of 279

Nginx

location block

120 of 279

Nginx

directive

121 of 279

Nginx

html defaults

122 of 279

Default page

Exercise

123 of 279

$ sudo vi /var/www/html/index.html

Create and edit index.html

124 of 279

NodeJS

125 of 279

NodeJS

126 of 279

$ sudo apt install nodejs npm

$ sudo apt install git

NodeJS

Install NodeJS and npm

Install git

127 of 279

Part 4

Master of the stack

  • Application Server
  • Process Manager
  • Git

128 of 279

Application Architecture

129 of 279

Application Architecture

130 of 279

Application Setup

Exercise

131 of 279

$ sudo chown -R $USER:$USER /var/www

$ mkdir /var/www/app

$ cd /var/www/app && git init

Change ownership of the www directory to the current user

Create application directory

Move into app directory and initialize empty git repo

132 of 279

$ mkdir -p ui/js ui/html ui/css

$ touch app.js

$ npm init

Create directories

Create empty app file

Initialize project

133 of 279

$ npm i express --save

$ vi app.js

install express

edit app

134 of 279

https://www.dropbox.com/s/8wlwz5wdp7ohe16/Screenshot%202019-09-18%2022.34.07.png?dl=0

)

135 of 279

$ node app.js

Run application

136 of 279

Nginx

domain

IP

Nginx

Express

?

137 of 279

Nginx

location / {

proxy_pass URL_TO_PROXY_TO;

}

138 of 279

Proxy pass

Exercise

139 of 279

$ sudo vi /etc/nginx/sites-available/default

Edit nginx config

location / {

proxy_pass http://127.0.0.1:3000/;

}

140 of 279

What about server restarts?

141 of 279

Process Manager

142 of 279

Process Manager

  • Keeps your application running
  • Handles errors and restarts
  • Can handle logging and clustering

143 of 279

Process Manager

Exercise

144 of 279

$ sudo npm i -g pm2

$ pm2 start app.js

$ pm2 save

$ pm2 startup

Install PM2

Start PM2

Setup auto restart

145 of 279

Version Control

146 of 279

Version Control

  • Git
  • Subversion
  • Mercurial

Version Control

Record changes to a file system to preserve the history.

147 of 279

Static Files

148 of 279

Static Files

app.use(express.static(‘ui’))

149 of 279

Git

Exercise

150 of 279

  1. Create git repository
  2. Create SSH key
  3. Add SSH key to Github
  4. Add remote repo
  5. Push local repository to Github*

*Don’t forget to add a .gitignore file so you don’t commit node_modules/

151 of 279

Congratulations!

152 of 279

Further Exploration

Install Fail2ban

ExpressJS performance tips

153 of 279

Recap

Frontend

Backend

magic?

154 of 279

Recap

domain

IP

Web server

Application server

Server

155 of 279

Recap

  • Command line basics
  • How the internet works
  • How to create and manage a web server
  • Create a Node application

156 of 279

Part 1

bit of this, bit of that

  • Standard streams
  • Redirection
  • Finding things
  • Nginx

157 of 279

Standard Streams

158 of 279

Standard Streams

  • standard output
    • stdout
  • standard input
    • stdin
  • standard error
    • stderr

159 of 279

Redirection

160 of 279

Redirection

  • |
    • read from stdout
  • >
    • write stdout to file
  • >>
    • append stdout to file
  • <
    • read from stdin
  • 2>
    • read from stderr

161 of 279

Redirection

foo > bar

What does this do?

162 of 279

Finding Things

163 of 279

Finding things

grep

find

search file names

search file contents

164 of 279

Finding things

find

find

directory

option

file/folder

165 of 279

Finding things

find

useful options

  • -name
  • -type
  • -empty
  • -executable
  • -writable

166 of 279

Find

Exercise

167 of 279

$ find /var/log/nginx -type f -name “*.log”

Find all log files in /var/log

168 of 279

$ find / -type d -name log

Find all directories with the name ‘log’

169 of 279

Finding things

grep

$ grep -i ‘jem’ /var/www

grep

options

search

expression

directory

$ zgrep FILE

search inside gzip file

170 of 279

Grep

Exercise

171 of 279

$ ps aux | grep node

Find running node processes

172 of 279

Nginx

173 of 279

Nginx - Redirect

location /help {

return 301 https://developer.mozilla.org/en-US/;

}

174 of 279

Nginx - Subdomain

server {

listen 80;

listen [::]:80; # IPV6 notation

server_name test.jemisthe.best;

location / {

proxy_pass http://localhost:3000;

}

}

175 of 279

Nginx - Gzip

/etc/nginx/nginx.conf

176 of 279

Part 2

Security

  • Controlling access
  • Securing applications
  • Firewalls
  • Permissions
  • Upgrade NodeJS

177 of 279

Security

178 of 279

$ sudo cat /var/log/auth.log

Read auth.log

179 of 279

Security

What could someone do if they gained root access to your server?

180 of 279

Security

  • SSH
  • Firewalls
  • Updates
  • Two factor authentication
  • VPN

181 of 279

Updates

182 of 279

$ sudo apt install unattended-upgrades

$ cat /etc/apt/apt.conf.d/50unattended-upgrades

Application Updates

Install unattended upgrades

View conf file

183 of 279

Firewalls

184 of 279

Firewall

Firewall

“A network security device that monitors incoming and outgoing network traffic and decides whether to allow or block specific traffic based on a defined set of security rules.”

185 of 279

Firewall

186 of 279

nmap

Exercise

187 of 279

$ man nmap

$ nmap YOUR_SERVER_IP_ADDRESS

$ sudo apt install nmap

$ nmap -sV YOUR_SERVER_IP_ADDRESS

Install nmap

Run nmap

Run nmap with more service/version info

188 of 279

189 of 279

Firewall

port

communication endpoint that maps to a specific process or network service

190 of 279

$ less /etc/services

Firewall

191 of 279

iptables -p tcp --dport 80 -j REJECT

Firewall

192 of 279

Firewall

ufw

uncomplicated firewall

193 of 279

$ ufw allow ssh

Firewall

deny

reject

http

https

194 of 279

ufw

Exercise

195 of 279

$ sudo ufw allow ssh

$ sudo ufw status

$ sudo ufw enable

Check firewall status

Enable ssh

Enable firewall

196 of 279

197 of 279

How would you create a ufw rule to block all HTTP connections?

198 of 279

sudo ufw reject http

199 of 279

Permissions

200 of 279

Permissions

201 of 279

Upgrade node

Exercise

202 of 279

$ curl -sL https://deb.nodesource.com/setup_10.x -o nodesource_setup.sh

$ sudo bash nodesource_setup.sh

$ sudo apt install nodejs

Download setup script from nodesource

Run script

Install nodejs

203 of 279

$ sudo npm update -g

Application Updates

Update outdated packages

204 of 279

Part 3

HTTP/*

  • HTTP
  • Headers
  • Status codes
  • HTTPS
  • Certbot
  • HTTP/2
  • HTTP/3

205 of 279

HTTP

206 of 279

HTTP

HTTP

hypertext transport protocol

207 of 279

HTTP

request

response

HTTP

208 of 279

HTTP

request

GET / HTTP/1.1

Host: jemisthe.best

User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.132 Safari/537.36

Accept: text/html

Accept-Encoding: gzip, br

Accept-Language: en,en-US

209 of 279

HTTP Headers

210 of 279

HTTP

request

GET / HTTP/1.1

Host: jemisthe.best

User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.132 Safari/537.36

Accept: text/html

Accept-Encoding: gzip, br

Accept-Language: en,en-US

211 of 279

HTTP

common headers

User-agent

The requesting device type

Accept

What the device will handle

Accept-language

Browser languages

Content-type

The type of media

Set-cookie

Sets stateful information

X-

Typically used for custom headers

212 of 279

HTTP

response

HTTP/1.1 200 OK

Server: nginx/1.14.0 (Ubuntu)

Date: Wed, 25 Sep 2019 02:13:13 GMT

Content-Type: text/html; charset=utf-8

Content-Length: 12

213 of 279

Status Codes

214 of 279

Status Codes

HTTP Status Code

Indicates the status of an HTTP request

215 of 279

Status Codes

200

OK

301

Moved permanently

302

Found (temporary redirect)

401

Not authorized

500

Internal server error

216 of 279

Status Codes

1xx

Information

2xx

Success

3xx

Redirect

4xx

Client error

5xx

Server error

217 of 279

What is the proper status code for a successful POST request?

218 of 279

Headers and Status Codes

Exercise

219 of 279

In your app, create a path that returns 418 and a custom header.

hint: res.status(), res.set()

220 of 279

221 of 279

HTTPS

222 of 279

HTTPS

frontendmasters.com

Visa 4235529596901600

223 of 279

HTTPS

frontendmasters.com

Visa 4235529596901600

Evil Server

224 of 279

HTTPS

*Ifkfi24fh243&&o9#$kD#!8b

https://frontendmasters.com

225 of 279

HTTPS

226 of 279

Certbot

Exercise

227 of 279

228 of 279

229 of 279

HTTP/2

230 of 279

HTTP/2

TCP

TCP

TCP

http/1.1

231 of 279

HTTP/2

TCP

http/2

232 of 279

http/2

Exercise

233 of 279

$ sudo vi /etc/nginx/sites-available/default

Edit nginx config

listen 443 http2 ssl;

234 of 279

HTTP/3

235 of 279

HTTP/3

QUIC

Quick UDP Internet Connections

236 of 279

Part 4

Containers

  • Microservices
  • Containers
  • Orchestration
  • Load balancers
  • Deployments

237 of 279

Microservices

238 of 279

Microservices

Microservices

Architecture of loosely connected services

239 of 279

Microservices

240 of 279

Containers

241 of 279

The cloud

242 of 279

Containers

VPS

243 of 279

Containers

Ubuntu

NodeJS

MySQL

Python

Nginx

244 of 279

Containers

Ubuntu

NodeJS

MySQL

Nginx

Python

245 of 279

Containers

OSX

NodeJS

MySQL

Nginx

Python

Fedora

NodeJS

MySQL

Nginx

Python

246 of 279

Containers

  • Lightweight
  • Portable
  • Easier for development
  • Easier to manage
  • Faster startup
  • Decouple application from infrastructure

247 of 279

Containers

  • Amazon ECS
  • Apache Mesos
  • CoreOS rkt

248 of 279

Orchestration

249 of 279

Orchestration

250 of 279

Orchestration

  • Docker Swarm
  • Amazon EKS
  • Apache Mesos
  • AKS

Kubernetes

“K8s”

251 of 279

Load Balancers

252 of 279

Load Balancers

90%

15%

?

request

253 of 279

Load Balancers

90%

15%

request

Load Balancer

254 of 279

Load Balancers

Scheduling Algorithms

  • Round Robin*
  • IP Hashing
  • Random Choice
  • Least Connections
  • Least Load

255 of 279

top

Exercise

256 of 279

$ top

$ sudo apt install htop

$ htop

Display running processes

Install htop

Display running processes

257 of 279

Load Balancers

258 of 279

Load Balancers

259 of 279

Deployments

260 of 279

Deployments

261 of 279

Part 5

Saving Data

  • Files
  • Databases

262 of 279

Files

263 of 279

Files

Why not save everything to a file?

264 of 279

Databases

265 of 279

Databases

  • SQL
  • Tables
  • Related data
  • Strict structure

relational

266 of 279

Databases

  • NoSQL
  • Data agnostic
  • Loose structure

non-relational

267 of 279

Redis

268 of 279

$ sudo apt install redis-server

$ sudo vi /etc/redis/redis.conf

$ sudo systemctl restart redis.service

Redis

Install redis server

Edit config to start with system

Restart redis server

269 of 279

Redis

270 of 279

MySQL

271 of 279

MySQL

Install mysql server

Run setup

$ sudo apt install mysql-server

$ mysql_secure_installation

272 of 279

MySQL

273 of 279

Part 6

Final Project

  • Websockets

274 of 279

Websockets

275 of 279

Websockets

Websocket

Persistent bidirectional connection between client and server

276 of 279

Websockets

location / {

proxy_set_header Upgrade $http_upgrade;

proxy_set_header Connection "upgrade";

proxy_pass http://127.0.0.1:3000;

}

277 of 279

Final Project

278 of 279

Final Project

  1. Create a chat bot using websockets

279 of 279

Congratulations!