Install Node + NPM:
curl -sL https://deb.nodesource.com/setup_9.x -o nodesource_setup.sh; sudo bash nodesource_setup.sh; sudo apt-get install nodejs; node -v; #just to verify it works
Set up your GIT and codebase:
First, generate and copy SSH keys to
~/.ssh/ folder on your computer and on the server, set their permissions to 600
On both your computer and the server, run this command to authenticate with GIT:
eval "$(ssh-agent -s)"; ssh-add ~/.ssh/gitlab
/www/yourproject directory and clone your Git repository into this folder*
mkdir /www git clone YOUR-REPOSITORY /www/$(hostname) cd /www/$(hostname)
*I call my project folder the same as my server name so below I can automatically reference it without having to remember what its called every time:
/www/$(hostname) To find the name of your server, run command
Automatically run commands on Login:
Copy the following to the bottom of this file, to have these commands run every time you SSH into the server:
eval "$(ssh-agent -s)" ssh-add ~/.ssh/gitlab mkdir -p /www/$(hostname) cd /www/$(hostname) git reset HEAD -\-hard; git pull ps cax | grep node echo "TIPS:" echo "vim /etc/nginx/sites-available/default" echo "service nginx restart"
Automatically run commands on startup or on schedule:
Add something like the following to the bottom of this file:
30 2 * * 1 root bash /www/ps-api/_cron/weekly.sh @reboot root bash /www/ps-api/_cron/db.sh @reboot root bash /www/ps-api/_cron/api.sh @reboot root bash /www/ps-api/_cron/deploy.sh
NOTE: the "/www/ps-api" in this path - replace this with the path to your project.
Unfortunately, the neat trick to automatically use the server name does not work in this Cron file.
Also, /www/YOUR-PROJECT/_cron/YOUR.sh assumes that your codebase uses this
_cron this folder.
Why do all this?
So that whenever the server starts, it will execute:
api.sh- to start your Node server
db.sh- to start your database and database admin panel
deploy.sh- to start your Node process which will listen for webhooks from Github - to automatically pull latest changes whenever your Github repository "master" branch has been updated
weekly.sh checks and updates the SSL Certificate - explained later in this book.
(api.sh, db.sh, deploy.sh explained in next pages as well)