Full Stack
for
Frontend Engineers 2
Jem Young
Senior Software Engineer
Serious Business
Things you’ll learn
Full Stack For Frontend Recap
Recap
domain
IP address
Recap
ping
traceroute
vi
Recap
(your sweet new server)
Recap
Why full stack?
Part 1
Server setup
Create a server
Server setup
Server setup - Node
update apt repo for nodejs
$ curl -sL https://deb.nodesource.com/setup_6.x | sudo -E bash -
Server setup - Node
check npm directory
install nodejs and npm
$ npm config get prefix
$ sudo apt install nodejs
If the npm directory is not /usr/local, follow instructions here
WARNING
Server setup - Node
Server setup - Node
install forever module
$ npm i -g forever
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
Part 2
Server security
Server security
Control access
Secure your applications
Server security - add ssh key
create a new directory
paste public key into authorized_keys file
$ mkdir -p ~/.ssh
$ vi ~/.ssh/authorized_keys
Be sure to test logging in with your new user
WARNING
Server security
Server security - firewalls
nmap
Exercise
nmap
$ nmap YOUR_SERVER_IP
Tales from real life
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)
Creating iptable rules
Create an iptable rule to block all outgoing HTTP connections
Creating iptable rules
iptables -p tcp --dport 80 -j REJECT
Creating iptable rules
Create an iptable rule to only allow icmp connections on port 892 from the IP address 192.0.0.1
Creating iptable rules
iptables -A INPUT -s 192.0.0.1 -p icmp --dport 892 -j ACCEPT
There has to be a better way!
Server security - ufw
ufw - uncomplicated firewall
$ sudo ufw allow ssh
$ sudo ufw enable
Create ufw rules
Create a ufw rule to block all outgoing HTTP connections
Creating ufw rules
ufw reject out http
Server security - firewalls
Server security - firewalls
Automatic Updates
Server security - update software
$ sudo apt install unattended-upgrades
Server security - update software
/etc/apt/apt.conf.d/20auto-upgrades
Server security - update software
/etc/apt/apt.conf.d/50unattended-upgrades
Fail2ban
Exercise
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
If you misconfigure fail2ban, you can lock yourself out of your server!
WARNING
Part 3
advanced shells
search file names
search file contents
Finding things
grep
find
Finding things
find
find
directory
option
file/folder
useful options
Finding things
find
find
Exercise
Find all log files in /var/log/
$ find /var/log/ -type f -name *.log
Find all empty files in /etc
$ find /etc -type f -empty
Find all directories with the word log
$ find / -type d -name log
Searching contents
search inside gzip file
grep
grep - global regular expression print
$ grep -i ‘jem’ /var/www
grep
options
search
expression
directory
$ zgrep FILE
Find running node processes
ps aux | grep node
Redirection
Redirection operators
Write to file
ps aux > foo
What does this do?
foo < bar > baz
Shells
What is a shell?
What is a shell?
show current shell
shell
application
kernel
$ echo $0
Changing shells
Exercise
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
Differences between shells
Shell scripting
Why shell scripting
Bash scripting
Exercise
Shell scripting - bash
$ vi load.sh
#!/bin/sh
cat /proc/loadavg | awk '{print $1"-"$2"-"$3}'
load.sh
Get load average
cat /proc/loadavg
awk '{print $1"-"$2"-"$3}'
Extract the 1st, 2nd, and 3rd columns of data
load average
1 minute
5 minute
15 minutes
chmod
$ sudo chmod 755 ./load.sh
Make executable
Creating a shell script with Node
Exercise
Node shell scripting - setup
create a workspace folder
move into workspace folder
create index.js
$ mkdir ~/workspace
$ cd ~/workspace
$ touch index.js
Node shell scripting - setup
initialize project
add reference to script
$ npm init
$ vi package.json
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);
});
Part 4
HTTPS
Nginx setup
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
Why HTTPS
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
HTTPS
$ sudo certbot --nginx
Use certbot to get certificate
$ sudo certbot renew --dry-run
Test auto renew
How to do we run periodic tasks?
hour
minute
day of month
month
day of week
command to execute
cron
Exercise
cron
Open crontab for editing
$ sudo crontab -e
Renew certificate every week at 12pm on Monday
Part 5
Nginx tuning
Nginx - gzip
Nginx - gzip
/etc/nginx/nginx.conf
Nginx - gzip
increase compression level
gzip_comp_level 6
/var/etc/nginx/nginx.conf
Expires headers
Nginx - expires headers
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
Nginx - expires headers
caching
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
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
websockets
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
http/2
Nginx - http/2
listen 443 http2 ssl; # managed by Certbot
/etc/nginx/sites-available/default
Nginx - http/2
Redirect
Redirect request to new url
location /help {
return 301 https://developer.mozilla.org/en-US/;
}
/etc/nginx/sites-available/default
Part 6
databases
Database types
relational
non-relational
SQL
NoSQL
MySQL
Exercise
MySQL
Install mysql
$ sudo apt install mysql-server
$ mysql_secure_installation
$ mysql -u root -p
Run setup script
Login as root
Database tips
Part 7
Containers and more
Containers
Dedicated Server
VPS
VPS
VPS
VPS
Containers
the cloud
Containers
Dedicated Server
VPS
node
mysql
nginx
Containers
Containers
Automating deployments
Automating deployments
HOORAY!!
Links
Part 1
Bash course