buy
How to configure Node.js

Overview

This guide helps you to configure and install sample Node.js applications.

Create a simple Node.js Application

Create a hello.js file in /home/[Webuzo_Username]/public_html/[Your_Domain]/ directory and insert the following code in it:

var http = require('http');
http.createServer(function (req, res) {
  res.writeHead(200, {'Content-Type': 'text/plain'});
  res.end('Hello World\n');
}).listen(8080, 'PUBLIC_IP_ADDRESS');
console.log('Server running at http://PUBLIC_IP_ADDRESS:8080/');

Replace PUBLIC_IP_ADDRESS with your server's public IP address. You can also replace 8080 port with any other port according to your need.

Test Application

  • To test your application, run the below command in the terminal
$ node hello.js

Note: Running a Node.js application in this manner will block additional commands until the application is killed by pressing CTRL + C

This Node.js application simply listens on the specified IP address and port, and returns "Hello World" with a 200 HTTP success code.

  • In order to test the application via the browser just visit the below URL:
http://PUBLIC_IP_ADDRESS:8080

If you see the following output, the application is working properly and listening on the proper IP address and port:

Output: Hello World 

If you do not see the proper output, make sure that your Node.js application is running, and configured to listen on the proper IP address and port.

Install & Configure Ghost Application

Follow the steps given below in order to configure and install Ghost. It is a blog application which runs on Node.js

  • You can install the Ghost Application in any directory you want, but for simplicity we will install it in the public_html folder (make sure you are logged in as ROOT)
cd /home/[WEBUZO_USERNAME]/public_html
mkdir ghostdir
cd ghostdir
npm init
npm i ghost@latest --save

Press CTRL+C to exit if it warns you about not having permissions to access and move on to the next steps. Don't worry about the warnings. This will create the directory, initialize a new node application inside that directory, install the ghost package and add it to the "package.json" description file. You can just hit enter and accept defaults when running npm init or enter real information if desired.

  • Ghost uses MySQL to store information so create a new Database and User in MySQL with limited permissions instead of using the Root MySQL user. First log into MySQL as your admin/root user
mysql -u MYSQLROOTUSER -pMYSQLROOTPASSWORD
  • Execute the commands below once you are logged in the MySQL CLI
create database GHOSTDBNAME;
create user 'GHOSTDBUSER'@'localhost' identified by 'GHOSTDBUSER_PASSWORD';
grant all privileges on GHOSTDBNAME.* to 'GHOSTDBUSER'@'localhost';
flush privileges;
quit;

Replace 'GHOSTDBNAME' and 'GHOSTDBUSER' with your own Database and Username.

  • Ghost also requires another application named knex-migrator to initialize it's own database structure, so let's install that now
npm install -g knex-migrator

Press CTRL+C to exit if it warns you about not having permissions to access and move on to the next steps.

If you run cat package.json you should see something like below depending on the values entered during the npm init dialog:

{
  "name": "ghostdir",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "ghost": "^1.21.2"
  }
}
  • Ghost requires a configuration file named config.production.json, you can copy a default one using the below command
cp node_modules/ghost/core/server/config/env/config.production.json config.production.json

The config.production.json will look something like below:

{
    "database": {
        "client": "mysql",
        "connection": {
            "host"     : "127.0.0.1",
            "user"     : "root",
            "password" : "",
            "database" : "ghost"
        }
    },
    "paths": {
        "contentPath": "content/"
    },
    "logging": {
        "level": "info",
        "rotation": {
            "enabled": true
        },
        "transports": ["file", "stdout"]
    }
}

Edit this file, add an entry for your site's URL and server config, and update the database config section with the name, user and password you chose in the MySQL CLI. For the server config, you'll also have to choose a unique port number for this Node application instance, something like 4200 or any other available port. If you're not sure what ports are already in use on your server, run netstat -tuplen to see.

Your server config file should now look like this. Replace GHOST_URL with something like this "http://Your_Primary_Domain:GHOST_PORT":

{
    "url": "GHOST_URL",
    "server": {
        "host": "127.0.0.1",
        "port": GHOST_PORT
    },
    "database": {
        "client": "mysql",
        "connection": {
            "host"     : "127.0.0.1",
            "user"     : "GHOSTDBUSER",
            "password" : "GHOSTDBUSER_PASSWORD",
            "database" : "GHOSTDBNAME"
        }
    },
    "auth": {
        "type": "password"
    },
    "paths": {
        "contentPath": "/ABSOLUTE/PATH/TO/GHOST/content"
    },
    "logging": {
        "level": "info",
        "rotation": {
            "enabled": true
        },
        "transports": ["file", "stdout"]
    }
}

