Linux Server setup

Node.js, npm, MongoDB

In this chapter, we will set up web applications based on Node.js. We will first install and configure all components. Then we will program our application using frameworks or so-called middleware.

Node.js is a server-side JavaScript runtime environment. In conjunction with npm, the package manager of Node.js, numerous modules can be loaded into your own application. Node.js itself and all modules on are open source.

In connection with Node.js, the use of the NoSQL database MongoDB is very popular. The community version is free and quite sufficient for our purposes.

I will create the web application in the folder /var/www/ We had already prepared that in Subdomain Server Block (dev). Be sure to adjust that accordingly for yourself at the appropriate point.

Install Node.js and npm with apt

Unlike other installation routines, npm is not part of Node.js for package manager installations. Therefore, we will install both separately.

As usual, we install Node.js with apt:

__$ sudo apt install -y nodejs

With the following command we can query the installed version of Node.js:

__$ node -v

A version number should appear as output, otherwise something went wrong during installation.

We proceed similarly for npm. First the installation with apt:

__$ sudo apt install -y npm

And testing by the version output:

__$ npm -v

Reconfigure npm for global packages

Those who did not follow the tutorial and run everything as root user can skip the following section.

With npm there is the possibility to install modules globally, which can then be used system-wide. Since we installed Node.js as root user, but created another user tom for security reasons, we need to change the location of the global modules and create an environment variable. This allows tom to create global modules in his user directory. We avoid access problems with this, because otherwise the packages would only be accessible with sudo.

First, we create a hidden folder for npm packages underneath the home directory of tom:

__$ mkdir -p ~/.npm/lib

(In Linux, folders and files whose names begin with a period (.) are not displayed. They can be output with the -a parameter: ls -a)

We change the default path for global npm packages with the npm config set command:

__$ npm config set prefix ~/.npm

We add an environment variable to the current user's Bash configuration file .bashrc:

__$ echo 'export PATH="$PATH:$HOME/.npm/bin"' >> ~/.bashrc

And let the system re-read the bash configuration file:

__$ . ~/.bashrc

With these settings, Node.js first looks for modules inside the project in the node_modules directory and if nothing is found there, in the home directory under ~/.npm.

The actual location would otherwise have been /usr/local/npm.

Alternatively, we could have shared the npm system folder /usr/local/npm or parts of it to tom, but this would lead to chaotic conditions sooner or later.

Create database in MongoDB

We have already set up the MongoDB database in the Installing MongoDB chapter.

Therefore, we can now simply call the MongoDB console:

__$ mongo

Create a new database with the name MyDatabase:

__gt use MyDatabase;

A newly created database is not displayed by show databases; as long as it is empty. Therefore, we create a collection and delete it again immediately. This step is actually unnecessary, it should only be mentioned here so that there are no misunderstandings.

__gt db.MyCollection.insert({ key: "val" });
__gt db.MyCollection.drop();