This article describes how to install the Node.js platform. You can use Node.js to host third-party applications or you can run your own applications.
Installation Prerequisites
- Your account must have the normal shell (not jailshell) enabled. To determine which shell your account is using, log in using SSH, and then type the following command:
echo $SHELL
uname -rIf the output from this command contains el6 (for example, 2.6.32-531.17.1.lve1.2.60.el6.x86_64) or el7 (for example, 3.10.0-714.10.2.lve1.5.12.el7.x86_64) , then your server is running CentOS 6 or CentOS 7. However, if you see el5h (for example, 2.6.32-531.23.3.lve1.2.66.el5h.x86_64), then your server is running CentOS 5 and does not support Node.js.
Installing Node.js and NPM
-
- Log in to your server using SSH.
- At the command prompt, type the following commands:
- To extract the Node.js files, type the following command:
tar xvf node-v4.4.1-linux-x64.tar.xz
- Rename the extracted folder to the more convenient nodejs name, type the following command:
mv node-v4.4.1-linux-x64 nodejs
- To install the node and npm binaries, type the following commands:
mkdir ~/bin cp nodejs/bin/node ~/bin cd ~/bin ln -s ../nodejs/lib/node_modules/npm/bin/npm-cli.js npm
After you run these commands, Node.js and npm are installed on your account. To verify this, type the following command:
cd ~ wget https://nodejs.org/dist/v4.4.1/node-v4.4.1-linux-x64.tar.xzThis command downloads the binaries for the Node.js version 4.4.1 LTS (long-term support) release. If you download a newer development version of Node.js, change the version numbers in the following commands to match the version you downloaded.
node --version npm --version
Starting a Node.js Application
After you install Node.js, you are ready to run Node.js applications. However, the exact steps to do this vary depending on the application configuration.
Method #1: Use npm
Many third-party and “production-ready” applications use the npmprogram to start application, as shown by the following command:
nohup npm start --production &
The & places the command in the background, and the nohup command ensures that the application continues running even if you log out of the current terminal session.
For this method to work, there must be a valid package.json file for the application. The package.json file contains project metadata that the npm program reads to determine how to start the application, manage its dependencies, and more.
To view the official npm documentation for the package.json file.
Method #2: Run node directly
For simple applications, or for any application that does not have a package.json file, you can run the node executable directly and specify the application filename. For example:
nohup node my_app.js &
However, you lose the benefits of using npm to manage the application.
As above, the & places the command in the background, and the nohupcommand ensures that the application continues running even if you log out of the current terminal session.
Stopping a Node.js Application
To stop a currently running Node.js application, type the following command:
pkill node
This command immediately stops all running Node.js applications.
Integrating a Node.js Application with the Web Server
- In a text editor, add the following lines to the .htaccess file in the /home/username/public_html directory, where username represents your account username:
- In both RewriteRule lines, replace XXXXX with the port on which your Node.js application listens. To run a Node.js application on a managed server, you must select an unused port between 49152 and 65535 (inclusive).
- Save the changes to the .htaccess file, and then exit the text editor. Visitors to your web site are redirected to the Node.js application listening on the specified port.
RewriteEngine On RewriteRule ^$ http://127.0.0.1:XXXXX/ [P,L] RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^(.*)$ http://127.0.0.1:XXXXX/$1 [P,L]