Let’s Deploy a Node.js Application on a Linux Server
INDEX
Install nvm (Node Version Manager)
Install Node.js
Install Express
Install forever
Server Configuration
Summary


Install nvm (Node Version Manager)
Although it is possible to install Node.js directly using the yum command, we will instead use nvm (Node Version Manager), a tool that allows you to switch between Node.js versions as needed.
This makes it easier to adapt your environment to future development requirements and projects.
First, install nvm.
$ curl https://raw.githubusercontent.com/creationix/nvm/master/install.sh | sh
The installation of nvm is now complete.
Next, we will use nvm to install Node.js.
Before installing, let’s check which versions of Node.js are available.
The following command will display a list of installable versions.
$ nvm ls-remote
Install Node.js
Use nvm to install the desired version of Node.js.
At the time this article was originally published, version 7.10.0 was the latest release.
We will install Node.js version 7.10.0.
$ nvm install 7.10.0
Once the installation is complete, verify it using the following command.
$ node -v
nvm allows you to install multiple versions of Node.js.
For example, if you want to install version 7.5.0, simply run the same type of command with the target version number.
$ nvm install 7.5.0
After installation, you can switch between installed Node.js versions using the following command.
Although changing versions is not required for this tutorial, it is a good idea to try it once.
$ nvm use 7.5.0
Now run the version check command again.
The displayed version should now be 7.5.0.
$ node -v
Updating npm
When Node.js is installed, a tool called npm is installed as well.
npm is Node.js’s package manager and is required for installing various libraries, tools, and additional features.
Before moving on to the next step, update npm.
If npm is not installed correctly, you can also install it globally using the following commands.
$ npm update -g npm
or
$ npm install -g npm
Install Express
To make Node.js development and execution easier to understand and manage, we will use Express.
Express is one of the most popular frameworks for Node.js, with a large amount of documentation and community information available, making it relatively beginner-friendly.
Using the npm command that we updated and installed earlier, install Express globally.
$ sudo npm install express -g
After the installation is complete, check the installed version.
$ express --version
If the version information is displayed, the installation has completed successfully.
Create and Run a Sample Application
Now let’s start using Express.
In this tutorial, we will ultimately configure the application so it can be published on the web through nginx.
It is best to create the application directory in a structure that matches the operation and organization of your web server environment.
$ cd
$ express -e sample
$ cd sample
$ npm install
First, use the cd command to move to your desired directory, such as the location where your web applications are managed.
Next, the express command automatically creates an application named sample.
Move into the newly created sample directory using the cd command.
Then use the npm install command to locally install the required libraries and dependencies.
Now let’s run the application you created.
Use the following command to start it.
$ node app.js
You should see a message similar to:
Node.js is listening to PORT:3000
However, while the application is running in this state, the terminal will no longer accept other commands.
To improve this, we will introduce forever, a tool that keeps the application running in the background as a daemon process.
For now, stop the application by pressing Control + C.
What is daemonization?
Daemonization means keeping a program running in a standby state so it can respond whenever requests are received.
Install forever
Now install forever.
As before, use npm to globally install the forever package.
$ sudo npm install forever -g
After the installation is complete, start the application again using forever.
$ forever start app.js
The application should now start successfully and continue running in the background.
Once you have reached this point, proceed with the server configuration so the application can be accessed through a web browser.
Server Configuration
In this tutorial, we will configure the application to be published using a subdomain on an existing domain.
Be sure to configure your DNS and name server settings in advance.
From here, we will proceed with the nginx configuration.
*This tutorial assumes that the basic nginx setup has already been completed. Please refer to TIPS vol.37 for nginx installation instructions.
We will configure Node.js using nginx’s reverse proxy feature.
Add the following settings to your nginx configuration file.
upstream sampleApp {
server localhost:3000;
}
server {
listen 80;
server_name api.example.jp;
access_log /var/log/nginx/api.example.jp.log;
location / {
proxy_pass http://sampleApp/;
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-Host $host;
proxy_set_header X-Forwarded-Server $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
The access_log path and domain name can be changed as needed.
In this example, the application is configured to run on the subdomain api.example.jp.
The important point is to ensure that the name defined in the upstream section matches the destination specified in proxy_pass.
After saving the configuration, reload nginx.
$ sudo systemctl reload nginx
Now access the configured domain in your web browser.
If the Express test page is displayed successfully, the setup is complete.
Summary
In this tutorial, we added configuration settings to an existing nginx environment in order to run a Node.js application, with the main goal of getting Node.js up and running successfully.
To build practical applications, however, it is necessary to continue learning both Node.js and Express in greater depth.
We hope to continue studying and exploring more practical use cases and implementations going forward.
Source: nodejs
Source: phiary “Installing Node.js with nvm (Node Version Manager)”
Source: Qiita @ogwmtnr (Mitsunori Ogawa) “Configuring Node.js + Express + forever and Routing Through nginx”
Source: Qiita @nkjm (Kazuki Nakajima) “Getting Started with App Development Using Express + Node.js”
RECENT POSTS

Vol.204
Brand experience: Reconnecting a company's current reality with society
Vol.203
What Is Design Management
Vol.202
Why Hiring No Longer Works— Redesigning Organizations and Decisions for an Uncertain Age
Vol.201
How to Choose a Branding Agency: 5 Criteria to Avoid Failure
Vol.200
Design Management: A Practical Guide for SMEs and Startups to Drive Real Results
Vol.199
How to Rebuild Brand Competitiveness: A Practical Guide to Brand Management for SMEs









