Toggle navigation

Node.JS(MongoDB) Image Guide

Version 6.11.4

Websoft9 Node.js stack is a pre-configured, ready to run image for running Node.js application on Alibaba Cloud.

Node.js® is a JavaScript runtime built on Chrome’s V8 JavaScript engine. Node.js uses an event-driven, non-blocking I/O model that makes it lightweight and efficient. Node.js’ package ecosystem, npm, is the largest ecosystem of open source libraries in the world.

Environment Software(Node.js 6.11.4 LTS,WebPack3.8.1,PM2 2.7.2)
Node.js applicaton root directory: /data/wwwroot
Express demo program: /data/wwwroot/example

Application Server(Nginx1.10)
Nginx Configuration Files: /etc/nginx/sites-available/default
Nginx is used for reverse proxy

Database(MongoDB3.4.9)
MongoDB Configuration file: /etc/mongod.conf
MongoDB data directory: /data/mongodb

After the installation of Image,please verify it

  • Login on Alibaba Cloud console,get you Internet IP Address
  • Open you Chrome or Firefox on your local PC,visit the http://Internet IP Address,you can see the welcome information like “Welcome to Express”,if there no response from browser,please check the Security Group settings to ensure that port 80 is been allowed

Two usernames and passwords are required for application installation, application using, and application maintenance:

1、Mongo Database:No usename and password,you can set it by yourselft

2、Linux Server:Username is root or ubuntu,Password is set by youself when buying.
Please Using the Putty for ssh remote and WinSCP for SFTP

Note:You can reset the password of OS through Cloud Console if you have forgotten it

Connect to a Linux instance using Windows OS

This section uses PuTTY as an example. PuTTY can be downloaded here.

You can connect to a Linux instance via PuTTY as follows:

  1. Start Putty.exe.
  2. Enter the public IP address of the instance in Host Name (or IP address).
  3. Use the default port 22.
  4. Select SSH as Connection Type.
  5. Type a session name in Saved Sessions, and then click Save. In later logins, you may directly load the session without re-entering the IP address.
  6. Click Open to connect.
  7. Upon first connection, the following dialog box will be displayed. Click Yes.
  8. As prompted, enter the username and password for the Linux ECS instance. The password will not be displayed on-screen. Press the Enter key to complete connection to the instance.

When you connect your computer to the Linux instance successfully, you can operate the instance from your computer.

Use SFTP you can mange file,upload and download file,configure Server.

  1. Start WinSCP. Login Dialog will appear.
  2. Then select your File protocol(SFTP) on the dialog
  3. Enter your host name to Host name field, username to User name and password to Password like below
  4. You may want to save your session details to a site so you do not need to type them in every time you want to connect. Press Save button and type site name.
  5. When appear the dialog below,please select the “yes”
  6. You can manane Linux file below
  7. WinSCP can integrate the Putty and transfer the log in information to Putty
  8. Now you can use Putty(Not need to log in) from the menu bar of WinSCP

Following are the steps for adding your Node.js Applicaton:

  1. Upload your Node.js application to the directory:/data/wwwroot/,e.g.the assume folder name is myapps
  2. Using putty to log in Linux,then go the myapps directory and install the node.js application dependency package
    [root@iZbp1i4rjdsy216yq7lgucZ ~]# cd /data/wwwroot/myapps
    [root@iZbp1i4rjdsy216yq7lgucZ myapps]# npm install
    
  3. Modify the database configuration file if necessary
  4. Come back to the myapps directory and start up the index.js or app.js.Using the “pm2 list” command to show the state of application,if the application is “online” means is runing OK
    [root@iZbp1i4rjdsy216yq7lgucZ ~]# cd /data/wwwroot/myapps
    [root@iZbp1i4rjdsy216yq7lgucZ myapps]# pm2 start index.js 
    [root@iZbp1i4rjdsy216yq7lgucZ myapps]# pm2 list
  5. Then you shoul save the process of application and make it boot automatically start
    [root@iZbp1i4rjdsy216yq7lgucZ myapps]# pm2 save 
    [root@iZbp1i4rjdsy216yq7lgucZ myapps]# sudo pm2 startup 
    [root@iZbp1i4rjdsy216yq7lgucZ myapps]# pm2 save 
  6. Add a new reverse proxy conf file of Ngnix.(Like the /etc/nginx/sites-available/default below).Please make sure using the correct port(The default port is 3000)
    server {
            listen 80 default_server;
            server_name _;
    
            location / {
             proxy_pass http://127.0.0.1:3000;
             proxy_set_header Host $host;
             proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            }
    }
  7. Restart Nginx
    [root@iZbp1i4rjdsy216yq7lgucZ]# systemctl restart nginx.service

Please remove the Sample which is created by the express framework before adding new application of Node.js.Fllowing is the steps:

//delete the PM2 process of Sample
[root@iZbp1i4rjdsy216yq7lgucZ]# pm2 delete 0
//delete the PM2 init script
[root@iZbp1i4rjdsy216yq7lgucZ myapps]# pm2 unstartup systemd
//delete the have been saved PM2 file of process
[root@iZbp1i4rjdsy216yq7lgucZ myapps]# rm -rf /root/.pm2

If you want use Domain for this application,you should complete below steps:

  • Domain resolution if you want to use (Add an A record to the Internet IP Address of Server from your Domain Control Panel)
  • Modiy the Apache configuration file: /etc/nginx/sites-available/default

    server {
    ...
            listen 80 default_server;
            server_name _;//add you domain here
    ...
    }
    
  • Restart the services

You can use the command to manage the services for Server.Please choose the corresponding command mode according to different types of operating system

Ubuntu Linux

Nginx Restart

~# systemctl restart nginx

Routine backup (archives) of the database and application setup is essential to ensure failover is smooth.

Backup is based on the server snapshot automatic backup and manual local backup in two ways, both of which have their own advantages and disadvantages, it is recommended to use together

Automatic Backup by Snapshot of Server

IaaS provider have the “snapshot” function for Server, the snapshot is for the server disk. Snapshot tool can record the specified point in time the hard disk data, all backed up, and can achieve a key recovery.

If you have an automatic snapshot of the disk settings, the following scenes you can be more calm face:

  • Work a few days of the results, was mistakenly deleted
  • Hackers damage your site
  • The content has been messed up

Done a snapshot backup, a key to restore to the backup point of time state. That is, on-site reduction, very good function.

Different cloud vendor snapshot settings slightly different, specific reference:

This document is original from Websoft9.In order to ensure the seriousness and usability of the documentation, no one may misappropriate or modify the second distribution. The need for friends reproduced please mark the source, otherwise we reserve the right to give legal action.

We will periodically update the documentation. Please visit the online documentation for the latest version

This document URL: http://en.websoft9.com/xdocs/nodejs-mongodb-image-guide/
Support & Partnership: http://www.websoft9.com