Skip to content

Instantly share code, notes, and snippets.

@marktyers
Last active April 18, 2016 17:53
Show Gist options
  • Save marktyers/fc8f058e6428e53d02580151e895b909 to your computer and use it in GitHub Desktop.
Save marktyers/fc8f058e6428e53d02580151e895b909 to your computer and use it in GitHub Desktop.

make sure you ssh into the correct IP address (not hostname)

Resize Partition

df -h
fdisk /dev/mmcblk0
# options p (note start of partition 2) d 2 n p 2 (enter start value, default end) w
reboot
resize2fs /dev/mmcblk0p2
df -h

Install Packages

apt-get update -y && apt-get dist-upgrade -y && apt-get install -y apt-utils && apt-get install -y rpi-update sudo nano tree git curl zip unzip nodejs && apt-get autoremove -y && apt-get clean -y

Use the apt-get purge -y packagename command to remove a package including data.

Configure NodeJS

sudo ln -s /usr/bin/nodejs /usr/bin/node
node -v
sudo curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.29.0/install.sh | bash
nvm list-remote
sudo nvm install 5.9.1
nvm install 5.9.1
nvm alias default 5.9.1
node -v
npm -v
sudo npm install -g forever
npm
npm install -g forever

Secure Tunnel

Download, unzip and move

wget https://bin.equinox.io/c/4VmDzA7iaHb/ngrok-stable-linux-arm.zip
unzip ngrok-stable-linux-arm.zip
sudo mv ngrok /usr/local/bin/
./ngrok authtoken xxxxxxxxxx
./ngrok http 8080

MySQL

sudo apt-get install -y mysql-server

Create a ~/.my.cnf option file so we don't have to enter a username and password each time. Then use `` to restart MySQL. You can do this on your local machine as well but the file on OSX is /etc/my.cnf

[client]
user = root     
password = "mypassword"
database = "autonect"

Then sudo reboot to restart the server. Once logged in you can use the mysql command without any parameter to log in.

mysql -e "CREATE DATABASE IF NOT EXISTS autonect;"
mysql -e "GRANT INSERT, UPDATE, DELETE, CREATE ON autonect.* TO autonect IDENTIFIED BY 'mypassword';"
cd api
mysql < 'sql/dropschema.sql'
mysql < 'sql/schema.sql'
mysql < 'sql/users.sql'

Changing the Host Name

change host name in two files and restart service

nano /etc/hostname
nano /etc/hosts

reboot

make sure you use nano ~/.ssh/known_hosts to remove the IP address from the known host file on the local machine

Configuring the Nano Editor

Nano supports colour syntax highlighting, All the syntax files are in the /usr/share/nano/ directory.

asm.nanorc    debian.nanorc   java.nanorc      nano-menu.xpm  perl.nanorc    sh.nanorc
awk.nanorc    fortran.nanorc  makefile.nanorc  nanorc.nanorc  php.nanorc     tcl.nanorc
cmake.nanorc  gentoo.nanorc   man.nanorc       objc.nanorc    pov.nanorc     tex.nanorc
c.nanorc      groff.nanorc    mgp.nanorc       ocaml.nanorc   python.nanorc  xml.nanorc
css.nanorc    html.nanorc     mutt.nanorc      patch.nanorc   ruby.nanorc

Edit the config file ~/.nanorc and add the following (shell scripting example).

## Shell Scripting
include /usr/share/nano/sh.nanorc
set tabsize 2

Additional syntax files can be downloaded from https://github.com/scopatz/nanorc

Routing

Your nodejs script will ru on port 8080 but we want to access this externally on the default port 80.

sudo iptables -A PREROUTING -t nat -i eth0 -p tcp --dport 80 -j REDIRECT --to-port 8080

To undo this replace the -A flag with a -D flag.

sudo iptables -D PREROUTING -t nat -i eth0 -p tcp --dport 80 -j REDIRECT --to-port 8080 

Environment Vars

Environment vars are used to store server-specific values. For example rather than specifying a port to run nodejs on in the script we can request the correct port from the server. They can also be used to flag up the type of server such as test or production which can be used by the scripts to determine their behaviour.

We can use the printenv command to view all the environment vars. If we pass a name parameter such as printenv SHELL it prints a single var.

We can add temporary vars in the shell. These can be added to the ~/.bashrc file which means they get loaded when the shell loads. We can either edit the file and append manually or use the redirection operator >>. We need to reload the file for the changes to take effect.

export SERVER_STATUS=test
echo "export SERVER_STATUS=test" >> ~/.bashrc
echo "export PORT=8080" >> ~/.bashrc
source ~/.bashrc

These can be accessed in NodeJS by using process.env.SERVER_STATUS

Creating a Git Account

useradd -s /bin/bash -m -d /home/git -c "Running Node" git
passwd git
sudo usermod -a -G sudo git

Public Keys

Log in as the git user then use ssh-keygen -t rsa to generate a public private keypair.

Log out and use ssh-copy-id -i ~/.ssh/id_rsa.pub git@autonect to exchange keys with the server. On a Mac you will need to use brew install ssh-copy-id beforehand.

Try logging in as git using ssh git@autonect, you wont be prompted for a password!

mkdir api
mkdir api.git
cd api.git/
git init --bare
cd hooks/
nano post-receive
chmod +x ~/api.git/hooks/post-receive

The file should look something like this,

#!/bin/sh
GIT_WORK_TREE=/home/git/api git checkout -f
cd ~/api
forever stop 0
forever start server.js

Installing Global NodeJS Packages

sudo apt-get install -g forever

Adding Server as a New Remote

git remote -v
git remote add web git@autonect:/home/git/api.git
git remote -v
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment