Setup a Node / Express, Nginx, PM2, Postgres App Server
1. Pre-Steps
Build an Linux server first. See Gist Ubuntu Server Setup
SSH into server as non-root user - typically 'ifactory'
Have password for non-root user as its requested the first time 'sudo' is used
Makre sure server is up to date
$ sudo apt update [ Ensure package manager is up to date ]
2. Setup NodeJS
$ cd ~
$ curl -sL https://deb.nodesource.com/setup_14.x -o nodesource_setup.sh [ Download Node V14 into users directory ]
$ sudo bash nodesource_setup.sh [ Run node setup script. Enter password for user. ]
$ sudo apt install nodejs [ Install node ]
$ node -v [ Check node version ]
$ npm -v [ Check npm version ]
$ sudo apt install build-essential [ Install node build essential - required for some npm packages ]
3. Setup PM2
$ sudo npm install pm2@latest -g [ Install pm2 - node production process runner ]
$ pm2 startup systemd [ Configures a startup script to launch PM2 and its managed processes on server reboot ]
This previous command generates the next command to run. Example output below for 'ifactory'.
$ sudo passwd postgres [ Change the Linux 'postgres' user password ]
$ su - postgres [ Switch to Linux Postgres user ]
$ psql -d template1 -c "ALTER USER postgres WITH PASSWORD 'new-password';" [ Change Database 'postgres' user password ]
C. Allow Remote Connections
Check firewall allows connections over default Postgres port 5432:
$ sudo ufw status verbose [ Displays firewall list ]
$ sudo ufw allow from 119.18.38.68 to any port 5432 [ Allow connections from iFactory office IP ]
Allow Postgress to listen to external connections:
Add a line below in the 'CONNECTIONS AND AUTHENTCIATION' section :
# listen_addresses = 'localhost' [ default value is commented out ]
listen_addresses = '*' [ listen on all IP's ]
Open config file
nano /etc/postgresql/12/main/pg_hba.conf
Comment out old line and add new line
# IPv4 local Connections
# host all all 127.0.0.1/32 md5
host all all 0.0.0.0/0 md5 [ Accept connectionson all IP's ]
Exit and restart postgres
exit [ exit out of postgres user and return to ifactory user ]
sudo service postgresql restart [ will request ifactory user password ]
5. Setup Nginx
A. Set ownership and permssions of www folder
$ sudo chown -R ifactory /var/www [ set ifactory owner of www folder ]
$ sudo chgrp -R www-data /var/www [ set www folder to be in group www-data ]
$ sudo adduser ifactory www-data [ add user to the www-data group that will be the app folder owner ]
$ grep ^www-data /etc/group [ show users in www-data group ]
B. Create website/app location
$ mkdir /var/www/example.com [ create folder for a domain ]
$ chown -R ifactory /var/www/example.com [ set ifactory owner of the folder ]
$ chmod 755 -R /var/www/example.com/ [ Set permissions ]
$ chmod g+s /var/www/example.com/ [ ensure any new files or folders inherit the set permisions ]
C. Install & Configure Nginx
$ sudo apt install nginx [ Install nginx package ]
$ sudo unlink /etc/nginx/sites-enabled/default [ Disable the default configuration file by removing the symlink ]
$ sudo touch /etc/nginx/sites-available/example.com.conf [ Create a new site config for example.com ]
$ sudo nano /etc/nginx/sites-available/example.com.conf [ Paste in site nginx config from above URL ]
$ sudo ln -s /etc/nginx/sites-available/example.com.conf /etc/nginx/sites-enabled/example.com.conf [ Create symlink to new site ]
$ sudo nginx -t [ Check nginx config syntax ]
$ sudo systemctl restart nginx [ Restart nginx ]
Related Commands
$ find . -type l -ls [ Show all symbolic links i.e symlinks ]
$ sudo nginx -t -c /etc/nginx/sites-available/example.com.conf [ Check nginx config syntax ]