Installation [ Old ]
All the steps required to deploy Nesula on a node app server
Note: we will be using Linux user 'ifactory' with DNS entries for nesula.ifactory.co and www.nesul... to ths server.
1. Pre-Steps
Have a Node Server prebuilt - see the other setup docs.
2. Create Folder
$ mkdir /var/www/nesula.ifactory.co
$ cd /var/www/nesula.ifactory.co
3. Install Nesula Repository
$ git init
$ git clone https://github.com/iFactoryDigital/Nesula.git [ When prompted enter Github username & password ]
New Github Clone Method
Create an SSH Key and add to your account. Note Github.com only excepts OpenSSH key's. To convert your PuttyGen my-ssh-key.pub ceate a new file my-ssh-key.open-ssh.pub and add the following:
ssh-rsa
TBC - SSH is too much bother while clone https:// still functions
4. Download Dependencies
$ cd Nesula [ enter working directory ]
NestJS Dependencies
$ cd /var/www/nesula.ifactory.co/Nesula/nest-api
$ npm install
Angular Dependencies
$ cd /var/www/nesula.ifactory.co/Nesula//angular-app
$ npm install
6. Database and ORM Config
If you are using Postgres on this server then ensure a staging or production database has been created e.g. 'nesula_stage'
Rename the existing ormconfig.json to ormconfig-dev.json
Create a new ormconfig.json for the stage/prod environment and update with correct host, username, password and database.
5. Build App
$ cd /var/www/nesula.ifactory.co/Nesula/nest-api
$ npm run build
$ cd /var/www/nesula.ifactory.co/Nesula//angular-app
$ npm run build
7. Setup Virtual Hosts
$ sudo touch /etc/nginx/sites-available/nesula.ifactory.co.conf [ create empty .conf file ]
$ sudo nano /etc/nginx/sites-available/nesula.ifactory.co.conf [ edit empty .conf file ]
Paste in the Nginx configuration
server {
server_name nesula.ifactory.co www.nesula.ifactory.co;
root /var/www/nesula.ifactory.co;
index index.html;
location / {
proxy_pass http://localhost:3000; #whatever port your app runs on
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
}
Then confirm Ngin config and restart
$ sudo ln -s /etc/nginx/sites-available/nesula.ifactory.co.conf /etc/nginx/sites-enabled/nesula.ifactory.co.conf [ Create symlink to new site ]
$ sudo nginx -t [ Confirm Nginx .conf files are sytnax error free ]
$ sudo systemctl restart nginx [ restart nginx for .conf updates ]
5. Install SSL Cetificate
This assumes Certbot is already installed and firewall allows HTTPS. See Hello.js node setup document for more.
$ sudo certbot --nginx -d nesula.ifactory.co -d www.nesula.ifactory.co
6. Serve the App
Change the working directory to run commands:
$ cd /var/www/nesula.ifactory.co/Nesula/nest-api
Run in Staging 'stage' with Node
Ensure this line is in the nest-api package.json:
"scripts": {
"start:stage": "node dist/main.js",
}
To start the app in stage:
$ npm run start:stage [ Run app in stage ]
Run In Production 'prod' with PM2
Ensure this line is in the nest-api package.json:
"scripts": {
"start:prod": "pm2 start dist/main.js",
}
To serve app in prod:
$ npm run start:prod
$ pm2 startup [ Generate an active startup script to restart current processes ]
7. Monitor App Performance - OPTIONAL
$ ps ax | grep node [ If run with node - list all node processes ]
$ pm2 monit [ If run with PM2 - Displays the PM2 real-time monitoring UI ]
$ htop [ Display real-time per CPU usage and active processes ]
8. Environment Variables, config & NODE_ENV
It is import to correctly set the NODE_ENV environment variable.
Nesula makes use of the NestJS ConfigModule and ConfigService. ConfigService imports the following config files:
/nest-api/src/_common/config/configuration.ts [ App configuration ]
/nest-api/.env [ Environment specific variables ]
The configuraiton.ts file values can either be populated by values from the .env file or if they are not environment specific hard coded directly.
Environment variables are available in configuration.ts as follows:
{
...
app-var: process.env.NES_APP_VAR [ NES_APP_VAR is from .env file ]
app-name: 'App-name' [ value hardcoded in file ]
}
Nesula has four config files:
.env [ ONLY environment file imported by Nesula ConfigService ]
.env.development [ development environment config ]
.env.test [ staging or test environment config ]
.env.production [ production environment config ]
Set config file for environment
To deploy a config file in a particular environment we replace the .env file. For Example in production:
$ cd /var/www/nesula.ifactory.co/Nesula/nest-api
$ rm .env [ delete existing .env file ]
$ cp .env.production .env [ copy production file to .env file ]
$ rm .env.development .env.test [ delete irrelevant .env files]
To simplify this deployment step we have created the following commands:
# Defined in nest-api/package.json
$ npm run deploy:dev [ set .env to .env.development ]
$ npm run deploy:test [ set .env to .env.test ]
$ npm run deploy:prod [ set .env to .env.production ]
We can also manually set the process.env.NODE_ENV from the server environment variable at run time
To set NODE_ENV at the command line:
$ export NODE_ENV=stage [ set server environment variable 'NODE_ENV' to 'stage' ]
$ echo $NODE_ENV [ show value of environment variable ]
$ printenv [ show all environment variables ]
$ unset NODE_ENV [ Delete environemtn variable ]
9. Check DNS & Reverse DNS Settings
$ dig +short $URL [ check A Record with $URL Place holder ]
$ dig +short nesula.ifactory.co [ check A Record with actual domain address ]
$ dig -x $IP +short [ Check rDNS / reverse DNS of $IP ]
$ dig -x 172.105.161.59 +short [ Check rDNS / reverse DNS for actual IP address ]
Last updated
Was this helpful?