Deploying React to Linux Server with Git Push

No Comments

Selected VPS: Linode, 1GB Ram, 20 GB SSD, 1 TB transfer
OS: Ubuntu 17.04
Web Server: Ngnix
If you’d like to try Linode, I would greatly appreciate using this referral link – Linode: SSD Cloud Hosting & Linux Servers

Start with regular updates

apt-get update && apt-get upgrade

Set up fail2ban and Firewall

I’m installing fail2ban 0.10 since it supports ipv6. At the time of this post, it is not available as a regular package.

wget https://github.com/fail2ban/fail2ban/archive/0.10.0.tar.gz
tar -xvzf 0.10.0.tar.gz
python3 setup.py install

#To enable fail2ban as an automatic service, copy the script for your distro from the files directory to /etc/init.d.

cp files/debian-initd /etc/init.d/fail2ban
update-rc.d fail2ban defaults
service fail2ban start

#Add local jail
awk '{ printf "# "; print; }' /etc/fail2ban/jail.conf | sudo tee /etc/fail2ban/jail.local
vim /etc/fail2ban/jail.local

uncomment sshd section and add
enabled = true

sudo apt-get install sendmail iptables-persistent
sudo service fail2ban start

Firewall ( allow established connections, traffic generated by the server itself, traffic destined for our SSH and web server ports. We will drop all other traffic):

sudo service fail2ban stop
sudo iptables -A INPUT -i lo -j ACCEPT
sudo iptables -A INPUT -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPT
sudo iptables -A INPUT -p tcp --dport 22 -j ACCEPT
sudo iptables -A INPUT -p tcp -m multiport --dports 80,443 -j ACCEPT
sudo iptables -A INPUT -j DROP

# easy way to rate-limit ssh with ufw:
# technically, we could do all of the iptables stuff with ufw
ufw enable
ufw limit ssh

If using IPv6:

ip6tables -A INPUT -p tcp --dport 80 -j ACCEPT
ip6tables -A INPUT -p tcp --dport 443 -j ACCEPT
ip6tables -A INPUT -p tcp --dport 22 -j ACCEPT # (replace with your undisclosed port)
ip6tables -A INPUT -p icmpv6 -j ACCEPT
ip6tables -A INPUT -j REJECT
ip6tables -A FORWARD -j REJECT

View iptables rules:

sudo iptables -S

Save iptables rules:

sudo dpkg-reconfigure iptables-persistent
sudo service fail2ban start

SSH

vim /etc/ssh/sshd_config

#Add or uncomment (if using Ubuntu < 17.04)
protocol 2

#Add allowed ciphers
Ciphers aes128-ctr,aes192-ctr,aes256-ctr
KexAlgorithms ecdh-sha2-nistp256,ecdh-sha2-nistp384,ecdh-sha2-nistp521,diffie-hellman-group-exchange-sha256
MACs hmac-sha2-256,hmac-sha2-512

Restart and test ssh config:

service sshd restart
#returns nothing if everything configured properly
sshd -t

NGINX

sudo apt-get install software-properties-common
sudo add-apt-repository ppa:nginx/stable
sudo apt-get install nginx
service nginx status

Update /etc/nginx/sites-enabled/default

root /var/www/html/your_site;

location / {
# Some comments...
try_files $uri /index.html;   # ADD THIS
}

sudo service nginx restart

Installing React Dependencies

# install yarn
curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | sudo apt-key add -
echo "deb https://dl.yarnpkg.com/debian/ stable main" | sudo tee /etc/apt/sources.list.d/yarn.list
sudo apt-get update && sudo apt-get install yarn
 
#install node (apt-get repo has an older version of Node)
curl -sL https://deb.nodesource.com/setup_8.x | sudo -E bash -
sudo apt-get install -y nodejs
sudo apt-get install -y build-essential

GIT Push Deploy

Let’s set up git on the server

apt-get install git-core
mkdir repos && cd repos
mkdir your_site.git
cd your_site.git
git init --bare

Set up a post-push hook
cd /repos/your_app.git/hooks
touch post-receive

#!/bin/bash -l

GIT_REPO=$HOME/repos/your_app.git
TMP_GIT_CLONE=$HOME/tmp/git/your_app
PUBLIC_WWW=/var/www/html

git clone $GIT_REPO $TMP_GIT_CLONE
cd $TMP_GIT_CLONE
yarn install
yarn build
rm -rf $PUBLIC_WWW/your_app_bup
mv $PUBLIC_WWW/your_app $PUBLIC_WWW/your_app_bup
cp -a build/. $PUBLIC_WWW/your_app
rm -Rf $TMP_GIT_CLONE
exit

