1 of 140

Full Stack

for

Frontend Engineers 2

2 of 140

Jem Young

Senior Software Engineer

3 of 140

Serious Business

  • Slides
    • jemyoung.com/fsfe2
  • Part 1

4 of 140

Things you’ll learn

  • Learn how to use Node and Bash to create shell scripts
  • Learn advanced Nginx configuration
  • Common server vulnerabilities and how to mitigate them
  • How to add HTTPS to your server
  • Understand databases
  • Containers and automating deployments

5 of 140

Full Stack For Frontend Recap

6 of 140

Recap

  • How the internet works

domain

IP address

7 of 140

8 of 140

Recap

  • How the internet works
  • Command line basics

ping

traceroute

vi

9 of 140

10 of 140

Recap

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

(your sweet new server)

11 of 140

Recap

  • Build a basic web page
  • Command line basics
  • How the internet works
  • How to create and manage a web server
  • Create a deploy system for a Node app

12 of 140

Why full stack?

13 of 140

Part 1

Server setup

  • Create a server
  • Basic setup
  • Adding nodejs

14 of 140

  1. Create a new Ubuntu server
    1. Use Ubuntu 16.04.x
    2. Be sure to use an SSH key
  2. Point domain to new server
  3. Log into server as root

Create a server

15 of 140

Server setup

16 of 140

Server setup - Node

update apt repo for nodejs

$ curl -sL https://deb.nodesource.com/setup_6.x | sudo -E bash -

17 of 140

Server setup - Node

check npm directory

install nodejs and npm

$ npm config get prefix

$ sudo apt install nodejs

18 of 140

If the npm directory is not /usr/local, follow instructions here

WARNING

19 of 140

Server setup - Node

20 of 140

Server setup - Node

install forever module

$ npm i -g forever

21 of 140

Server setup

Clone repo

$ git clone https://github.com/young/fsfe2.git

$ cd /var/www/

$ cd fsfe2

Change working directory

Change working directory

$ npm i

Install modules

22 of 140

Part 2

Server security

  • Controlling access
  • Securing applications

23 of 140

Server security

Control access

  • Strong authentication
  • firewalls
  • user/file permissions

Secure your applications

  • Keep software up to date
  • Limit application use

24 of 140

Server security - add ssh key

create a new directory

paste public key into authorized_keys file

$ mkdir -p ~/.ssh

$ vi ~/.ssh/authorized_keys

25 of 140

Be sure to test logging in with your new user

WARNING

26 of 140

Server security

27 of 140

Server security - firewalls

28 of 140

nmap

Exercise

29 of 140

nmap

$ nmap YOUR_SERVER_IP

30 of 140

Tales from real life

31 of 140

32 of 140

33 of 140

34 of 140

35 of 140

36 of 140

37 of 140

Server security - iptables

$ sudo iptables -A INPUT -p tcp --dport 22 -j ACCEPT

-A append rule

-p protocol (tcp, icmp)

--dport destination port

-j jump (DROP, REJECT, ACCEPT, LOG)

38 of 140

Creating iptable rules

Create an iptable rule to block all outgoing HTTP connections

39 of 140

Creating iptable rules

iptables -p tcp --dport 80 -j REJECT

40 of 140

Creating iptable rules

Create an iptable rule to only allow icmp connections on port 892 from the IP address 192.0.0.1

41 of 140

Creating iptable rules

iptables -A INPUT -s 192.0.0.1 -p icmp --dport 892 -j ACCEPT

42 of 140

There has to be a better way!

43 of 140

Server security - ufw

ufw - uncomplicated firewall

$ sudo ufw allow ssh

$ sudo ufw enable

44 of 140

Create ufw rules

Create a ufw rule to block all outgoing HTTP connections

45 of 140

Creating ufw rules

ufw reject out http

46 of 140

Server security - firewalls

47 of 140

Server security - firewalls

48 of 140

Automatic Updates

49 of 140

Server security - update software

$ sudo apt install unattended-upgrades

50 of 140

Server security - update software

/etc/apt/apt.conf.d/20auto-upgrades

51 of 140

Server security - update software

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

52 of 140

Fail2ban

Exercise

53 of 140

Fail2ban

$ sudo apt install fail2ban

$ sudo cp /etc/fail2ban/jail.conf /etc/fail2ban/jail.local

Copy jail file

$ sudo vi /etc/fail2ban/jail.local

Install fail2ban

54 of 140

If you misconfigure fail2ban, you can lock yourself out of your server!

WARNING

55 of 140

Part 3

advanced shells

  • Finding Things
  • Redirection Operators
  • Shells
  • Shell Scripts

56 of 140

search file names

search file contents

Finding things

grep

find

57 of 140

Finding things

find

find

directory

option

file/folder

58 of 140

useful options

Finding things

find

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

59 of 140

find

Exercise

60 of 140

Find all log files in /var/log/

$ find /var/log/ -type f -name *.log

61 of 140

Find all empty files in /etc

$ find /etc -type f -empty

62 of 140

Find all directories with the word log

$ find / -type d -name log

63 of 140

Searching contents

search inside gzip file

grep

grep - global regular expression print

$ grep -i ‘jem’ /var/www

grep

options

search

expression

directory

$ zgrep FILE

64 of 140

Find running node processes

ps aux | grep node

65 of 140

Redirection

66 of 140

Redirection operators

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

67 of 140

Write to file

ps aux > foo

68 of 140

What does this do?

69 of 140

foo < bar > baz

70 of 140

Shells

71 of 140

What is a shell?

72 of 140

What is a shell?

show current shell

shell

application

kernel

$ echo $0

73 of 140

Changing shells

Exercise

74 of 140