Note: You must use the absolute path to your copied content directory, otherwise Ghost will continue to read and write files from the node_modules/ghost/content subdirectory instead, which would be overwritten if you upgrade the NPM package, losing all your uploads and theme changes. Make sure to remove the trailing slash on the absolute path as this can cause problems as well. For Eg : /home/[WEBUZO_USERNAME]/public_html/ghostdir/content

  • create an index.js file and enter the following
var ghost = require('ghost');
var path = require('path');
ghost().then(function (ghostServer) {
    ghostServer.start();
});
  • Ghost will need a local content directory for uploading images and modifying themes. Create a local copy from the Ghost NPM package using the following command
cp -r node_modules/ghost/content .

Make sure the copied content directory matches the contentPath variable you set in the config file.

  • Now execute the following commands to change ownership and give appropriate permissions
chown -R [WEBUZO_USERNAME]:[WEBUZO_USERNAME] /home/[WEBUZO_USERNAME]/public_html/ghostdir
find /home/[WEBUZO_USERNAME]/public_html/ghostdir -type d -print0 | xargs -0 chmod 0755
find /home/[WEBUZO_USERNAME]/public_html/ghostdir -type f -print0 | xargs -0 chmod 0644
  • Now you'll need to initiate the Ghost database structure by issuing the following command
NODE_ENV=production knex-migrator init --mgpath node_modules/ghost

Which should output the following if successful:

[2018-02-18 05:36:24] INFO Creating table: posts
[2018-02-18 05:36:24] INFO Creating table: users
[2018-02-18 05:36:24] INFO Creating table: roles
[2018-02-18 05:36:24] INFO Creating table: roles_users
[2018-02-18 05:36:24] INFO Creating table: permissions
[2018-02-18 05:36:24] INFO Creating table: permissions_users
[2018-02-18 05:36:24] INFO Creating table: permissions_roles
[2018-02-18 05:36:25] INFO Creating table: permissions_apps
[2018-02-18 05:36:25] INFO Creating table: settings
[2018-02-18 05:36:25] INFO Creating table: tags
[2018-02-18 05:36:25] INFO Creating table: posts_tags
[2018-02-18 05:36:25] INFO Creating table: apps
[2018-02-18 05:36:25] INFO Creating table: app_settings
[2018-02-18 05:36:25] INFO Creating table: app_fields
[2018-02-18 05:36:25] INFO Creating table: clients
[2018-02-18 05:36:25] INFO Creating table: client_trusted_domains
[2018-02-18 05:36:25] INFO Creating table: accesstokens
[2018-02-18 05:36:25] INFO Creating table: refreshtokens
[2018-02-18 05:36:25] INFO Creating table: subscribers
[2018-02-18 05:36:25] INFO Creating table: invites
[2018-02-18 05:36:26] INFO Creating table: brute
[2018-02-18 05:36:26] INFO Creating table: webhooks
[2018-02-18 05:36:26] INFO Model: Post
[2018-02-18 05:36:26] INFO Model: Tag
[2018-02-18 05:36:26] INFO Model: Client
[2018-02-18 05:36:26] INFO Model: Role
[2018-02-18 05:36:26] INFO Model: Permission
[2018-02-18 05:36:27] INFO Model: User
[2018-02-18 05:36:27] INFO Relation: Role to Permission
[2018-02-18 05:36:28] INFO Relation: Post to Tag
[2018-02-18 05:36:28] INFO Relation: User to Role
[2018-02-18 00:36:28] INFO Finished database init!
  • You should now be able to start the new Ghost installation using the following command
NODE_ENV=production node index.js

If everything is working normally, it should display something like the following:

[2018-02-18 05:38:02] WARN Theme's file locales/en.json not found.
[2018-02-18 05:38:03] INFO Ghost is running in production...
[2018-02-18 05:38:03] INFO Your blog is now available on https://GHOST_URL/
[2018-02-18 05:38:03] INFO Ctrl+C to shut down

Now point your browser to your GHOST_URL address, and you should see the default Ghost site up and running.

    Was this page helpful?
    Newsletter Subscription
    Subscribing you to the mailing list