Run on post-receiv:

chmod +x post-receive

On your local machine:

git remote add linode [email protected]_server_address:repos/your_app.git
git push linode master
Categories: git, Linux, React

Setting Up My Mac Dev Environment

No Comments

Set up a package manager: Homebrew

Istall iterm2

ZSH

Replace bash with zsh (it offers some nice plugins and themes).

brew install zsh zsh-completions

Install oh-my-zsh – “A delightful community-driven (with 1,000+ contributors) framework for managing your zsh configuration.”

sh -c "$(curl -fsSL https://raw.githubusercontent.com/robbyrussell/oh-my-zsh/master/tools/install.sh)"

To update oh-my-zsh, run upgrade_oh_my_zsh in zsh shell.

Pick your set of plugins (edit .zshrc file):
plugins=(git brew gem ruby rvm rails yarn npm)

Pick a theme (.zshrc):

ZSH_THEME="af-magic"

You can checkout available <a href="https://github.com/robbyrussell/oh-my-zsh/wiki/themes" target="_blank">themes</a> and <a href="https://github.com/robbyrussell/oh-my-zsh/wiki/Plugins" target="_blank">plugins</a>

<h3>Add SSH Keys</h3>
<a href="https://help.github.com/articles/generating-a-new-ssh-key-and-adding-it-to-the-ssh-agent/" target="_blank">Set up SSH keys for Github</a>
Add generated ssh key to your keychain, so you don't have to reenter your ssh passphrase:

ssh-add -K ~/.ssh/id_rsa

If you're on OSX Sierra, then you need another step:
create ~/.ssh/config with this content:

Host *
  UseKeychain yes
  AddKeysToAgent yes
  IdentityFile ~/.ssh/id_rsa

Still run ssh-add -K ~/.ssh/id_rsa

Rails/Ruby/RVM/rbenv

If using rvm with zsh, add this to your .zshrc:

[[ -s "$HOME/.rvm/scripts/rvm" ]] && . "$HOME/.rvm/scripts/rvm"

If you use rbenv, you may need to add this to your .zshrc:

export PATH="$HOME/.rbenv/shims :$PATH"
eval "$(rbenv init -)"

Exercism

For exercism zsh completions, add

if [ -f ~/.config/exercism/exercism_completion.zsh ]; then
. ~/.config/exercism/exercism_completion.zsh
fi

And run

$ mkdir -p ~/.config/exercism/
$ curl http://cli.exercism.io/exercism_completion.zsh > ~/.config/exercism/exercism_completion.zsh

git standup

git-standup

brew install git-standup

fzf is a general-purpose command-line fuzzy finder

https://github.com/junegunn/fzf#key-bindings-for-command-line

brew install fzf
# Install shell extensions
/usr/local/opt/fzf/install

autojump - a faster way to navigate your filesystem

https://github.com/wting/autojump

brew install autojump

# add autoload to .zshrc
[[ -s $(brew --prefix)/etc/profile.d/autojump.sh ]] && . $(brew --prefix)/etc/profile.d/autojump.sh

fkill - Fabulously kill processes

https://github.com/sindresorhus/fkill-cli
https://github.com/SamVerschueren/alfred-fkill

npm install --global fkill-cli
npm install --global alfred-fkill

Mac apps

Git alias (add to .gitconfig)

Show recent branches:
[alias]
recent = "for-each-ref --sort=-committerdate --count=10 --format='%(refname:short)' refs/heads/"

Atom configs

Plugins:

  • Pigments
  • Project Manager
  • Atom Beautify
  • got-definition
  • language-elixir
  • autocomplete-elixir
  • last-cursor-position
  • linter-lixirc
  • linter-eslint
  • liner-rubocop
  • prettier-atom

Integration with alfred:

npm install -g alfred-atom.
Categories: Development Setup, git

Analyzing Git with GitInspector

No Comments

Pretty cool tool for analyzing your Git repos – GitInspector. Thanks to @zachdevelop for discovering, figuring out how to use, and sharing it.

Here’s a sample command for analyzing some C#, JavaScript, .NET web services, ASP.Net, and F#:

Windows:

py C:\pathToGitInspector\gitinspector.py --format=html -r -m --file-types="cs,js,asax,ascx,asmx,aspx,cshtml,fs" | Out-File C:\git_output.html

Mac:

gitinspector.py --format=html -f="coffee,rb,js,json,feature,haml,erb,html" -r > inspector.html
Categories: git