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 HTT
Trang 1Full Stack
for
Trang 2Jem Young
Senior Software Engineer
Trang 3Serious Business ● Slides ○ jemyoung.com/fsfe2
● Part 1
○ https://frontendmasters com/courses/full-stack/
○ jemyoung.com/fsfe
Trang 4Things 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
Trang 5Full Stack For Frontend
Recap
Trang 6● How the internet works
Trang 8● How the internet works
● Command line basics
ping
traceroute
vi
Trang 10● Command line basics
● How the internet works
● How to create and manage a web server
(your sweet new server)
Trang 11● 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
Trang 12Why full stack?
Trang 141 Create a new Ubuntu server
a Use Ubuntu 16.04.x
b Be sure to use an SSH key
2 Point domain to new server
3 Log into server as root
Create a server
Trang 15Server setup
4 Update server
5 Add a new user ‘test’
6 Grant ‘test’ sudo access
7 Switch to ‘test’ user
Trang 16Server setup - Node
$ curl -sL https://deb.nodesource.com/setup_6.x
| sudo E bash
-update apt repo for nodejs
https://explainshell.com/explain?cmd=curl+-sL
Trang 17Server setup - Node
$ npm config get prefix check npm directory
$ sudo apt install nodejs install nodejs and npm
Trang 18If the npm directory is not /usr/local,
follow instructions here
WARNING
Trang 19Server setup - Node
https://docs.npmjs.com/getting-started/fixing-npm-permissions#option-2-change-npms-defau lt-directory-to-another-directory
Trang 20Server setup - Node
$ npm i -g forever install forever module
Trang 21Change working directory
Change working directory
Trang 22Part 2
Server security
● Controlling access
● Securing applications
Trang 23Secure your applications
- Keep software up to date
- Limit application use
Trang 24Server security - add ssh key
$ mkdir -p ~/.ssh create a new directory
$ vi ~/.ssh/authorized_keys paste public key into
authorized_keys file
Trang 25Be sure to test logging in with your
new user
WARNING
Trang 26Server security
a use the passwd command
2 Disable root login
3 Disable password login
Trang 27Server security - firewalls
Trang 28nmap
Trang 29$ nmap YOUR_SERVER_IP
Trang 30Tales from real life
Trang 37Server 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)
Trang 38Creating iptable rules
Create an iptable rule to block all
outgoing HTTP connections
Trang 39Creating iptable rules
iptables -A OUTPUT -p tcp dport 80 -j REJECT
Trang 40Creating iptable rules
Create an iptable rule to only allow icmp connections on port 892 from the IP
address 192.0.0.1
Trang 41Creating iptable rules
iptables -A INPUT -s 192.0.0.1 -p icmp dport 892 -j ACCEPT
Trang 42There has to be a better
way!
Trang 43Server security - ufw
$ sudo ufw allow ssh
$ sudo ufw enable
ufw - uncomplicated firewall
Trang 44Create ufw rules
Create a ufw rule to block all outgoing
HTTP connections
Trang 45Creating ufw rules
ufw reject out http
Trang 46Server security - firewalls
Trang 48Automatic Updates
Trang 49Server security - update software
$ sudo apt install unattended-upgrades
Trang 50Server security - update software
/etc/apt/apt.conf.d/20auto-upgrades
https://wiki.debian.org/UnattendedUpgrades
Trang 51Server security - update software
/etc/apt/apt.conf.d/50unattended-upgrades
Trang 52Fail2ban
Trang 53$ 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
Trang 54If you misconfigure fail2ban, you can
lock yourself out of your server!
WARNING
Trang 56find search file names
search file contents
Finding things
Trang 57find directory option file/folder
Finding things
Trang 59find
Trang 60Find all log files in /var/log/
$ find /var/log/ -type f -name *.log
Trang 61Find all empty files in /etc
$ find /etc -type f -empty
Trang 62Find all directories with the word log
$ find / -type d -name log
Trang 63Searching contents
grep
grep - global regular expression print
$ grep -i ‘jem’ /var/www
grep options search
expression
directory
$ zgrep FILE search inside gzip file
Trang 64Find running node processes
ps aux | grep node
Trang 65Redirection
Trang 67Write to file
ps aux > foo
Trang 68What does this do?
Trang 69foo < bar > baz
Trang 70Shells
Trang 71What is a shell?
Trang 72shell application kernel
$ echo $0 show current shell
Trang 73Changing shells
Trang 74Changing shells
$ cat /etc/shells list acceptable shells to
change to
$ chsh -s /bin/sh change shell to ‘sh’
$ su $USERNAME login into new shell to see the
change
$ chsh -s /bin/bash change shell to ‘bash’
Trang 75Differences between shells
https://en.wikipedia.org/wiki/Comparison_of_command_shells
Trang 76Shell scripting
Trang 77- simple
- portable
Why shell scripting
Trang 79Bash scripting
Trang 80Shell scripting - bash
$ vi load.sh
#!/bin/sh
cat /proc/loadavg | awk '{print $1"-"$2"-"$3}'
Trang 82load average
1 minute
5 minute
15 minutes
Trang 83$ sudo chmod 755 /load.sh Make executable
Trang 84https://isabelcastillo.com/linux-chmod-permissions-cheat-sheet
Trang 85Creating a shell script with Node
Trang 86Node shell scripting - setup
$ mkdir ~/workspace create a workspace folder
$ cd ~/workspace move into workspace folder
$ touch index.js create index.js
Trang 87Node shell scripting - setup
$ npm init initialize project
$ vi package.json
add reference to script
Trang 88Node 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);
});
Trang 89https://frontendmasters.com/courses/bash-vim-regex/
Trang 91Nginx setup
1 Install nginx
2 Proxy traffic to node server
3 Add domain name
4 Open port 443
Trang 92Nginx setup - adding domain name
sudo vi /etc/nginx/sites-available/default
server_name jem.party www.jem.party;
Add domain name to nginx conf
Trang 94https://github.com/diafygi/acme-tiny
Trang 95https://certbot.eff.org/
Trang 96$ 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
Trang 97$ sudo certbot nginx Use certbot to get certificate
$ sudo certbot renew dry-run Test auto renew
Trang 99How to do we run periodic
tasks?
Trang 101https://crontab.guru/
Trang 103cron
Trang 106Nginx - gzip
Trang 107/etc/nginx/nginx.conf
Trang 108Nginx - gzip
gzip_comp_level 6 increase compression level
http://nginx.org/en/docs/http/ngx_http_gzip_module.html
/var/etc/nginx/nginx.conf
Trang 109Expires headers
Trang 110Nginx - expires headers
Trang 111location /static/ {
expires 30d;
proxy_pass http://127.0.0.1:3001/static/;}
expire static assets in 30 days
/etc/nginx/sites-available/default
Trang 112Nginx - expires headers
Trang 113caching
Trang 114Nginx - 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
Trang 116websockets
Trang 118http/2
Trang 119Nginx - http/2
listen 443 http2 ssl; # managed by Certbot
/etc/nginx/sites-available/default
https://http2.akamai.com/demo
Trang 120Nginx - http/2
Trang 121Redirect request to new url
location /help {
return 301 https://developer.mozilla.org/en-US/; }
/etc/nginx/sites-available/default
Trang 122Part 6
databases
● Database types
● MySQL
Trang 123Database types
Trang 124MySQL
Trang 126Database 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
Trang 127https://github.com/mysqljs/mysql
Trang 129Dedicated Server
Trang 130the cloud
Trang 133Containers
Trang 135Installing Postgres on Docker
Trang 136https://www.linux.com/news/8-open-source-CONTAIN ER-ORCHESTRATION-TOOLS-KNOW
Trang 137Automating deployments
Trang 139HOORAY!!