Changing shells

list acceptable shells to change to

change shell to ‘sh’

login into new shell to see the change

change shell to ‘bash’

$ cat /etc/shells

$ chsh -s /bin/sh

$ su $USERNAME

$ chsh -s /bin/bash

75 of 140

Differences between shells

76 of 140

Shell scripting

77 of 140

Why shell scripting

  • simple
  • portable

78 of 140

79 of 140

Bash scripting

Exercise

80 of 140

Shell scripting - bash

$ vi load.sh

#!/bin/sh

cat /proc/loadavg | awk '{print $1"-"$2"-"$3}'

81 of 140

load.sh

Get load average

cat /proc/loadavg

awk '{print $1"-"$2"-"$3}'

Extract the 1st, 2nd, and 3rd columns of data

82 of 140

load average

1 minute

5 minute

15 minutes

83 of 140

chmod

$ sudo chmod 755 ./load.sh

Make executable

84 of 140

chmod

85 of 140

Creating a shell script with Node

Exercise

86 of 140

Node shell scripting - setup

create a workspace folder

move into workspace folder

create index.js

$ mkdir ~/workspace

$ cd ~/workspace

$ touch index.js

87 of 140

Node shell scripting - setup

initialize project

add reference to script

$ npm init

$ vi package.json

88 of 140

Node shell scripting

$ vi index.js

#!/usr/bin/node

const exec = require('child_process').exec;

const stat = exec(`cat /proc/loadavg | awk '{print $1"-"$2"-"$3}'`);

stat.stdout.on('data', function(data) {

console.log(data);

});

89 of 140

90 of 140

Part 4

HTTPS

  • Nginx setup
  • Why HTTPS
  • Getting a certificate
  • Cron

91 of 140

Nginx setup

92 of 140

Nginx setup - adding domain name

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

server_name jem.party www.jem.party;

Add domain name to nginx conf

93 of 140

Why HTTPS

  • Security
  • Technology
    • Service Workers
    • Web Bluetooth
  • HTTP/2

94 of 140

95 of 140

96 of 140

HTTPS

$ sudo add-apt-repository ppa:certbot/certbot

$ sudo apt update

$ sudo apt install python-certbot-nginx

Add the certbot repository

Pull in new repository information

Install certbot with nginx plugin

97 of 140

HTTPS

$ sudo certbot --nginx

Use certbot to get certificate

$ sudo certbot renew --dry-run

Test auto renew

98 of 140

99 of 140

How to do we run periodic tasks?

100 of 140

hour

minute

day of month

month

day of week

command to execute

101 of 140

102 of 140

103 of 140

cron

Exercise

104 of 140

cron

Open crontab for editing

$ sudo crontab -e

Renew certificate every week at 12pm on Monday

105 of 140

Part 5

Nginx tuning

  • gzip
  • Websockets
  • http/2

106 of 140

Nginx - gzip

107 of 140

Nginx - gzip

/etc/nginx/nginx.conf

108 of 140

Nginx - gzip

increase compression level

gzip_comp_level 6

/var/etc/nginx/nginx.conf

109 of 140

Expires headers

110 of 140

Nginx - expires headers

111 of 140

Nginx - expires headers

expire static assets in 30 days

location /static/ {

expires 30d;

proxy_pass http://127.0.0.1:3001/static/;

}

/etc/nginx/sites-available/default

112 of 140

Nginx - expires headers

113 of 140

caching

114 of 140

Nginx - cache

proxy_cache_path /tmp/nginx levels=1:2 keys_zone=slowfile_cache:10m inactive=60m use_temp_path=off;

proxy_cache_key "$request_uri";

/etc/nginx/sites-available/default

115 of 140

Nginx - cache

location /slowfile {

proxy_cache_valid 1m;

proxy_ignore_headers Cache-Control;

add_header X-Proxy-Cache $upstream_cache_status;

proxy_cache slowfile_cache;

proxy_pass http://127.0.0.1:3001/slowfile;

}

/etc/nginx/sites-available/default

116 of 140

websockets

117 of 140

Nginx - websockets

location / {

proxy_set_header Upgrade $http_upgrade;

proxy_set_header Connection "upgrade";

proxy_pass http://127.0.0.1:3001;

}

/etc/nginx/sites-available/default

118 of 140

http/2

119 of 140

Nginx - http/2

listen 443 http2 ssl; # managed by Certbot

/etc/nginx/sites-available/default

120 of 140

Nginx - http/2

121 of 140

Redirect

Redirect request to new url

location /help {

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

}

/etc/nginx/sites-available/default

122 of 140

Part 6

databases

  • Database types
  • MySQL

123 of 140

Database types

relational

non-relational

SQL

NoSQL

124 of 140

MySQL

Exercise

125 of 140

MySQL

Install mysql

$ sudo apt install mysql-server

$ mysql_secure_installation

$ mysql -u root -p

Run setup script

Login as root

126 of 140

Database tips

  1. Back up your database
  2. Use a strong root password
  3. Don’t expose the database outside the network
  4. Sanitize your SQL
  5. Back up your database

127 of 140

128 of 140

Part 7

Containers and more

  • Containers
  • Orchestration
  • Automating deployments

129 of 140

Containers

Dedicated Server

VPS

VPS

VPS

VPS

130 of 140

Containers

the cloud

131 of 140

Containers

Dedicated Server

VPS

node

mysql

nginx

  • shared resources
  • no OS
  • fast deployment

132 of 140

133 of 140

Containers

134 of 140

Containers

135 of 140

136 of 140

137 of 140

Automating deployments

138 of 140

Automating deployments

139 of 140

HOORAY!!

140 of 140

Links

Part 1

Bash course