standards for setting up Ubuntu, node, MongoDB, nginx, pm2 env for stagging and production with one server over an ip address
0.0.1
$ ssh root@ipnumberadd users to group
adduser simonadd user to sudo group:
gpasswd -a simon sudo add ssh key to user. on local pc:
cat .ssh/ids_rsa.pub if key doesn't exist generate one:
ssh-keygen- copy id_rsa.pub key
on the server as root switch user:
su - simoncreate .ssh dir & .ssh/authorized_keys file, paste the ida_rsa.pub there:
mkdir .ssh
chmod 700 .ssh
vim .ssh/authorized_keyschange premissions:
chmod 600 .ssh/authorized_keysrestart ssh service to apply changes
service ssh restartallow ssh:
sudo ufw allow sshallow specific ports for ssh, http, ssl/tls:
sudo ufw allow 4444/tcp
sudo ufw allow 80/tcp
sudo ufw allow 81/tcp
sudo ufw allow 443/tcpshow allowed and enable firewall
sudo ufw show added
sudo ufw enableconfigure server timezone
sudo dpkg-reconfigure tzdata- a menu will open, choose your city
configure NTP to stay in sync with other servers:
sudo apt-get update
sudo apt-get install ntp####nodejs, npm, express, bower:
sudo apt-get update
sudo apt-get install nodejs
sudo apt-get install npm
npm install express -g
npm install bower -g####MongoDB
import public key
sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 7F0CEB10create a list file:
echo "deb http://repo.mongodb.org/apt/ubuntu "$(lsb_release -sc)"/mongodb-org/3.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-3.0.listreload
sudo apt-get updateinstall stable:
sudo apt-get install -y mongodb-orgstart
sudo service mongod startinstall
sudo apt-get update
sudo apt-get install nginxstart
sudo service nginx startCreate the file yourdomain at /etc/nginx/sites-available/:
vim /etc/nginx/sites-available/yourdomainsomething like:
#the IP(s) on which your node server is running. I chose port 3000 for production and 8000 for stagging.
# the nginx server instance
server {
listen 80;
server_name 104.236.241.255;
access_log /var/log/nginx/production.log;
# pass the request to the node.js server with the correct headers
location / {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_set_header X-NginX-Proxy true;
proxy_pass http://127.0.0.1:3000/;
proxy_redirect off;
}
}
server {
listen 81;
server_name 104.236.241.255;
access_log /var/log/nginx/stagging.log;
location / {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_set_header X-NginX-Proxy true;
proxy_pass http://127.0.0.1:8000/;
proxy_redirect off;
}
}- save and quit
link your file to site-enabled to apply changes:
cd /etc/nginx/sites-enabled/
ln -s /etc/nginx/sites-available/yourdomain yourdomainrestart
sudo /etc/init.d/nginx restartone server as root:
sudo npm install pm2 -gas user (simon):
try:
pm2 listif error (EACCES, permission denied ) then you have to give permissions to user:
sudo chmod -R 777 .pm2change .pm2 ownership
chown nobody:nogroup -R .pathrun an app:
pm2 start bin/wwwsudo env PATH=$PATH:/usr/bin pm2 startup ubuntu -u simonthen save processes:
pm2 save- now when rebooting system, your app should run on start
add ecosystem.json file to your project (example):
{
apps: [
{
name: "heatinc-stagging",
script: "bin/www",
env: {
NODE_PORT: 8000,
env: "production"
}
}
],
deploy: {
stagging: {
user: "simon",
host: "104.236.241.255",
ref: "origin/master",
repo: "[email protected]:Digitiv-Inc/heat-inc.git",
path: "~/www/stagging",
"post-deploy": "npm run deploy; export NODE_PORT=8000; pm2 startOrRestart ecosystem.json -f --env production",
env: {
NODE_PORT: 8000
}
}
}
}push setup to server (remotely):
pm2 deploy ecosystem.json stagging setupdeploy app on server (remotely):
pm2 deploy ecosystem.json stagging- now on server your stagging app should be running
- to check
pm2 list